i18next-parser icon indicating copy to clipboard operation
i18next-parser copied to clipboard

Trans defaults cause variable names to be treated as keys

Open Caerbannog opened this issue 4 years ago • 7 comments

🐛 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

Caerbannog avatar Nov 16 '20 23:11 Caerbannog

@vincaslt Can you have a look?

karellm avatar Nov 17 '20 00:11 karellm

I can take a look sometime this week. It would really help to have a PR with a failing test case.

vincaslt avatar Nov 17 '20 08:11 vincaslt

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)

Caerbannog avatar Nov 17 '20 09:11 Caerbannog

any progress on this?

KaboomFox avatar Dec 16 '20 05:12 KaboomFox

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.

vincaslt avatar Dec 17 '20 16:12 vincaslt

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.

raine avatar Jan 14 '22 15:01 raine

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)

ahoisl avatar Jan 27 '22 16:01 ahoisl

Should be fixed as of 8.4.0

karellm avatar Aug 07 '23 12:08 karellm