Intl.PluralRules
Baseline 广泛可用 *
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() 返回一个标签,该标签代表单个数字或数字范围的复数形式,无论是基数还是序数。代码可以使用返回的标签为给定语言恰当地表示数字。可能返回的标签的完整集合是:zero、one、two、few、many 和 other(“通用”复数形式,当语言只有一种形式时也使用)。
由于英语在基数方面只有两种形式,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 参数指定该语言(以及可能的某些回退语言)。
// 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”(序数),以及这如何影响美式英语中某些数字的格式。
// 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)
使用返回的标签格式化文本
以下代码扩展了前面的示例,展示了如何使用序数的返回标签来格式化英语文本。
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 |
浏览器兼容性
加载中…