Intl.PluralRules
Intl.PluralRules
对象启用复数敏感格式和复数相关的语言规则。
¥The Intl.PluralRules
object enables plural-sensitive formatting and plural-related language rules.
描述
¥Description
语言使用不同的模式来表达项目的复数(基数)和表达项目的顺序(序数)。英语有两种表达基数的形式:一种表示单数 "item"(1 小时,1 只狗,1 条鱼),另一种表示零或任何其他数量的 "items"(0 小时,2 只旅鼠,100000.5 条鱼),而中文只有一种形式,阿拉伯语有六种!同样,英语有四种表示序数的形式:"th"、"st"、"nd"、"rd",给出顺序:0、1、2、3、4、5、……、21、22、23、24、25 等等,而中文和阿拉伯语都只有一种序数形式。
¥Languages use different patterns for expressing both plural numbers of items (cardinal numbers) and for expressing the order of items (ordinal numbers). English has two forms for expressing cardinal numbers: one for the singular "item" (1 hour, 1 dog, 1 fish) and the other for zero or any other number of "items" (0 hours, 2 lemmings, 100000.5 fish), while Chinese has only one form, and Arabic has six! Similarly, English has four forms for expressing ordinal numbers: "th", "st", "nd", "rd", giving the sequence: 0th, 1st, 2nd, 3rd, 4th, 5th, ..., 21st, 22nd, 23rd, 24th, 25th, and so on, while both Chinese and Arabic only have one form for ordinal numbers.
给定特定语言和一组格式化选项,方法 Intl.PluralRules.prototype.select()
和 Intl.PluralRules.prototype.selectRange()
返回一个标记,该标记表示单个或一系列数字(基数或序数)的复数形式。代码可以使用返回的标签来表示适合给定语言的数字。可能返回的完整标签集是:zero
、one
、two
、few
、many
和 other
("general" 复数形式,如果该语言只有一种形式也可使用)。
¥Given a particular language and set of formatting options, the methods Intl.PluralRules.prototype.select()
and Intl.PluralRules.prototype.selectRange()
return a tag that represents the plural form of a single or a range of numbers, cardinal or ordinal.
Code can use the returned tags to represent numbers appropriately for the given language.
The full set of tags that might be returned are: zero
, one
, two
, few
, many
, and other
(the "general" plural form, also used if the language only has one form).
由于英语只有两种基数形式,因此 select()
方法仅返回两个标签:"one"
表示单数,"other"
表示所有其他基数。这允许构建对每种情况都有意义的英语句子,例如:“1 只狗很高兴;你想和它一起玩吗? 只狗很高兴;你想和他们一起玩吗?”。
¥As English only has two forms for cardinal numbers, the select()
method returns only two tags: "one"
for the singular case, and "other"
for all other cardinal numbers.
This allows construction of sentences that make sense in English for each case, such as: "1 dog is happy; do you want to play with it?" and "10 dogs are happy; do you want to play with them?".
为每种形式创建适当的句子取决于语言,即使在英语中也可能不像在名词上添加 "s" 来形成复数形式那么简单。使用上面的示例,我们看到该表单可能会影响:
¥Creating appropriate sentences for each form depends on the language, and even in English may not be as simple as just adding "s" to a noun to make the plural form. Using the example above, we see that the form may affect:
- 名词:1 只狗/2 只狗(但不包括 "fish" 或 "sheep",它们具有相同的单复数形式)。
- 动词:1 只狗快乐,2 只狗快乐
- 代词(和其他指称):你想和它/它们一起玩吗?
其他语言有更多的形式,选择合适的句子可能更加复杂。
¥Other languages have more forms, and choosing appropriate sentences can be even more complex.
select()
可以返回英语序数的四个标签中的任何一个,代表每种允许的形式:one
代表 "st" 号码(1, 21, 31, ...),two
代表 "nd" 号码(2, 22, 32, ...),few
代表 "rd" 号码(3, 33, 43, ...),以及 other
对于 "th" 数字(0、4-20 等)。同样,返回的标签允许对描述序数的字符串进行适当的格式化。
¥select()
can return any of four tags for ordinal numbers in English, representing each of the allowed forms: one
for "st" numbers (1, 21, 31, ...), two
for "nd" numbers (2, 22, 32, ...), few
for "rd" numbers (3, 33, 43, ...), and other
for "th" numbers (0, 4-20, etc.).
Again, the returned tags allow appropriate formatting of strings describing an ordinal number.
有关规则及其使用方式的更多信息,请参阅 复数规则。有关规则列表以及它们如何应用于不同语言,请参阅 LDML 语言复数规则。
¥For more information about the rules and how they are used, see Plural Rules. For a list of the rules and how they apply for different languages, see the LDML Language Plural Rules.
构造函数
静态方法
实例属性
¥Instance properties
这些属性在 Intl.PluralRules.prototype
上定义并由所有 Intl.PluralRules
实例共享。
¥These properties are defined on Intl.PluralRules.prototype
and shared by all Intl.PluralRules
instances.
Intl.PluralRules.prototype.constructor
-
创建实例对象的构造函数。对于
Intl.PluralRules
实例,初始值为Intl.PluralRules
构造函数。 Intl.PluralRules.prototype[Symbol.toStringTag]
-
[Symbol.toStringTag]
属性的初始值为字符串"Intl.PluralRules"
。该属性在Object.prototype.toString()
中使用。
实例方法
¥Instance methods
Intl.PluralRules.prototype.resolvedOptions()
-
返回一个新对象,其属性反映了在对象初始化期间计算的区域设置和排序规则选项。
Intl.PluralRules.prototype.select()
-
返回一个字符串,指示用于区域设置感知格式的复数规则。
Intl.PluralRules.prototype.selectRange()
-
此方法接收两个值并返回一个字符串,指示用于区域设置感知格式的复数规则。
示例
使用语言环境
¥Using locales
此示例显示了基数的本地化复数规则的一些变化。
¥This example shows some of the variations in localized plural rules for cardinal numbers.
为了获取应用用户界面中使用的语言的格式,请确保使用 构造函数 locales
参数指定该语言(可能还有一些后备语言):
¥In order to get the format for the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the constructor locales
argument:
// 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"
使用选项
¥Using options
指定数字的复数形式也可能取决于 构造函数 options
,例如数字如何四舍五入,以及它是基数还是序数。
¥The plural form of the specified number may also depend on constructor options
, such as how the number is rounded, and whether it is cardinal or ordinal.
此示例显示如何将规则类型设置为 "ordinal",以及这如何影响美国英语中某些数字的形式。
¥This example shows how you can set the type of rules to "ordinal", and how this affects the form for some numbers in US English.
// 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)
使用返回的标签格式化文本
¥Formatting text using the returned tag
下面的代码扩展了前面的示例,展示了如何使用返回的序数标记来格式化英语文本。
¥The code below extends the previous example, showing how you might use the returned tag for an ordinal number to format text in English.
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'
规范
Specification |
---|
ECMAScript Internationalization API Specification # pluralrules-objects |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also