Intl.PluralRules

Picks the correct grammatical category for a number in a given language, such as one, few, or many.

Returns
An object with a select() method that returns a category: "zero", "one", "two", "few", "many", or "other".
On this page

Example

#
JavaScript
const rules = new Intl.PluralRules('en-US')
const forms = { one: 'point', other: 'points' }

function points(n) {
  return `${n} ${forms[rules.select(n)]}`
}

console.log(points(1))
console.log(points(3))
console.log(points(5))
console.log(points(12))
console.log(points(22))
Console
1 point
3 points
5 points
12 points
22 points

Definition and usage

#

Intl.PluralRules tells you which grammatical category a number belongs to in the chosen language. The select(n) method returns the category name, and you pick the matching word yourself. The object doesn’t know the words themselves, only the rules, so you prepare the word forms yourself, usually in an object keyed by category.

Polish, for example, has three categories for whole numbers: "one" for numbers like 1, "few" for numbers ending in 2, 3, or 4, except 12, 13, and 14, and "many" for the rest, including 0 and most numbers from 5 to 21. For fractions, the rules return "other". English has only "one" and "other", so a simple n === 1 ? ... : ... isn’t enough for many languages.

The type: "ordinal" option switches the rules to ordinal numbers, which is mainly useful in English ("1st", "2nd", "3rd", "4th"). Create the rules object once and reuse it. The categories available in a given language are returned by resolvedOptions().pluralCategories.

Syntax

#
Syntax
new Intl.PluralRules(locales)
new Intl.PluralRules(locales, options)

rules.select(number)

Parameters

#
  • locales

    optional, string or array

    A locale code, e.g. "pl-PL" or "en-US". If omitted, it means the browser’s language.
  • options

    optional object

    Settings, including type: "cardinal" (the default, cardinal numbers) or "ordinal" (ordinal numbers).

More examples

#
Categories in Polish and English
JavaScript
const pl = new Intl.PluralRules('pl-PL')
const en = new Intl.PluralRules('en-US')
const numbers = [0, 1, 2, 5, 1.5]

console.log(numbers.map((n) => pl.select(n)))
console.log(numbers.map((n) => en.select(n)))
Console
['many', 'one', 'few', 'many', 'other']
['other', 'one', 'other', 'other', 'other']
A universal pluralization function
JavaScript
const rules = new Intl.PluralRules('en-US')

function pluralize(count, forms) {
  return `${count} ${forms[rules.select(count)]}`
}

const lessons = { one: 'lesson', other: 'lessons' }
const days = { one: 'day', other: 'days' }

console.log(pluralize(1, lessons))
console.log(pluralize(4, lessons))
console.log(pluralize(11, lessons))
console.log(`Streak: ${pluralize(21, days)}`)
Console
1 lesson
4 lessons
11 lessons
Streak: 21 days
Ordinal numbers in English
JavaScript
const ordinal = new Intl.PluralRules('en-US', { type: 'ordinal' })
const suffixes = { one: 'st', two: 'nd', few: 'rd', other: 'th' }

const place = (n) => `${n}${suffixes[ordinal.select(n)]}`

console.log([1, 2, 3, 4, 11, 21, 22].map(place))
Console
['1st', '2nd', '3rd', '4th', '11th', '21st', '22nd']

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Prepare forms for all four Polish categories: one, few, many, and other. Without other, fractional numbers give undefined.
  • Don’t recreate the rules by hand with conditions like n % 10 < 5. It’s easy to miss exceptions like 12, 13, and 14, and Intl.PluralRules already knows them for every language.
  • Create the rules object once, outside the function, and only call select(). Creating a new object for every number is needlessly expensive.

Related entries

#

Spotted a mistake or missing an example? Write to us.