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".
Example
#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))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
#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, includingtype:"cardinal"(the default, cardinal numbers) or"ordinal"(ordinal numbers).
More examples
#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)))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)}`)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))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, andother. Withoutother, fractional numbers giveundefined. - Don’t recreate the rules by hand with conditions like
n % 10 < 5. It’s easy to miss exceptions like 12, 13, and 14, andIntl.PluralRulesalready 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
#- Intl.NumberFormatFormats numbers according to a given language’s rules: separators, currencies, percentages, and units.
- Intl.RelativeTimeFormatFormats relative time in the chosen language, e.g. "in 3 days", "2 hours ago", or "yesterday".
- Intl.DateTimeFormatFormats dates and times according to the rules of a chosen language and time zone.
- Template literalsText templates in backticks: insert values with ${} and span multiple lines.
Spotted a mistake or missing an example? Write to us.