Sub topic 14
Choosing Plural Forms with Intl.PluralRules
A label such as "1 file" or "5 files" looks simple until you translate it. English has two forms of a counted noun. Polish has four, Arabic has six, and Japanese has one. Intl.PluralRules tells you which form a locale uses for a given number. You still supply the words; the API only picks the category.
This chapter assumes you know JavaScript functions and objects, have read the earlier chapter on Intl locales and options (BCP 47 tags and resolvedOptions), and have seen Intl.NumberFormat used to format numbers.
The working model: number in, category out
Intl.PluralRules is built from a locale and an options object. Its select(n) method returns one of six fixed category names, all defined by the Unicode CLDR data:
| Category | Typical meaning | Example locale where it appears |
|---|---|---|
| `zero` | Special form for 0 | Arabic, Welsh, Latvian |
| `one` | Singular-like form | English, French, Russian |
| `two` | Dual form | Arabic, Welsh, Slovenian |
| `few` | Small quantities | Polish, Russian, Czech |
| `many` | Larger quantities or fractions | Polish, Russian, Arabic |
| `other` | Everything else, always present | Every locale |
The names are labels for rule sets and should not be read literally. In French, one covers both 0 and 1.5. In Russian, one covers 21 and 101. Your code only needs to map each category to a string, and it should never assume what numbers belong to a category.
const en = new Intl.PluralRules("en-US");
console.log(en.select(0)); // "other" ("0 files")
console.log(en.select(1)); // "one"
console.log(en.select(2)); // "other"
console.log(en.select(-1)); // "one" (sign is ignored)
const fr = new Intl.PluralRules("fr");
console.log(fr.select(0)); // "one" ("0 fichier")
console.log(fr.select(1.5)); // "one"
const pl = new Intl.PluralRules("pl");
console.log([1, 2, 5, 12, 22, 1.5].map((n) => pl.select(n)));
// ["one", "few", "many", "many", "few", "other"]Finding out which categories a locale needs
Translators need to know which forms to write. resolvedOptions().pluralCategories lists the categories the locale actually uses:
for (const locale of ["en", "ja", "ru", "pl", "ar"]) {
const cats = new Intl.PluralRules(locale).resolvedOptions().pluralCategories;
console.log(locale, [...cats].sort().join(", "));
}
// en one, other
// ja other
// ru few, many, one, other
// pl few, many, one, other
// ar few, many, one, other, two, zeroThe order of this array has varied between engine versions, so the example sorts it before printing. You can use this list in a test that checks every translation file has a key for each category its locale needs.
Cardinal versus ordinal
The type option chooses between two rule sets:
"cardinal"(the default) is for counts: "3 files"."ordinal"is for positions: "3rd place".
English cardinals have two categories, but English ordinals have four:
const ordinal = new Intl.PluralRules("en-US", { type: "ordinal" });
const suffixes = { one: "st", two: "nd", few: "rd", other: "th" };
const nth = (n) => n + suffixes[ordinal.select(n)];
console.log([1, 2, 3, 4, 11, 12, 13, 21, 22, 23, 101, 111].map(nth));
// ["1st", "2nd", "3rd", "4th", "11th", "12th", "13th",
// "21st", "22nd", "23rd", "101st", "111th"]The rules handle the teens (11th, 12th, 13th) correctly. Hand-written versions often get these wrong when they check only the last digit. You can use this to build day-of-month labels like "July 4th". Intl.DateTimeFormat does not produce English ordinal suffixes, so combine formatToParts with this helper if you need them.
A reusable pluralization helper
In real code you keep templates per category and fill in the formatted number. Build the rules and the number formatter once, then reuse them:
function makePlural(locale, forms, options = {}) {
const rules = new Intl.PluralRules(locale, options);
const numbers = new Intl.NumberFormat(locale, options);
return (n) => {
const category = rules.select(n);
const template = forms[category] ?? forms.other;
return template.replace("{n}", numbers.format(n));
};
}
const filesEn = makePlural("en", { one: "{n} file", other: "{n} files" });
console.log(filesEn(1), "|", filesEn(1200)); // "1 file | 1,200 files"
const filesPl = makePlural("pl", {
one: "{n} plik",
few: "{n} pliki",
many: "{n} plików",
other: "{n} pliku",
});
console.log([1, 3, 5, 22, 1.5].map(filesPl).join(" | "));
// "1 plik | 3 pliki | 5 plików | 22 pliki | 1,5 pliku"Falling back to other with ?? keeps the output readable when a translation is missing a key. The missing key is still a translation bug that your tests should catch.
Fraction digits change the category
Plural rules depend on the number as it is displayed, and that includes visible trailing zeros. In English, "1 star" is singular but "1.0 stars" is plural. Intl.PluralRules accepts the same digit options as Intl.NumberFormat (minimumFractionDigits, maximumFractionDigits, minimumIntegerDigits, and the significant-digit options) so it can match what you show:
const plain = new Intl.PluralRules("en");
const oneDecimal = new Intl.PluralRules("en", { minimumFractionDigits: 1 });
console.log(plain.select(1)); // "one" -> "1 star"
console.log(oneDecimal.select(1)); // "other" -> "1.0 stars"
const rating = makePlural(
"en",
{ one: "{n} star", other: "{n} stars" },
{ minimumFractionDigits: 1, maximumFractionDigits: 1 }
);
console.log(rating(1), "|", rating(4.25)); // "1.0 stars | 4.3 stars"This is why makePlural passes the same options to both constructors. If the formatter rounds 0.96 to "1" while the rules see 0.96, the category and the displayed text no longer match.
Ranges with selectRange
Text such as "1–3 files" needs the category of a range, which in some languages is not simply the category of the end value. Newer engines offer selectRange(start, end):
const rules = new Intl.PluralRules("en");
if (typeof rules.selectRange === "function") {
console.log(rules.selectRange(1, 3)); // "other"
} else {
console.log(rules.select(3)); // fallback: use the end value
}selectRange throws a RangeError if either argument is NaN. It does not throw when start is greater than end. Using the end value's category is a reasonable fallback for most locales on older runtimes.
Common errors
| Mistake | Symptom | Fix |
|---|---|---|
| `n === 1 ? "file" : "files"` | Wrong forms in Polish, Russian, Arabic, French zero | Use `select(n)` and a per-category map |
| Using cardinal rules for positions | "2th", "3th" | Pass `{ type: "ordinal" }` |
| Different digit options for rules and formatter | "1.0 star" | Pass identical options to both |
| Assuming `one` means exactly 1 | Russian "21" gets the wrong template | Map categories to strings and never reason about the numbers inside them |
| Building the rules object inside a loop | Slow rendering of long lists | Create once and reuse (covered in a later chapter) |
| Pluralizing relative times by hand | Duplicate logic | `Intl.RelativeTimeFormat` already handles plurals |
Exercise
Write a function minutesLeft(n) for Russian that returns "1 минута", "2 минуты", "5 минут", and "1,5 минуты" for the inputs 1, 2, 5, and 1.5. Use Intl.PluralRules and Intl.NumberFormat, and make 21 produce "21 минута".
Show answer
Map Russian's four categories: one → "минута", few → "минуты", many → "минут", other → "минуты" (fractions fall in other). Code: const rules = new Intl.PluralRules("ru"); const nf = new Intl.NumberFormat("ru"); const forms = { one: "минута", few: "минуты", many: "минут", other: "минуты" }; const minutesLeft = (n) => ${nf.format(n)} ${forms[rules.select(n)]}; Then 21 selects "one" and gives "21 минута", and 1.5 selects "other" and gives "1,5 минуты".
Exercise
A product page shows a rating with one decimal place using new Intl.NumberFormat("en", { minimumFractionDigits: 1 }), and the label is chosen with new Intl.PluralRules("en").select(rating). For a rating of 1, what does the user see, and how do you fix it?
Show answer
The number displays as "1.0", but the plain rules return "one", so the page shows "1.0 star". Construct the PluralRules with the same digit options, new Intl.PluralRules("en", { minimumFractionDigits: 1 }). It then returns "other", and the label reads "1.0 stars".
Exercise
Why does a hand-written English ordinal function that checks only n % 10 produce "11st", and how does the ordinal PluralRules avoid it?
Show answer
Checking only the last digit maps 11, 12, and 13 to "st", "nd", and "rd". The CLDR English ordinal rules first exclude values whose last two digits are 11–13 and put them in "other", so select(11) returns "other" and the helper produces "11th".