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

Nested plural key _zero not getting added to other translation files

Open llevonick opened this issue 3 years ago • 1 comments

🐛 Bug Report

A nested plural key for <key>_zero doesn't get added to the translation files after running npx i18next 'src/app/**/*.{ts,tsx,js,jsx}' despite the key being in the en.json file

To Reproduce

We have an en.json file with a bunch of nested keys, but this is the relevant piece with plural values:

{
  "comment": {
    "commentCount_zero": "No Comments",
    "commentCount_one": "<0>{{count}}</0> comment",
    "commentCount_other": "<0>{{count}}</0> comments"
  }
}

comment.commentCount is referenced in the code in this Trans component:

<Trans i18nKey="comment.commentCount" count={totalComments}>
  <div className={totalCommentClasses}>
    {{ count: totalComments }}
  </div>
    comments
</Trans>

I also tried swapping out the component for this piece of code & updating the json so there are no numbered brackets to see if it would make a difference when generating the keys for the translation file but it still runs into the same issue:

<div className={commentTextClasses}>
  {i18n.t('comment.commentCount', { count: totalComments })}
</div>
{
  "comment": {
    "commentCount_zero": "No Comments",
    "commentCount_one": "{{count}} comment",
    "commentCount_other": "{{count}} comments"
  }
}

The result after running npx i18next 'src/app/**/*.{ts,tsx,js,jsx}' is this in the es.json file (missing the commentCount_zero key):

{
  "comment": {
    "commentCount_one": "<0>{{count}}</0>comments",
    "commentCount_other": "<0>{{count}}</0>comments"
  }
}

(or this result when not using the Trans component):

{
  "comment": {
    "commentCount_one": "",
    "commentCount_other": ""
  }
}

Expected behavior

I would expect the commentCount_zero key to be added to the es.json translation file, but at the moment that key doesn't automatically get added & has to be added in manually

Your Environment

  • runtime version: node v12.20.0
  • i18next version: 21.6.11
  • os: Mac
  • i18next-parser version: 6.0.0

Let me know if you need any additional info!

llevonick avatar Mar 24 '22 19:03 llevonick

Looking forward to this

antoine-souesme avatar Jun 09 '22 09:06 antoine-souesme

Same problem.

giovannetti-eric avatar Jul 28 '23 17:07 giovannetti-eric

Ok, with more search, I think I found why:

The generated keys by i18next-parser will still depend on the plural rules of the locales specified. If you wish to have specific plurals generated (like _zero), you may have to add locales that have this specific form.

giovannetti-eric avatar Jul 28 '23 18:07 giovannetti-eric

The plural keys depend on the language indeed. So not all languages have the same keys. It's part of i18next and not the parser per say as we rely on the i18next plural resolver: https://github.com/i18next/i18next-parser/blob/d64d35acff0b1e392d131d847ff19d646f0853b8/src/transform.js#L198

In other words, it's a feature, not a bug.

karellm avatar Jul 31 '23 03:07 karellm