Intl.PluralRules
Picks the word form for a number: 1 file, 2 files.
Available since: ES2018
Syntax
new Intl.PluralRules(locales, options)Parameters
| Parameter | Type | Description |
|---|---|---|
| locales | string | Optional. The language. |
| options | object | Optional. type: cardinal (a count) or ordinal (a position). |
Return value
Intl.PluralRulesAn object with a select(number) method.
Examples
Forms for English:
const rules = new Intl.PluralRules('en');
const words = { one: 'file', other: 'files' };
for (const n of [1, 2, 5]) {
console.log(`${n} ${words[rules.select(n)]}`);
}Output:
1 file
2 files
5 filesRussian has more forms:
const rules = new Intl.PluralRules('ru');
console.log(rules.select(1), rules.select(3), rules.select(5), rules.select(21));Output:
one few many oneOrdinal numbers:
const rules = new Intl.PluralRules('en', { type: 'ordinal' });
const suffixes = { one: 'st', two: 'nd', few: 'rd', other: 'th' };
console.log([1, 2, 3, 4, 11, 22].map((n) => n + suffixes[rules.select(n)]).join(' '));Output:
1st 2nd 3rd 4th 11th 22ndNotes
The select() method returns a category, not a word: zero, one, two, few, many or other. You provide the words for each category yourself.
See also
- Intl.NumberFormat — Formats numbers, currencies, percents and units by language rules.
- Intl.RelativeTimeFormat — Formats relative time: 3 days ago, in 1 hour.
In the tutorial
- Formatting with Intl — Locale-aware formatting of numbers, money, dates and plural words in JavaScript: toLocaleString, Intl.NumberFormat, Intl.DateTimeFormat and Intl.PluralRules.