Codectionary / Developer documentation / JavaScript

Plural Categories with Intl.PluralRules

Intl.PluralRules maps a number to a language’s plural category, such as one, few, many, or other. Categories are language rules, not a universal singular/plural boolean. The API selects categories; the application still supplies translated phrases for those categories.

Syntax

new Intl.PluralRules("en").select(count);

Examples

Plural Categories with Intl.PluralRules: working example

Run this standalone example in a modern browser console unless another environment is specified. Expected result: The console prints 0 items | 1 item | 2 items.

const rules = new Intl.PluralRules("en");
function describe(count) {
  const noun = rules.select(count) === "one" ? "item" : "items";
  return `${count} ${noun}`;
}
console.log([0, 1, 2].map(describe).join(" | "));
  1. Expected result: The console prints 0 items | 1 item | 2 items.
  2. English cardinal rules classify 1 as one and these other values as other.
  3. Other locales can require more than these two message forms.

Best practices

  • Keep complete localized messages in a translation system.
  • Use ordinal rules explicitly when formatting rankings instead of item counts.

Runtime & versions

Browsers and JavaScript runtimes. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

ECMAScript specification

Specifications & further reading