試一試
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 的複數化類別的字串。它可以是 zero、one、two、few、many 或 other 之一。
描述
此函式根據 Intl.PluralRules 物件的語言環境和格式選項選擇複數化類別。這些選項在 Intl.PluralRules() 建構函式中設定。
示例
使用 select()
首先,建立 Intl.PluralRules 物件,傳入適當的 locales 和 options 引數。此處我們為埃及阿拉伯語方言建立一個複數規則物件。由於未指定 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 |
瀏覽器相容性
載入中…