CodeBlanks Logo Icon CodeBlanks

Intl.PluralRules

JavaScript course · Expert · Lesson 54/63

Learn to pick a plural category per locale with Intl.PluralRules.

Intl.PluralRules

Intl.PluralRules returns the plural category of a number for a given locale. English has two categories: one and other. Slavic languages have more: one, few, many. Pass a locale string to the constructor; the select method takes a number and returns the category name. To handle '1 message' and '3 messages' correctly in translation, you look at this category. Each locale has its own behavior; you do not write it yourself.

The type option

The type option picks the rule kind. The default 'cardinal' expresses quantity: '1 apple', '3 apples'. 'ordinal' shows order: '1st', '2nd', '3rd'. In English the ordinal rule has four categories: one, two, few, other. Turkish forms ordinals with the '-inci' suffix and returns similar categories. Stick with the default cardinal when order is not needed.

Exercise English plural category

Create an Intl.PluralRules for the 'en' locale. Call select with 3 and assign the result to the category variable.

Show example solution
1. Create with const plural = new Intl.PluralRules('en').
2. Get the category with plural.select(3).
3. Assign to the category variable.

Example solution:
const plural = new Intl.PluralRules('en');
const category = plural.select(3);

Test yourself

  1. What does Intl.PluralRules.select(3) return in English?

    • one
    • few
    • many
    • other
    Show answer

    other — Correct. English uses only one and other; 3 returns 'other'.

  2. Call the select method

    const plural = new Intl.PluralRules('en'); const category = plural.____(3);
    Show answer
    const plural = new Intl.PluralRules('en'); const category = plural.select(3);
  3. Match rule kinds to their use

    cardinal
    Expresses quantity
    ordinal
    Expresses order
    one
    Singular category
    other
    Plural category (English)

All lessons in Expert