Intl.PluralRules.prototype.select()

Intl.PluralRules 实例的 select() 方法返回一个字符串,指示使用哪个复数规则来进行数字的区域设置感知格式设置。

¥The select() method of Intl.PluralRules instances returns a string indicating which plural rule to use for locale-aware formatting of a number.

Try it

语法

¥Syntax

js
select(number)

参数

¥Parameters

number

要获取复数规则的数字。

返回值

¥Return value

表示 number 的复数类别的字符串。这可以是 zeroonetwofewmanyother 之一。

¥A string representing the pluralization category of the number. This can be one of zero, one, two, few, many, or other.

描述

¥Description

此函数根据 Intl.PluralRules 对象的区域设置和格式选项选择复数类别。这些选项在 Intl.PluralRules() 构造函数中设置。

¥This function selects a pluralization category according to the locale and formatting options of an Intl.PluralRules object. These options are set in the Intl.PluralRules() constructor.

示例

¥Examples

使用选择()

¥Using select()

首先,创建一个 Intl.PluralRules 对象,传递适当的 localesoptions 参数。在这里,我们为埃及方言中的阿拉伯语创建了一个复数规则对象。由于未指定 type,规则对象将为基数提供格式(默认)。

¥First, create an Intl.PluralRules object, passing the appropriate locales and options parameters. Here we create a plural rules object for Arabic in the Egyptian dialect. Because the type is not specified the rules object will provide formatting for cardinal numbers (the default).

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

然后在规则对象上调用 select(),指定需要复数形式的数字。请注意,阿拉伯语有 5 种基数形式,如图所示。

¥Then call select() on the rules object, specifying the number for which the plural form is required. Note that Arabic has 5 forms for cardinal numbers, as shown.

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

规范

Specification
ECMAScript Internationalization API Specification
# sec-intl.pluralrules.prototype.select

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also