Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better API for plurals, for better extraction #2180

Closed
joshhunt opened this issue May 9, 2024 · 3 comments
Closed

Better API for plurals, for better extraction #2180

joshhunt opened this issue May 9, 2024 · 3 comments

Comments

@joshhunt
Copy link

joshhunt commented May 9, 2024

馃殌 Feature Proposal

A new API for specifying plurals in code, with explicit default messages for all plural forms.

Motivation

Many people's i18n workflow is to write default phrases in UI code with explicit keys with t() / <Trans />, and then extract them with i18next-parser.

Plurals make this very awkward because there's no way to extract additional plural forms from source code, so you must write one form in code, run i18n-parser, then go edit the json messages file with the additional forms. It also means there's no single source of truth for phrases, as additional plural forms would only live in the json messages file.

Example

t.plural("inbox.new-messages", {
  other: "You have {count} new messages",
  one: "You have {count} new message"
}, { count })

I can't really see a great API for supporting this with the React <Trans />.


If accepted, I'm happy to work on this and contribute it to i18next, and the required changes to i18next-parser.

See also i18next/i18next-parser#998

@jamuhl
Copy link
Member

jamuhl commented May 9, 2024

defaultValue already supports defining plural forms: https://www.i18next.com/translation-function/essentials#overview-options

@adrai
Copy link
Member

adrai commented May 9, 2024

example:

        <Trans
          i18nKey="newMessages"
          count={count}
          tOptions={{
            defaultValue_one: "You have a message.",
            defaultValue_other: "You have {{count}} messages.",
          }}
        />

https://react.i18next.com/latest/trans-component#trans-props
image

@joshhunt
Copy link
Author

joshhunt commented May 9, 2024

Ahhh - good catch. I had not noticed that.

Personally, I think that API is kind of awkward, but happy to give it a go 馃憤

@joshhunt joshhunt closed this as completed May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants