On an Egyptian guest's receipt, "2 nights" is written ليلتان, one word with no number. "3 nights" becomes ٣ ليالٍ, while "11 nights" becomes ١١ ليلة, with yet another word. Picking a word form by count like this is called pluralization. Its sibling, gender, picks a word form by who is being talked about. Think of Mrs. Sri with a set of rubber stamps for writing the length of stay on receipts. For Indonesian one stamp is enough: "2 malam", "11 malam". For English two: "1 night" and "2 nights". For Arabic six, including a stamp just for exactly two nights. Intl.PluralRules is the clerk who reads the number and hands over the right stamp.
CLDR plural categories
CLDR (the locale data browsers carry) gives each language some of six categories: zero, one, two, few, many, other. Intl.PluralRules tells you a number's category:
new Intl.PluralRules('ar-EG').select(3); // "few"
new Intl.PluralRules('en-US').select(1); // "one"
new Intl.PluralRules('id-ID').resolvedOptions().pluralCategories; // ["other"]
| Locale | Categories | "nights" example |
|---|---|---|
id-ID |
other | 1 malam, 2 malam |
en-US |
one, other | 1 night, 2 nights |
ja-JP |
other | 1泊, 2泊 |
ar-EG |
zero, one, two, few, many, other | ليلة واحدة, ليلتان, ٣ ليالٍ, ١١ ليلة |
Categories are names, not numbers. In French, 0 and 1.5 are one too; in Russian, 21 is one too. Every language has other. Indonesian and Japanese have no plural marker, so a single other form is enough.
Which numbers land in which category
Arabic categories are worked out from the last two digits. The results of new Intl.PluralRules('ar-EG').select(n), the same in Chromium 147, Firefox 146, WebKit 26.4 and Node 22.22.1:
| n | Category | The nights message (from the react-intl lesson) |
|---|---|---|
| 0 | zero | ٠ ليلة |
| 1 | one | ليلة واحدة |
| 2 | two | ليلتان (the dual form, no digit) |
| 3 to 10 | few | ٣ ليالٍ |
| 11 to 99 | many | ١١ ليلة |
| 100, 101, 102 | other | ١٠٠ ليلة |
| 103 to 110 | few | ١٠٣ ليالٍ |
| 111 to 199 | many | ١١١ ليلة |
| 1.5 | other | ١٫٥ ليلة |
So 3 and 103 are both few, while 2 is two and 102 is other. Our nights message has no zero branch, so 0 uses other. One small difference between engines: Node 22.22.1 lists pluralCategories as few, many, one, two, zero, other, the three browsers as zero, one, two, few, many, other. The contents are the same.
Why count === 1 ? ... : ... breaks
const nf = new Intl.NumberFormat('ar-EG');
const naive = (n) => (n === 1 ? 'ليلة واحدة' : nf.format(n) + ' ليالٍ'); // the "one or many" pattern
| n | naive(n) |
Correct |
|---|---|---|
| 2 | ٢ ليالٍ | ليلتان |
| 11 | ١١ ليالٍ | ١١ ليلة |
| 100 | ١٠٠ ليالٍ | ١٠٠ ليلة |
A two-branch pattern only fits languages with two forms. Arabic has six, Russian has one, few, many and other. Keep every form in the dictionary and let Intl.PluralRules choose (through ICU plural or the _few suffix in i18next). Decimals differ too: new Intl.PluralRules('en-US', { minimumFractionDigits: 1 }).select(1) gives "other", because what is written is "1.0 nights".
Ordinal numbers: type: 'ordinal'
"Your 21st stay" needs other rules: ordinals.
const ord = new Intl.PluralRules('en-US', { type: 'ordinal' });
[1, 2, 3, 4, 11, 12, 13, 21, 22, 23, 111].map((n) => ord.select(n));
// ["one", "two", "few", "other", "other", "other", "other", "one", "two", "few", "other"]
{n, selectordinal, one {Your #st stay} two {Your #nd stay} few {Your #rd stay} other {Your #th stay}}
- English: 1st, 2nd, 3rd, 4th, then 11th, 12th, 13th (not 11st), 21st, 22nd, 23rd, 111th.
id-ID,ja-JPandar-EGhave onlyotherfor ordinals: "Menginap ke-21", "21回目のご宿泊".- In ICU it is
selectordinal; in i18next,t('stay', { count: 21, ordinal: true })reads the keysstay_ordinal_one,stay_ordinal_two,stay_ordinal_few,stay_ordinal_other.
Gender with select
id-ID: Terima kasih, {gender, select, male {Pak} female {Bu} other {Kak}} {name}!
ar-EG: {gender, select, female {شكرًا لكِ يا {name}!} other {شكرًا لكَ يا {name}!}}
en-US: Thank you, {name}!
- In
id-IDthe choice is a form of address: Pak Hadi, Bu Rina, Kak Alex. Inar-EGthe choice is grammar: the word "you" ends differently for a man (لكَ) and a woman (لكِ). English and Japanese need neither. - Grammatical gender isn't the guest's gender. The Arabic word ليلة (night) is grammatically feminine, so "one night" is always ليلة واحدة with the feminine واحدة, whoever the guest is. Nouns in French or German have their own gender too.
- Provide
other. ICU throwsSyntaxError: MISSING_OTHER_CLAUSEwithout that branch, and guests are free not to state a gender. Don't guess from a name ("Alex" could be anyone): store the form of address the guest picked, and useotherwhen it's empty.
Plural and select in one message
Put select outside and plural inside, with a whole sentence in each branch, so translators see complete sentences:
en-US: {gender, select,
male {He booked {n, plural, one {# night} other {# nights}}.}
female {She booked {n, plural, one {# night} other {# nights}}.}
other {They booked {n, plural, one {# night} other {# nights}}.}}
id-ID: {gender, select, male {Pak {name}} female {Bu {name}} other {Kak {name}}} memesan {n, plural, other {# malam}}.
intl-messageformat 12.1.3 gives "She booked 2 nights.", "They booked 1 night." and "Bu Rina memesan 2 malam.". In i18next the combination uses context plus count: the keys booked_male_one, booked_female_other, booked_one, booked_other, then t('booked', { context: gender, count: n }). For an unknown gender pass context: undefined, not 'other': with 'other', i18next 26.4.3 reads the key booked_other (the plural form) and writes "They booked 1 nights."
⚠️ Four wrong ideas about plurals and gender
count === 1 ? ... : ...is only right for English. In Arabic, 2, 11 and 100 nights all come out wrong.oneisn't the same as the number 1. In French 0 isonetoo, and in Russian 21 isonetoo.- Order needs its own rules. With cardinal rules you get "21th" or "11st". Use
type: 'ordinal'orselectordinal. - Don't guess gender from a name, and don't forget the
otherbranch. Ask the guest, then provideother.
TL;DR: CLDR has six plural categories: zero, one, two, few, many, other. Intl.PluralRules is the browser API that decides a number's category for a locale: id-ID and ja-JP only other, en-US one and other, ar-EG all six (0 zero, 1 one, 2 two, 3 to 10 few, 11 to 99 many, 100 to 102 other, then 103 few again). Pluralization can't just check count === 1, because some languages have more than two plural forms. Order uses { type: 'ordinal' } (1st, 2nd, 3rd, 4th, 11th, 21st) or ICU selectordinal. Gender uses select with its required other branch: Pak/Bu/Kak in id-ID, لكَ/لكِ in ar-EG; grammatical gender (ليلة is feminine) has nothing to do with the guest's gender, and gender must never be guessed from a name. Combine with select outside and plural inside; in i18next use context plus count, and pass context: undefined for an unknown gender.
⚡ Categories, not numbers
Write:
new Intl.PluralRules('ar-EG').select(3); // "few"
new Intl.PluralRules('id-ID').resolvedOptions().pluralCategories; // ["other"]
Meaning: CLDR gives each language some of six categories: zero, one, two, few, many, other. id-ID and ja-JP have only other, en-US has one and other, ar-EG has all six.
⚡ Why count === 1 isn't enough
Guess first: with the pattern n === 1 ? 'ليلة واحدة' : nf.format(n) + ' ليالٍ', what does "2 nights" become for an Egyptian guest?
٢ ليالٍ, while the right form is the dual ليلتان. 11 and 100 nights come out wrong too.
Meaning: A two-branch pattern only fits languages with two forms. Some languages have more than two plural forms, so keep every form in the dictionary and let Intl.PluralRules choose.
⚡ Order has its own rules
Write:
const ord = new Intl.PluralRules('en-US', { type: 'ordinal' });
Meaning: Positions use the ordinal rules: 1st, 2nd, 3rd, 4th, then 11th and 21st. In ICU it is selectordinal.
⚡ Gender with select
Meaning: select picks a form of address in id-ID (Pak, Bu, Kak) and grammar in ar-EG (لكَ or لكِ). The other branch is required, and gender must never be guessed from a name: store the form of address the guest picked.