Intl.PluralRules

Baseline 廣泛可用 *

此功能已成熟,並可在多種裝置和瀏覽器版本上使用。自 2019 年 9 月以來,它已在各種瀏覽器中可用。

* 此特性的某些部分可能存在不同級別的支援。

Intl.PluralRules 物件用於啟用對複數敏感的格式化和與複數相關的語言規則。

描述

語言在表達物品的複數數量(基數)和表達物品的順序(序數)時使用不同的模式。英語在表達基數時有兩種形式:一種用於單數“item”(1 hour,1 dog,1 fish),另一種用於零個或任何其他數量的“items”(0 hours,2 lemmings,100000.5 fish),而中文只有一種形式,而阿拉伯語有六種!同樣,英語在表達序數時有四種形式:“th”、“st”、“nd”、“rd”,給出序列:0th、1st、2nd、3rd、4th、5th、...、21st、22nd、23rd、24th、25th,依此類推,而中文和阿拉伯語在序數方面只有一種形式。

給定一種特定語言和一組格式化選項,方法 Intl.PluralRules.prototype.select()Intl.PluralRules.prototype.selectRange() 返回一個標籤,該標籤代表單個數字或數字範圍的複數形式,無論是基數還是序數。程式碼可以使用返回的標籤為給定語言恰當地表示數字。可能返回的標籤的完整集合是:zeroonetwofewmanyother(“通用”複數形式,當語言只有一種形式時也使用)。

由於英語在基數方面只有兩種形式,select() 方法只返回兩個標籤:“one”用於單數情況,而“other”用於所有其他基數。這使得在英語中可以構造出符合邏輯的句子,例如:“1 dog is happy; do you want to play with it?”(1 只狗是快樂的;你想和它玩嗎?)和“10 dogs are happy; do you want to play with them?”(10 只狗是快樂的;你想和它們玩嗎?)。

為每種形式建立合適的句子取決於語言,即使在英語中,這也不僅僅是簡單地在名詞後面加“s”來構成複數形式。以上面的例子為例,我們看到形式可能會影響

  • 名詞:1 dog,2 dogs(但不是“fish”或“sheep”,它們的單複數形式相同)。
  • 動詞:1 dog is happy(1 只狗很快樂),2 dogs are happy(2 只狗很快樂)。
  • 代詞(及其他指代):Do you want to play with it / them.(你想和它/它們玩嗎。)

其他語言有更多的形式,選擇合適的句子可能更復雜。

在英語中,select() 對於序數可以返回四種標籤之一,代表所有允許的形式:“one”用於“st”結尾的數字(1、21、31,…),“two”用於“nd”結尾的數字(2、22、32,…),“few”用於“rd”結尾的數字(3、33、43,…),以及“other”用於“th”結尾的數字(0、4-20,等)。同樣,返回的標籤允許恰當地格式化描述序數的字串。

有關規則及其使用方式的更多資訊,請參閱 Plural Rules。有關規則列表及其在不同語言中的應用方式,請參閱 LDML Language Plural Rules

建構函式

Intl.PluralRules()

建立一個新的 Intl.PluralRules 物件。

靜態方法

Intl.PluralRules.supportedLocalesOf()

返回一個數組,其中包含提供的區域設定中受支援的那些區域設定,而無需回退到執行時預設區域設定。

例項屬性

這些屬性定義在 Intl.PluralRules.prototype 上,並由所有 Intl.PluralRules 例項共享。

Intl.PluralRules.prototype.constructor

建立例項物件的建構函式。對於 Intl.PluralRules 例項,初始值是 Intl.PluralRules 建構函式。

Intl.PluralRules.prototype[Symbol.toStringTag]

[Symbol.toStringTag] 屬性的初始值是字串 "Intl.PluralRules"。此屬性用於 Object.prototype.toString()

例項方法

Intl.PluralRules.prototype.resolvedOptions()

返回一個新的物件,其中包含在物件初始化期間計算出的區域設定和排序選項。

Intl.PluralRules.prototype.select()

返回一個指示用於區域設定感知格式化的複數規則的字串。

Intl.PluralRules.prototype.selectRange()

此方法接收兩個值,並返回一個指示用於區域設定感知格式化的複數規則的字串。

示例

使用語言環境

此示例顯示了基數本地化複數規則的一些變化。

為了獲取應用程式使用者介面所使用的語言的格式,請確保使用 建構函式 locales 引數指定該語言(以及可能的某些回退語言)。

js
// US English
const enCardinalRules = new Intl.PluralRules("en-US");
console.log(enCardinalRules.select(0)); // "other"
console.log(enCardinalRules.select(1)); // "one"
console.log(enCardinalRules.select(2)); // "other"
console.log(enCardinalRules.select(3)); // "other"

// Arabic
const arCardinalRules = new Intl.PluralRules("ar-EG");
console.log(arCardinalRules.select(0)); // "zero"
console.log(arCardinalRules.select(1)); // "one"
console.log(arCardinalRules.select(2)); // "two"
console.log(arCardinalRules.select(6)); // "few"
console.log(arCardinalRules.select(18)); // "many"

使用選項

指定數字的複數形式也可能取決於 建構函式 options,例如數字如何四捨五入,以及它是基數還是序數。

此示例演示瞭如何將規則型別設定為“ordinal”(序數),以及這如何影響美式英語中某些數字的格式。

js
// US English - ordinal
const enOrdinalRules = new Intl.PluralRules("en-US", { type: "ordinal" });
console.log(enOrdinalRules.select(0)); // "other" (0th)
console.log(enOrdinalRules.select(1)); // "one"   (1st)
console.log(enOrdinalRules.select(2)); // "two"   (2nd)
console.log(enOrdinalRules.select(3)); // "few"   (3rd)
console.log(enOrdinalRules.select(4)); // "other" (4th)
console.log(enOrdinalRules.select(21)); // "one"  (21st)

使用返回的標籤格式化文字

以下程式碼擴充套件了前面的示例,展示瞭如何使用序數的返回標籤來格式化英語文字。

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

const suffixes = new Map([
  ["one", "st"],
  ["two", "nd"],
  ["few", "rd"],
  ["other", "th"],
]);
const formatOrdinals = (n) => {
  const rule = enOrdinalRules.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'

規範

規範
ECMAScript® 2026 國際化 API 規範
# pluralrules-objects

瀏覽器相容性

另見