Intl.Locale.prototype.collation

Intl.Locale 实例的 collation 访问器属性返回此语言环境的 排序规则类型,用于根据语言环境的规则对字符串进行排序。

¥The collation accessor property of Intl.Locale instances returns the collation type for this locale, which is used to order strings according to the locale's rules.

描述

¥Description

排序规则是对字符串进行排序的过程。每当必须对字符串进行排序并按特定顺序放置时(从搜索查询结果到数据库中的排序记录),都会使用它。虽然按顺序放置字符串的想法可能看起来微不足道,但顺序的想法可能会因地区和语言的不同而有所不同。collation 属性的值是在构造时通过区域设置标识符的 co 键或通过 Intl.Locale() 构造函数的 collation 选项设置的。如果两者都存在,则后者优先;如果两者都不存在,则该属性的值为 undefined

¥Collation is the process of ordering strings of characters. It is used whenever strings must be sorted and placed into a certain order, from search query results to ordering records in a database. While the idea of placing strings in order might seem trivial, the idea of order can vary from region to region and language to language. The collation property's value is set at construction time, either through the co key of the locale identifier or through the collation option of the Intl.Locale() constructor. The latter takes priority if they are both present; and if neither is present, the property has value undefined.

有关支持的排序规则类型的列表,请参阅 Intl.Locale.prototype.getCollations()

¥For a list of supported collation types, see Intl.Locale.prototype.getCollations().

collation 的集合访问器是 undefined。你不能直接更改此属性。

¥The set accessor of collation is undefined. You cannot change this property directly.

示例

¥Examples

与其他区域设置子标签一样,可以通过区域设置字符串或构造函数的配置对象参数将排序规则类型添加到 Intl.Locale 对象。

¥Like other locale subtags, the collation type can be added to the Intl.Locale object via the locale string, or a configuration object argument to the constructor.

通过区域设置字符串添加排序规则类型

¥Adding a collation type via the locale string

Unicode 语言环境字符串规范 中,排序规则类型是区域设置键 "扩展子标签"。这些子标签添加有关区域设置的附加数据,并使用 -u 扩展添加到区域设置标识符中。因此,可以将排序规则类型添加到传递到 Intl.Locale() 构造函数的初始区域设置标识符字符串中。要添加排序规则类型,请首先向字符串添加 -u 扩展名。接下来,添加 -co 扩展名以指示你要添加排序规则类型。最后,将排序规则类型添加到字符串中。

¥In the Unicode locale string spec, collation types are locale key "extension subtags". These subtags add additional data about the locale, and are added to locale identifiers by using the -u extension. Thus, the collation type can be added to the initial locale identifier string that is passed into the Intl.Locale() constructor. To add the collation type, first add the -u extension to the string. Next, add the -co extension to indicate that you are adding a collation type. Finally, add the collation type to the string.

js
const locale = new Intl.Locale("zh-Hant-u-co-zhuyin");
console.log(locale.collation); // "zhuyin"

通过配置对象参数添加排序规则类型

¥Adding a collation type via the configuration object argument

Intl.Locale() 构造函数有一个可选的配置对象参数,它可以包含多种扩展类型中的任何一种,包括排序规则类型。将配置对象的 collation 属性设置为所需的排序规则类型,然后将其传递到构造函数中。

¥The Intl.Locale() constructor has an optional configuration object argument, which can contain any of several extension types, including collation types. Set the collation property of the configuration object to your desired collation type, and then pass it into the constructor.

js
const locale = new Intl.Locale("zh-Hant", { collation: "zhuyin" });
console.log(locale.collation); // "zhuyin"

规范

Specification
ECMAScript Internationalization API Specification
# sec-Intl.Locale.prototype.collation

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看