i18next / i18next-parser

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

Does not parse plural default values from Trans component #580

Open addiebarron opened 2 years ago

addiebarron commented 2 years ago

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

karellm commented 2 years ago

Would you be interested in making a PR for this?

crazyyi commented 1 year ago

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.