i18next / i18next-parser

Parse your code to extract translation keys/values and manage your catalog files
MIT License
472 stars 195 forks source link

Key extraction to wrong namespace #494

Open tylerrasor opened 2 years ago

tylerrasor commented 2 years ago

🐛 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

karellm commented 2 years ago

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

tux21b commented 2 years ago

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'});
cah4a commented 1 year ago

any progress here?