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

Key extraction to wrong namespace

Open tylerrasor opened this issue 3 years ago • 3 comments

🐛 Bug Report

When using the useTranslation hook to get two different TFunctions scoped to different namespaces, extraction should place keys into the namespace of the corresponding TFunction.

To Reproduce

const { t } = useTranslation();
const { t: otherT } = useTranslations('otherNs');
const header = t('header string');
const otherHeader = otherT('other string that should not be extracted');

Expected behavior

Should get extracted to the default namespace:

// translation.json
"header string": "header string"

But gets extracted to the namespace provided to the other useTranslation hook:

// otherNs.json
"header string": "header string"

I believe this is the corresponding test that should be added, but haven't gotten much farther than that

// javascript-lexer.test.js
it('uses default namespace when other t function', () => {
    const Lexer = new JavascriptLexer()
    const content =
      'const {t} = useTranslation("baz");' +
      'const {t:otherT} = useTranslation("otherNs");' +
      't("bar");'
    assert.deepEqual(Lexer.extract(content), [
      { namespace: 'baz', key: 'bar', ns: 'baz' },
    ])
  })

image

Your Environment

  • runtime version: node v14
  • i18next version: 21.5.2
  • i18next-parser version: 5.3.0
  • os: Mac
  • any other relevant information

tylerrasor avatar Dec 22 '21 20:12 tylerrasor

@tylerrasor It is indeed a missing feature at the moment.

karellm avatar Apr 03 '22 22:04 karellm

I just discovered the same bug with key prefixes instead of namespaces. Looks like i18next-parser threads all t functions with the config that appeared last.

const { t: tRoot } = useTranslation();
const { t } = useTranslation('translation', {keyPrefix: 'staticData.locations'});

tux21b avatar May 02 '22 15:05 tux21b

any progress here?

cah4a avatar Sep 12 '23 15:09 cah4a