Sub topic 14
Choosing Plural Forms with Intl.PluralRules
This chapter assumes you know the basics of Intl covered earlier: BCP 47 locale tags, options objects, and resolvedOptions(). You should also be comfortable running JavaScript in a browser console or Node.js.
A UI that prints "1 days ago" or "2 file" looks broken. The quick fix, n === 1 ? "file" : "files", only works for English. Other languages split numbers into more groups. Polish uses different word forms for 2, 5, and 22. Arabic has six forms. Intl.PluralRules tells you which group a number belongs to for a given locale, so you can pick the right word.
Categories, Not Words
select(n) returns a plural category, which is one of "zero", "one", "two", "few", "many", or "other". It never returns the word itself. You keep a map from category to text, and "other" is the one category every locale has.
Cardinal Plurals
Cardinal plurals are for counting things ("3 files"). This is the default type.
const pl = new Intl.PluralRules("pl");
const plik = { one: "plik", few: "pliki", many: "plików", other: "pliku" };
for (const n of [1, 2, 5, 22, 25, 1.5]) {
console.log(n, plik[pl.select(n)]);
}
// 1 plik, 2 pliki, 5 plików, 22 pliki, 25 plików, 1.5 pliku
console.log(pl.resolvedOptions().pluralCategories);
// the categories Polish uses: few, many, one, other (order can vary by engine)resolvedOptions().pluralCategories lists the categories a locale actually uses. Use it to check that your message map covers every one of them.
Ordinal Plurals
Ordinal plurals are for positions ("1st", "2nd"). Pass { type: "ordinal" }:
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].map(nth));
// ["1st","2nd","3rd","4th","11th","12th","13th","21st","22nd","23rd"]The rules already handle 11, 12, and 13, so you don't need your own % 100 checks.
Common Errors
| Mistake | What goes wrong | Fix |
|---|---|---|
| `n === 1 ? a : b` | Wrong forms in Polish, Russian, Arabic, and other languages | Use `select(n)` and a category map |
| Map is missing a category | The lookup returns `undefined` | Fall back: `map[cat] ?? map.other` |
| Selecting on a different number than you display | The word doesn't match the shown number | Pass the same formatting digit options to both |
| Treating the category as text | The output reads "one" | Always look the category up in your map |
The digit-options mistake is easy to miss. In English, "1" takes "one" but "1.0" takes "other":
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"If you display a number with Intl.NumberFormat (covered in the next chapter), give PluralRules the same minimumFractionDigits and maximumFractionDigits. That way the category matches the digits the user actually sees.
Exercise
Using new Intl.PluralRules("en-US", { type: "ordinal" }) and the suffix map from this chapter, what does nth return for 111, 112, 113, and 121, and why?
Show answer
"111th", "112th", "113th", "121st". The English ordinal rules check the last two digits. Numbers ending in 11, 12, or 13 fall into "other", so they get "th". 121 ends in 21, which falls into "one", so it gets "st".
Exercise
A Polish message map has only one and other keys. What does map[new Intl.PluralRules("pl").select(5)] return, and how do you fix it?
Show answer
It returns undefined, because 5 is in the "many" category in Polish. Add few and many entries, using resolvedOptions().pluralCategories to check coverage, and look up with a fallback such as map[cat] ?? map.other.