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

Does not parse plural default values from Trans component

Open addiebarron opened this issue 3 years ago • 2 comments

Parser does not parse plural default values when using the Trans component

When generating translation files, the parser does not generate the default English string values for plurals when using the Trans component. The parser does work this way when using t.

To Reproduce

Use a Trans component with a defaultValue_plural passed into tOptions (this example uses JSON v3 plural rules).

const numCount = condition ? 1 : 3;
return (
  <Trans t={t} count={numCount} tOptions={{
      defaultValue_plural: "<1>Plural</1> default value"
    }}> 
    <strong>Singular</strong> default value
  </Trans>
)

Expected behavior

With the above example, the following JSON is generated:

{
  "<1>Singular</1> default value": "<1>Singular</1> default value",
  "<1>Singular</1> default value_plural": "<1>Singular</1> default value"
}

I would expect this result:

{
  "<1>Singular</1> default value": "<1>Singular</1> default value",
  "<1>Singular</1> default value_plural": "<1>Plural</1> default value"
}

When defaultValue_plural is passed into the options of a t call, the parser picks this up and inserts it into the translation JSON at the correct plural key. I would expect the same to happen with the Trans component, but it doesn't seem to. The behavior should be consistent with the Trans component.

In general I'd like to see better support for Trans with default values and plurals, though that may be in the court of the main i18next package. If anyone has any workaround suggestions (besides manually editing the JSON in this case), I'd love to hear it.

Your Environment

  • runtime version: node v16
  • i18next version: 21.6.4
  • os: Mac

addiebarron avatar May 27 '22 20:05 addiebarron

Would you be interested in making a PR for this?

karellm avatar May 28 '22 12:05 karellm

I tried t function and couldn't get the correct plurals either. It would always produce the defaultValue no matter what I put in defaultValue_plural.

crazyyi avatar Mar 20 '23 06:03 crazyyi