Creating and Using PluralRules
You'll learn how to create PluralRules instances and use the select() method to choose the correct plural form for any number.
The Core Idea
When displaying numbers with text, different languages have different plural rules. English has two forms ("1 item" vs "2 items"), but other languages might have three, four, or even more forms. Intl.PluralRules tells you which plural category a number belongs to in a given language, so you can pick the right word form.
Creating a PluralRules Instance
Just like other Intl objects, you create one by specifying a locale:
const rules = new Intl.PluralRules('en-US');
Using select() to Find the Plural Category
The select() method takes a number and returns a string indicating the plural category: "zero", "one", "two", "few", "many", or "other".
const rules = new Intl.PluralRules('en-US');
console.log(rules.select(0)); // "other"
console.log(rules.select(1)); // "one"
console.log(rules.select(2)); // "other"
console.log(rules.select(5)); // "other"
Practical Use: Building Localized Messages
You typically use the returned category to select the appropriate word:
const rules = new Intl.PluralRules('en-US');
const pluralForms = {
one: 'item',
other: 'items'
};
function formatItems(count) {
const form = rules.select(count);
return `${count} ${pluralForms[form]}`;
}
console.log(formatItems(1)); // "1 item"
console.log(formatItems(5)); // "5 items"
Think of PluralRules as a language-aware decision tree: you give it a number, and it tells you which grammatical path to take.
Key Takeaway: Use new Intl.PluralRules(locale) to create an instance, then call .select(number) to determine which plural category applies, enabling proper localized pluralization.