Skip to content

.tip

· one of the small things

Intl.PluralRules instead of count === 1

count === 1 ? "file" : "files" is a rule for English and nothing else. Russian takes three forms, Arabic six, and that branch picks the wrong one the moment someone switches locale. Intl.PluralRules tells you which form a number needs.

const plural = new Intl.PluralRules("ru");
plural.select(1); // "one"
plural.select(3); // "few"
plural.select(11); // "many"

Key the message table by those category names and look the form up instead of branching on the number. With { type: "ordinal" } the same call gives you the categories behind "1st" and "2nd".

javascripti18n