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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@formatjs/intl-messageformat, 10.5.0: Plural rules aren't applied correctly #4356

Closed
git-decadent opened this issue Feb 19, 2024 · 2 comments
Labels

Comments

@git-decadent
Copy link

Which package?

@formatjs/intl-messageformat @ 10.5.0

Describe the bug

The problem is related to how the plural rule is applied to the string, which is translated using react.i18next in conjunction with i18next-icu where intl-messageformat is a peer dependency. Initially, I suspected the issue lay with i18next-icu. However, during debugging, I traced it back to this line of code:

    const rule = formatters
          .getPluralRules(locales, {type: el.pluralType})
          .select((value as number) - (el.offset || 0))

Translations json:

export default {
    selectedCalendars: '{calendars, plural, =1 {one selected calendar} other {# selected calendars}}',
};

The use-case:

import i18next from "i18next";
import ICU from "i18next-icu";

i18next.use(ICU).init({
   ...
});

// Correct: "one selected calendar"
i18next.t("selectedCalendars", { calendars: 1 }); 

// Wrong: "{calendars, plural, =1 {one selected calendar} other {# selected calendars}}"
i18next.t("selectedCalendars", { calendars: 2 }); 

None of the rules like other, many, one, etc are functioning correctly. intl-messageformat throws an error, specifically: [TypeError: Cannot read property 'fn' of undefined]. This error occurs when the formatters.getPluralRules() invokes select() here.

If I remove the arguments passed to select() call like below, everything works as expected:

    var rule = formatters
        .getPluralRules(locales, { type: el.pluralType })
        .select();

To Reproduce

See the section Describe the bug

Expected behavior

Correct translation rules for plurals.

Smartphone (please complete the following information):

  • Device: iPhone 11, iPhone 15 in xCode simulator
  • OS: iOS 17.3. iOS 17.0
  • React Native App:
    "react": "~18.2.0",
    "react-i18next": "~13.3.1",
    "react-native": "~0.72.0",
    "i18next-icu": "~2.3.0",
    "intl-messageformat": "10.5.0",
    "@formatjs/intl-locale": "~3.4.5",
    "@formatjs/intl-pluralrules": "~5.2.12",

Additional context

The issue might stem from @formatjs/intl-pluralrules.

@git-decadent
Copy link
Author

git-decadent commented Feb 19, 2024

More debugging traced it to intl-pluralrules where the locale parameter passed to PluralRuleSelect() is undefined.

Then later this error [TypeError: Cannot read property 'fn' of undefined] takes place here.

@git-decadent
Copy link
Author

Apparently, my app was missing localeData to be imported.
So my bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant