Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Translation render prop #691

Merged
merged 4 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"coverage": "c8 --all --include='src/**/*[.js|.jsx|.ts|.tsx]' --reporter=lcov --reporter=text yarn test",
"test": "mocha -r @babel/register -r @babel/polyfill --recursive test/*.test.js test/**/*.test.js",
"test:cli": "yarn -s build && ./bin/cli.js '**/*.html' -o 'test/manual/$LOCALE/$NAMESPACE.json' && ./bin/cli.js -c test/cli/i18next-parser.config.js && ./bin/cli.js -c test/cli/i18next-parser.config.mjs && ./bin/cli.js -c test/cli/i18next-parser.config.ts && ./bin/cli.js -c test/cli/i18next-parser.config.yaml",
"test:watch": "mocha -r @babel/register -r @babel/polyfill --watch --parallel --recursive",
"watch": "babel src -d dist -w",
"prettify": "prettier --write \"{src,test}/**/*.js\"",
"build": "babel src -d dist",
Expand Down
5 changes: 5 additions & 0 deletions src/lexers/jsx-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ export default class JsxLexer extends JavascriptLexer {
const entry = {}
entry.key = getKey(tagNode)
return entry.key ? entry : null
} else if (tagNode.tagName.text === 'Translation') {
const namespace = getPropValue(tagNode, 'ns')
if (namespace) {
this.defaultNamespace = namespace
}
}
}

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

describe('<Translation>', () => {
it('extracts keys from render prop', (done) => {
const Lexer = new JsxLexer()
const content = `<Translation>{(t) => <>{t("first", "Main")}{t("second")}</>}</Translation>`
assert.deepEqual(Lexer.extract(content), [
{ defaultValue: 'Main', key: 'first' },
{ key: 'second' },
])
done()
})

it('sets ns (namespace) for expressions within render prop', (done) => {
const Lexer = new JsxLexer()
const content = `<Translation ns="foo">{(t) => t("first")}</Translation>`
assert.deepEqual(Lexer.extract(content), [
{ key: 'first', namespace: 'foo' },
])
done()
})
})

describe('<Trans>', () => {
it('extracts keys from i18nKey attributes from closing tags', (done) => {
const Lexer = new JsxLexer()
Expand Down