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

Empty translation string causes deletion of other translation(s)

Open bhr opened this issue 3 years ago • 3 comments

🐛 Bug Report

When t('') is used anywhere in code, other translations cannot be found/referenced by i18next-parser.

To Reproduce

return (
    <div className={clsx(style.root)}>
      <p>{t('TITLE')}</p>
      <p>
        {t('')}
        <Trans t={t} i18nKey="DESCRIPTION">
          Press <Button content={t('BUTTON_TITLE')} onClick={reset} plain underline /> to go back.
        </Trans>
      </p>
    </div>
  );

The key TITLE is removed form the translation.json

Expected behavior

The key TITLE should not be removed from the translation.json.

Fix / Workaround

Removing {t('')} fixes the issue. The TITLE key remains in the original translation

Your Environment

  • runtime version: i.e. node 16
  • i18next version: i.e. 5.3.0
  • os: Mac

bhr avatar Oct 20 '22 17:10 bhr

I experienced this same issue. It took a very long time to figure out why only half of my translations were showing up in the output file. It is due to src/helpers.js:35-41 which is this code.

  // There is no key to process so we return an empty object
  if (!key) {
    if (!target[entry.namespace]) {
      target[entry.namespace] = {}
    }
    return { target, duplicate, conflict }
  }

I don't understand why we would want to overwrite all previous translations just because there's an empty key. At the very least, keep old translations and just skip this key.

justinbc820 avatar Oct 16 '23 22:10 justinbc820

@justinbc820 Can you please make a PR to fix this?

karellm avatar Oct 29 '23 05:10 karellm