Intl.PluralRules.prototype.select()

Baseline 已廣泛支援

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

select() 方法 Intl.PluralRules 例項返回一個字串,該字串指示用於本地化數字格式的複數規則。

試一試

console.log(new Intl.PluralRules("ar-EG").select(0));
// Expected output: "zero"

console.log(new Intl.PluralRules("ar-EG").select(5));
// Expected output: "few"

console.log(new Intl.PluralRules("ar-EG").select(55));
// Expected output: "many"

console.log(new Intl.PluralRules("en").select(0));
// Expected output: "other"

語法

js
select(number)

引數

數字

用於獲取複數規則的數字。

返回值

表示 number 的複數化類別的字串。它可以是 zeroonetwofewmanyother 之一。

描述

此函式根據 Intl.PluralRules 物件的語言環境和格式選項選擇複數化類別。這些選項在 Intl.PluralRules() 建構函式中設定。

示例

使用 select()

首先,建立 Intl.PluralRules 物件,傳入適當的 localesoptions 引數。此處我們為埃及阿拉伯語方言建立一個複數規則物件。由於未指定 type,因此規則物件將提供基數格式(預設值)。

js
const pr = new Intl.PluralRules("ar-EG");

然後,在規則物件上呼叫 select(),指定需要其複數形式的數字。請注意,阿拉伯語有 5 種基數形式,如下所示。

js
pr.select(0); // 'zero'
pr.select(1); // 'one'
pr.select(2); // 'two'
pr.select(6); // 'few'
pr.select(18); // 'many'

規範

規範
ECMAScript® 2026 國際化 API 規範
# sec-intl.pluralrules.prototype.select

瀏覽器相容性

另見