Skip to content

Commit

Permalink
Merge pull request #804 from Angel-Studios/component-options
Browse files Browse the repository at this point in the history
add support for user-defined component functions
  • Loading branch information
karellm committed Apr 22, 2023
2 parents 3435168 + 76a237e commit f245677
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ Default configuration:
jsx: [{
lexer: 'JsxLexer',
attr: 'i18nKey', // Attribute for the keys
componentFunctions: ['Trans'], // Array of components to match
}],
}
```
Expand Down
3 changes: 2 additions & 1 deletion src/lexers/jsx-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class JsxLexer extends JavascriptLexer {
constructor(options = {}) {
super(options)

this.componentFunctions = options.componentFunctions || ['Trans']
this.transSupportBasicHtmlNodes =
options.transSupportBasicHtmlNodes || false
this.transKeepBasicHtmlNodesFor = options.transKeepBasicHtmlNodesFor || [
Expand Down Expand Up @@ -88,7 +89,7 @@ export default class JsxLexer extends JavascriptLexer {

const getKey = (node) => getPropValue(node, this.attr)

if (tagNode.tagName.text === 'Trans') {
if (this.componentFunctions.includes(tagNode.tagName.text)) {
const entry = {}
entry.key = getKey(tagNode)

Expand Down
17 changes: 17 additions & 0 deletions test/lexers/jsx-lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ describe('JsxLexer', () => {
done()
})

it('extracts keys from user-defined components', (done) => {
const Lexer = new JsxLexer({
componentFunctions: ['Translate', 'FooBar'],
})
const content = `<div>
<Translate i18nKey="something">Something to translate.</Translate>
<NotSupported i18nKey="jkl">asdf</NotSupported>
<FooBar i18nKey="asdf">Lorum Ipsum</FooBar>
</div>
`
assert.deepEqual(Lexer.extract(content), [
{ key: 'something', defaultValue: 'Something to translate.' },
{ key: 'asdf', defaultValue: 'Lorum Ipsum' },
])
done()
})

it('extracts keys from single line comments', (done) => {
const Lexer = new JsxLexer()
const content = `
Expand Down

0 comments on commit f245677

Please sign in to comment.