i18next / i18next-parser

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

Incorrect extraction for multiple `useTranslation` on the same page #973

Open affanshahid opened 9 months ago

affanshahid commented 9 months ago

🐛 Bug Report

If I have multiple invocations of useTranslation using different namespaces on the same page, the extraction does not work correctly.

To Reproduce

Create a component like:

const useSchema = () => {
  const { t } = useTranslation('validation');
  t('errors.invalid_type_received_undefined') // Note: `errors.invalid_type_received_undefined` is in ns validation
  // ...
};

export const HomePlanStep = () => {
  const { t } = useTranslation(['application-address', 'common']);
  useSchema()

  return <p>{t('homePlanStep.submitBtnText')}</p> // Note: this is in ns application-address
};

The dictionary that gets generated incorrectly has errors.invalid_type_received_undefined associated with NS: 'application-address'. The actual app runs just fine like this, only the extraction generates incorrect dictionaries.

If I make the following change, the dictionary gets generated correctly:

const useSchema = () => {
  const { t } = useTranslation('validation');
  t('errors.invalid_type_received_undefined', { ns: 'validation' }) // Note: added explicit ns
  // ...
};

Now the dictionary does not associate errors.invalid_type_received_undefined with NS: 'application-address'.

Expected behavior

The extractor should not mix-up namespaces when extracting

Your Environment