npm install intl-pluralrules
To use as a polyfill, just import it to ensure that
available in your environment:
Intl.PluralRules already exists and supports
multiple locales, the polyfill will not be
loaded. Full support for
minimumFractionDigits is not checked.
A complete implementation of PluralRules is available as
intl-pluralrules/plural-rules, if you'd prefer using it without modifying your
Intl object, or if you wish to use it rather than your environment's own:
'en' // 'one''en' minimumSignificantDigits: 3 // 'other'
In order to support all available locales, their data needs to be included in
the package. This means that when minified and gzipped, the above-documented
usage adds about 7kB to your application's production size. If this is a
concern, you can use
intl-pluralrules/factory and make-plural to build a
PluralRules class with locale support limited to only what you actually use.
Thanks to tree-shaking, this example that only supports English and French minifies & gzips to 1619 bytes. Do note that this size difference is only apparent with minified production builds.
const sel = en frconst getSelector = sellcconst cat = en: enCat fr: frCatconst getCategories = catlcord ? 'ordinal' : 'cardinal'const PluralRules =
All arguments of
getPluralRules(NumberFormat, getSelector, getCategories) are
Intl.NumberFormat, or a minimal implementation such as the one available at
intl-pluralrules/pseudo-number-format. It should at least support the
"en"locale and all of the min/max digit count options.
getSelector(lc)should return a
function(n, ord)returning the plural category of
n, using cardinal plural rules (by default), or ordinal rules if
nmay be a number, or the formatted string representation of a number. This may be called with any user-provided string
lc, and should return
undefinedfor invalid or unsupported locales.
getCategories(lc, ord)should return the set of available plural categories for the locale, either for cardinals (by default), or ordinals if
ordis true. This function will be called only with values for which
getSelectorreturns a function.