Intl.PluralRules
What you'll learn: How to automatically determine the correct plural form (like "1 item" vs "2 items") for any language using Intl.PluralRules.
Why Plural Rules Matter
Different languages have wildly different rules for pluralization. English has two forms (singular and plural), but Russian has three, Polish has four, and Arabic has six! Simply checking count === 1 only works for a handful of languages.
Think of Intl.PluralRules as a linguistic consultant that knows every language's grammatical number rules. Instead of hardcoding conditions, you ask it "what category does this number fall into?" and it returns a standardized label.
How It Works
Create an Intl.PluralRules instance for a specific locale, then use the select() method to determine which plural category a number belongs to:
const enRules = new Intl.PluralRules('en-US');
console.log(enRules.select(0)); // "other"
console.log(enRules.select(1)); // "one"
console.log(enRules.select(2)); // "other"
const ruRules = new Intl.PluralRules('ru-RU');
console.log(ruRules.select(1)); // "one" (1 день)
console.log(ruRules.select(2)); // "few" (2 дня)
console.log(ruRules.select(5)); // "many" (5 дней)
console.log(ruRules.select(1.5)); // "other" (1.5 дня)
The categories returned are standardized: "zero", "one", "two", "few", "many", and "other". Each language uses only the categories it needs. You then map these categories to your translated strings.
Practical Pattern
const rules = new Intl.PluralRules('pl-PL');
const messages = {
one: '# plik',
few: '# pliki',
many: '# plików'
};
function formatFiles(count) {
const category = rules.select(count);
return messages[category].replace('#', count);
}
Key Takeaway: Intl.PluralRules provides locale-aware plural category detection, freeing you from manually implementing complex grammatical number rules for every language you support.