How to pluralize words after numbers in JavaScript — “1 file, 2 files” in any language

You asked AI to show "Uploaded N files". It wrote:
`${n} ${n === 1 ? "file" : "files"}`
In English that's fine. Then you add Russian. The translator gives you "файл" and "файлов", and the same rule now prints "2 файлов" and "21 файлов" — both wrong.
Here's the catch: the n === 1 rule is copied from English, where a word has two forms. Russian has three, and which one you need depends on the last digits of the number — with special exceptions for 11–14. Polish, Ukrainian, Czech and Arabic have their own rules too. Good news: you don't have to code any of this by hand. JavaScript already has a built-in rulebook for every language. Let's go step by step.
How the Russian rule works
First, so you know what we're fixing. A Russian word after a number comes in three forms:
- one: 1, 21, 31, 101 — "файл";
- a few: 2–4, 22–24, 102 — "файла";
- many: 0, 5–20, 25, 111 — "файлов".
The trap is the teens. 11, 12, 13, 14 end in 1–4, but they're "many": "11 файлов", "12 файлов". Same for 111, 112.
There's a fourth case too — fractions: "1.5 файла". Almost everyone forgets it.
Step by step
-
Find every spot. Search the project for
=== 1 ?and> 1 ?next to text. There are usually more than you think: the cart, notifications, "N days left". -
Write down the word forms. For each word you need three forms plus one for fractions. An object is handy:
const FILE = { one: "файл", few: "файла", many: "файлов", other: "файла" }The keys
one,few,many,otheraren't random. They're the official category names — exactly what the browser returns. -
Plug in Intl.PluralRules. It's a built-in object that knows the rules of languages:
const ru = new Intl.PluralRules("ru") function plural(n, forms) { return `${n} ${forms[ru.select(n)]}` }ru.select(21)returns"one",ru.select(22)returns"few",ru.select(11)returns"many". No remainder math in your code. -
Create the rules once. Notice that
new Intl.PluralRules("ru")sits outside the function. If you create it inside on every call, a long list slows down noticeably. -
Test on the awkward numbers. Not 1 and 5, but the ones where it usually breaks. Here's what you should get:
plural(0, FILE)— 0 файловplural(1, FILE)— 1 файлplural(2, FILE)— 2 файлаplural(11, FILE)— 11 файловplural(21, FILE)— 21 файлplural(22, FILE)— 22 файлаplural(111, FILE)— 111 файловplural(1.5, FILE)— 1.5 файла
That's a ready-made set for an automated test. If you write tests with AI, hand it this whole list.
-
Do the same for English. English has just two categories:
const en = new Intl.PluralRules("en") en.select(1) // "one" en.select(21) // "other" — 21 filesBonus: English also has ordinals.
new Intl.PluralRules("en", { type: "ordinal" }).select(22)returns"two"— meaning "22nd".
What you get
One small function instead of a dozen ternaries. It handles any number correctly, including 0, 11–14, hundreds and fractions. To add a language, just pass another code: "uk", "pl", "de".
If your project already uses i18next
Then you don't write any of this. The library calls Intl.PluralRules for you. You just add keys with suffixes: files_one, files_few, files_many, files_other. And call t("files", { count: n }).
How to ask AI to get it right the first time
make it say 1 file 2 files in every languageAcross the project, plurals after numbers are done with n === 1 ? ... : .... Replace them with a single plural(n, forms) function built on Intl.PluralRules using the app locale. Store forms as an object with keys one, few, many, other. Create one PluralRules object per locale. Add a test for 0, 1, 2, 5, 11, 12, 21, 22, 111 and 1.5 in Russian, and 1, 2, 21 in English.When AI hands back the code, read it yourself: make sure no old === 1 is left somewhere.
What if I need the word without the number?
That works too: call forms[ru.select(n)] and output just the word. For example "Left: 3" with "days" next to it. You don't have to glue the number into the string.
Does this work in every browser?
Yes, in all modern ones. Intl.PluralRules has been in Chrome and Firefox since 2017–2018, in Safari since version 13, and in Node out of the box.
Why is 1.5 "файла" and not "файлов"?
That's how Russian works: fractions agree like "a few" — "полтора файла", "2,5 файла". The browser returns the other category for fractions, and that's where we put "файла".
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





