大网数据广告活动
Web 标准与前端开发

Intl.PluralRules() constructor

· 52CE 我爱测

The **`Intl.PluralRules()`** constructor creates Intl.PluralRules objects.

The Intl.PluralRules() constructor creates Intl.PluralRules objects.

Syntax

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

[!NOTE] Intl.PluralRules() can only be constructed with new. Attempting to call it without new throws a TypeError.

Parameters

  • locales
    • : A string with a BCP 47 language tag or an Intl.Locale instance, or an array of such locale identifiers. The runtime's default locale is used when undefined is passed or when none of the specified locale identifiers is supported. For the general form and interpretation of the locales argument, see the parameter description on the Intl main page.
  • options
    • : An object containing the following properties, in the order they are retrieved (all of them are optional):

      • localeMatcher
        • : The locale matching algorithm to use. Possible values are "lookup" and "best fit"; the default is "best fit". For information about this option, see Locale identification and negotiation.
      • type
        • : The type to use. Possible values are:
          • "cardinal" (default)
            • : For cardinal numbers (referring to the quantity of things).
          • "ordinal"
            • : For ordinal number (referring to the ordering or ranking of things, e.g., "1st", "2nd", "3rd" in English).

      Intl.PluralRules also supports the Intl.NumberFormat() digit options (see Intl.NumberFormat() for details):

      • minimumIntegerDigits
      • minimumFractionDigits
      • maximumFractionDigits
      • minimumSignificantDigits
      • maximumSignificantDigits
      • roundingPriority
      • roundingIncrement
      • roundingMode
      • trailingZeroDisplay

      These options are interpreted as if the notation option from Intl.NumberFormat is "standard" and style is "decimal".

Exceptions

  • RangeError
    • : Thrown if locales or options contain invalid values.
  • TypeError
    • : Thrown if roundingIncrement is not 1 and is combined with either significant-digit rounding or a roundingPriority other than "auto".

Examples

Basic usage

In basic use without specifying a locale, select() returns a plural category for the default locale and with default options. This is useful to distinguish between singular and plural forms, e.g., "dog" and "dogs".

const pr = new Intl.PluralRules();

pr.select(0); // 'other' if in US English locale

pr.select(1); // 'one' if in US English locale

pr.select(2); // 'other' if in US English locale

Using options

The results can be customized using the options argument, which has one property called type which you can set to ordinal. This is useful to figure out the ordinal indicator, e.g., "1st", "2nd", "3rd", "4th", "42nd", and so forth.

const pr = new Intl.PluralRules("en-US", { type: "ordinal" });

const suffixes = new Map([
  ["one", "st"],
  ["two", "nd"],
  ["few", "rd"],
  ["other", "th"],
]);
const formatOrdinals = (n) => {
  const rule = pr.select(n);
  const suffix = suffixes.get(rule);
  return `${n}${suffix}`;
};

formatOrdinals(0); // '0th'
formatOrdinals(1); // '1st'
formatOrdinals(2); // '2nd'
formatOrdinals(3); // '3rd'
formatOrdinals(4); // '4th'
formatOrdinals(11); // '11th'
formatOrdinals(21); // '21st'
formatOrdinals(42); // '42nd'
formatOrdinals(103); // '103rd'

Specifications

Browser compatibility

See also

  • Intl.PluralRules
  • Intl

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。