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

Consider using Intl.PluralRules instead of make-plurals #1446

Closed
timofei-iatsenko opened this issue Feb 19, 2023 · 2 comments · Fixed by #1486
Closed

Consider using Intl.PluralRules instead of make-plurals #1446

timofei-iatsenko opened this issue Feb 19, 2023 · 2 comments · Fixed by #1486

Comments

@timofei-iatsenko
Copy link
Collaborator

timofei-iatsenko commented Feb 19, 2023

This has decent browser support, including Node.js:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/PluralRules

https://caniuse.com/intl-pluralrules

So instead of:

import { i18n } from '@lingui/core';
import { en, cs } from 'make-plural/plurals'

i18n.loadLocaleData({
  en: { plurals: en },
  cs: { plurals: cs },
})

export async function dynamicActivate(locale: string) {
  const { messages } = await import(`./locales/${locale}/messages`)
  i18n.load(locale, messages)
  i18n.activate(locale)
}

We can do:

import { i18n } from '@lingui/core';

export async function dynamicActivate(locale: string) {
  const {messages} = await import(`./locales/${locale}/messages`)
  const pluralRules = new Intl.PluralRules(locale)

  i18n.loadLocaleData({
    [locale]: {plurals: (number) => pluralRules.select(number)}
  })
  i18n.load(locale, messages)
  i18n.activate(locale)
}

Things to consider:

  1. Can we make this as a default behavior with optional fallback to 'make-plural/plurals' or polyfill for older runtimes?
  2. Does Intl.PluralRules supported in ReactNative (in both platforms?) https://formatjs.io/docs/guides/runtime-requirements#react-native
  3. Nothing preventing us from updating docs right now with examples of usage Intl.PluralRules instead of make-plural/plurals

Links:

@Martin005
Copy link
Contributor

For our current implementation, it would not be as easy as only instantiating new Intl.PluralRules object. We would need two because when you make a new Intl.PluralRules, you need to specify if you want either cardinal (default) or ordinal plural rules, you can't get both from one object.

@timofei-iatsenko
Copy link
Collaborator Author

Yes, i know. I'm going to make it a default behavior so users won't need to i18n.loadLocaleData at all. All this setup would happen inside of a lib. We use Intl objects anyway, so it will not add additional requirements for runtime.

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

Successfully merging a pull request may close this issue.

3 participants