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

[Fix] Allow to use multiple translation functions with different namespaces and keyPrefixes #1083

Merged
merged 2 commits into from
Feb 16, 2025
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
91 changes: 68 additions & 23 deletions src/lexers/javascript-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class JavascriptLexer extends BaseLexer {
this.attr = options.attr || 'i18nKey'
this.parseGenerics = options.parseGenerics || false
this.typeMap = options.typeMap || {}
this.translationFunctionsWithArgs = {}
}

createCommentNodeParser() {
Expand Down Expand Up @@ -43,25 +44,49 @@ export default class JavascriptLexer extends BaseLexer {
}

setNamespaces(keys) {
if (this.defaultNamespace) {
return keys.map((entry) => ({
...entry,
namespace: entry.namespace || this.defaultNamespace,
}))
}

return keys
return keys.map((entry) => {
const namespace =
entry.ns ??
this.translationFunctionsWithArgs?.[entry.functionName]?.ns ??
this.defaultNamespace
return namespace
? {
...entry,
namespace,
}
: entry
})
}

setKeyPrefixes(keys) {
if (this.keyPrefix) {
return keys.map((key) => ({
...key,
keyPrefix: this.keyPrefix,
}))
}
return keys.map(({ functionName, ...key }) => {
const keyPrefix =
this.translationFunctionsWithArgs?.[functionName]?.keyPrefix ??
this.keyPrefix
return keyPrefix
? {
...key,
keyPrefix,
}
: key
})
}

return keys
variableDeclarationExtractor(node) {
const firstDeconstructedProp = node.name.elements?.[0]
if (
(firstDeconstructedProp?.propertyName ?? firstDeconstructedProp?.name)
?.escapedText === 't' &&
this.functions.includes(firstDeconstructedProp?.name?.escapedText) &&
this.namespaceFunctions.includes(node.initializer.expression?.escapedText)
) {
this.translationFunctionsWithArgs[
firstDeconstructedProp.name.escapedText
] = {
pos: node.initializer.pos,
storeGlobally: !firstDeconstructedProp.propertyName?.escapedText,
}
}
}

extract(content, filename = '__default.js') {
Expand All @@ -72,6 +97,9 @@ export default class JavascriptLexer extends BaseLexer {
const parseTree = (node) => {
parseCommentNode(keys, node, content)

if (node.kind === ts.SyntaxKind.VariableDeclaration) {
this.variableDeclarationExtractor.call(this, node)
}
if (
node.kind === ts.SyntaxKind.ArrowFunction ||
node.kind === ts.SyntaxKind.FunctionDeclaration
Expand Down Expand Up @@ -103,7 +131,7 @@ export default class JavascriptLexer extends BaseLexer {
)
parseTree(sourceFile)

return this.setNamespaces(keys)
return this.setKeyPrefixes(this.setNamespaces(keys))
}

/** @param {ts.FunctionLikeDeclaration} node */
Expand Down Expand Up @@ -161,12 +189,16 @@ export default class JavascriptLexer extends BaseLexer {
expressionExtractor(node) {
const entries = [{}]

const isNamespaceFunction =
this.namespaceFunctions.includes(node.expression.escapedText) ||
// Support matching the namespace as well, i.e. match `i18n.useTranslation('ns')`
this.namespaceFunctions.includes(this.expressionToName(node.expression))
const functionDefinition = Object.entries(
this.translationFunctionsWithArgs
).find(([name, translationFunc]) => translationFunc?.pos === node.pos)
let storeGlobally = functionDefinition?.[1].storeGlobally ?? true

if (isNamespaceFunction && node.arguments.length) {
if (
this.namespaceFunctions.includes(node.expression.escapedText) &&
node.arguments.length
) {
storeGlobally |= node.expression.escapedText === 'withTranslation'
const namespaceArgument = node.arguments[0]
const optionsArgument = node.arguments[1]
// The namespace argument can be either an array of namespaces or a single namespace,
Expand All @@ -193,7 +225,10 @@ export default class JavascriptLexer extends BaseLexer {
)
} else if (namespace.kind === ts.SyntaxKind.StringLiteral) {
// We found a string literal namespace, so we'll use this instead of the default
this.defaultNamespace = namespace.text
if (storeGlobally) {
this.defaultNamespace = namespace.text
}
entries[0].ns = namespace.text
}

if (
Expand All @@ -204,7 +239,10 @@ export default class JavascriptLexer extends BaseLexer {
(p) => p.name.escapedText === 'keyPrefix'
)
if (keyPrefixNode != null) {
this.keyPrefix = keyPrefixNode.initializer.text
if (storeGlobally) {
this.keyPrefix = keyPrefixNode.initializer.text
}
entries[0].keyPrefix = keyPrefixNode.initializer.text
}
}
}
Expand Down Expand Up @@ -344,10 +382,17 @@ export default class JavascriptLexer extends BaseLexer {
entries[0].namespace = entries[0].ns[0]
}
}
entries[0].functionName = node.expression.escapedText

return entries
}

const isTranslationFunctionCreation =
node.expression.escapedText &&
this.namespaceFunctions.includes(node.expression.escapedText)
if (isTranslationFunctionCreation) {
this.translationFunctionsWithArgs[functionDefinition?.[0]] = entries[0]
}
return null
}

Expand Down
3 changes: 3 additions & 0 deletions src/lexers/jsx-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export default class JsxLexer extends JavascriptLexer {
parseCommentNode(keys, node, content)

switch (node.kind) {
case ts.SyntaxKind.VariableDeclaration:
this.variableDeclarationExtractor.call(this, node)
break
case ts.SyntaxKind.CallExpression:
const entries = this.expressionExtractor.call(this, node)
if (entries) {
Expand Down
2 changes: 1 addition & 1 deletion test/lexers/javascript-lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe('JavascriptLexer', () => {
it('extracts the first valid namespace when it is an array', () => {
const Lexer = new JavascriptLexer()
const content =
'const ExtendedComponent = useTranslation([someVariable, "baz"]); t("bar");'
'const {t} = useTranslation([someVariable, "baz"]); t("bar");'
assert.deepEqual(Lexer.extract(content), [
{ namespace: 'baz', key: 'bar' },
])
Expand Down
44 changes: 44 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,50 @@ describe('parser', () => {
i18nextParser.end(fakeFile)
})

it('applies namespaces to the corresponding translation function', (done) => {
let result
let resultCommon
const i18nextParser = new i18nTransform({
lexers: {
tsx: [
{
lexer: 'JsxLexer',
functions: ['t', 'tCommon', 'tPrefix'],
},
],
},
})
const fakeFile = new Vinyl({
contents: fs.readFileSync(
path.resolve(__dirname, 'templating/multiple-translation-keys.tsx')
),
path: 'file.tsx',
})
const expectedCommon = {
foo: '',
random: { stuff: '' },
}
const expected = {
bar: '',
}

i18nextParser.on('data', (file) => {
if (file.relative.endsWith(path.normalize('en/test.json'))) {
result = JSON.parse(file.contents)
}
if (file.relative.endsWith(path.normalize('en/common.json'))) {
resultCommon = JSON.parse(file.contents)
}
})
i18nextParser.on('end', () => {
assert.deepEqual(result, expected)
assert.deepEqual(resultCommon, expectedCommon)
done()
})

i18nextParser.end(fakeFile)
})

it('handles escaped single and double quotes', (done) => {
let result
const i18nextParser = new i18nTransform()
Expand Down
17 changes: 17 additions & 0 deletions test/templating/multiple-translation-keys.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react'
import { useTranslation } from 'react-i18next'

function TestComponent() {
const { t: tCommon } = useTranslation('common')
const { t: tPrefix } = useTranslation('common', {keyPrefix: "random"})
const { t } = useTranslation('test')
return (
<>
<h1>{t('bar')}</h1>
<h2>{tCommon('foo')}</h2>
<h3>{tPrefix('stuff')}</h3>
</>
)
}

export default TestComponent
Loading