Sub topic 7
Plural forms across languages
This chapter assumes you have set up react-i18next, call t with a key, and pass variables to messages. Here you will see how plural forms work and why count === 1 ? ... : ... fails.
Why two forms are not enough
English has two forms: "1 item" and "2 items". Other languages have more. Russian distinguishes "1 товар", "2 товара" and "5 товаров". Arabic has six forms. Japanese has one.
The Unicode CLDR project defines up to six plural categories: zero, one, two, few, many and other. Each language uses a subset, and each has its own rule that maps a number to a category. Every language has other.
You can ask the browser which category a number falls into:
const ru = new Intl.PluralRules('ru');
console.log(ru.select(1)); // 'one'
console.log(ru.select(22)); // 'few'
console.log(ru.select(5)); // 'many'
console.log(ru.select(21)); // 'one'
console.log(ru.select(1.5)); // 'other'
const ar = new Intl.PluralRules('ar');
console.log(ar.select(0)); // 'zero'
console.log(ar.select(2)); // 'two'The number 21 is one in Russian because the rule looks at the last digits. Hard-coded checks against 1 cannot express that.
How i18next picks a key
i18next uses Intl.PluralRules internally. When you pass count, it computes the category for the active language. It then looks for the key plus a suffix, such as items_few. If that key is missing, it falls back to the base key.
flowchart LR
A["t('items', count: 22)"] --> B["Intl.PluralRules('ru').select(22)"]
B --> C["category: few"]
C --> D["lookup key items_few"]
D --> E["22 товара"]The block above is only for orientation. The diagram below is the version that renders.
A complete example
Each language catalog defines only the categories that language needs.
import i18n from 'i18next';
await i18n.init({
lng: 'ru',
fallbackLng: 'en',
interpolation: { escapeValue: false },
resources: {
en: {
translation: {
items_one: '{{count}} item',
items_other: '{{count}} items',
},
},
ru: {
translation: {
items_one: '{{count}} товар',
items_few: '{{count}} товара',
items_many: '{{count}} товаров',
items_other: '{{count}} товара',
},
},
},
});
for (const n of [1, 2, 5, 21, 1.5]) {
console.log(i18n.t('items', { count: n }));
}
// 1 товар, 2 товара, 5 товаров, 21 товар, 1.5 товараThe component code stays the same as in the English-only version: t('items', { count }). Translators add or remove suffixes without touching it.
Common errors
- Using
countunder another name. Only the option namedcounttriggers plural lookup.{ total: 3 }will not. - Writing old suffixes. Since i18next v21, keys use
_oneand_other. The older_pluralsuffix no longer works in the default format. - Omitting
other. It is the fallback category, so every language needs it. Without it, some numbers return the raw key. - Copying English categories. A Russian catalog with only
_oneand_othershows "5 товара", which is wrong. Check the CLDR chart for each language you add. - Hiding the number. Avoid "Item" versus "Items" with the count elsewhere. Keep
{{count}}inside the message, because word order varies.
Exercise
A Russian catalog defines itemsone, itemsfew, itemsmany and itemsother. Which key does t('items', { count: 22 }) use, and why does 21 not use items_many?
Show answer
It uses itemsfew, because Intl.PluralRules('ru').select(22) returns 'few' (last digit 2 to 4, excluding 12 to 14). The number 21 ends in 1 (not 11), so it falls in 'one', and i18next looks up itemsone.