i18next-parser
i18next-parser copied to clipboard
Trans defaults cause variable names to be treated as keys
🐛 Bug Report
The Trans component can be used to interpolate a line with i18next-react templates ("...<1>..."), but not to translate keys (what the t()
function does). Although I am aware that the Trans component can also perform key translation, there are cases where this is not desired.
In this case, parsing mistakenly extracts a key when nothing should be extracted. This is a regression that I discovered when upgrading from 3.0.1 to 3.3.0.
To Reproduce
const line = 'some generated template with <1>bold</1> text';
function Repro() {
return (
<Trans
defaults={line}
components={[<bold>text</bold>]}
/>
);
}
Parsing should generate an empty file, but produces the following file:
{ "line": "line" }
Your Environment
- runtime version: i.e. node v13
- i18next-parser: 3.3.0
- i18next: 19.8.3
- react-i18next: 11.7.3
- os: Linux
@vincaslt Can you have a look?
I can take a look sometime this week. It would really help to have a PR with a failing test case.
Here you go: https://github.com/i18next/i18next-parser/commit/18fa6457605555512af9082405d65fa684e0be8f. The test fails:
1) JsxLexer
<Trans>
skips variables in `defaults` prop:
AssertionError: expected [ Array(1) ] to deeply equal []
+ expected - actual
-[
- {
- "defaultValue": "variable"
- "key": "variable"
- }
-]
+[]
at Context.<anonymous> (test/lexers/jsx-lexer.test.js:48:14)
at processImmediate (internal/timers.js:461:21)
any progress on this?
No progress, haven't had time to get down to this.
I don't exactly understand why it shouldn't generate a default value. Is it documented somewhere? It does indeed seem incorrect to generate { line: 'line' }
but if key is missing and default is provided - it generates key from the default value I think.
Similar issue, but perhaps not exactly the same, with <Trans i18nKey={reason} ...>
Parser adds "reason": ""
to JSON. It doesn't seem right to add the variable name.
Similar issue, but perhaps not exactly the same, with
<Trans i18nKey={reason} ...>
Parser adds
"reason": ""
to JSON. It doesn't seem right to add the variable name.
Same issue here. You cannot pass the i18nKey as a variable, otherwise the variable name will be treated as if being the key. IMHO it should generate a warning like as it does for t(reason)
Should be fixed as of 8.4.0