试一试
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 |
浏览器兼容性
加载中…