Intl.Locale.prototype.collation - JavaScript 编辑

The Intl.Locale.prototype.collation property is an accessor property that returns the collation type for the Locale, which is used to order strings according to the locale's rules.

Description

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 helps to make it easier for JavaScript programmers to access the collation type used by a particular locale.

Below is a table with the available collation types, taken from the Unicode collation specification.

Valid collation types

Collation TypeDescription
big5hanPinyin ordering for Latin, big5 charset ordering for CJK characters (used in Chinese)
compatA previous version of the ordering, for compatibility
dictDictionary style ordering (such as in Sinhala)

The direct collation type has been deprected. Do not use.

direct

Binary code point order (used in Hindi)
ducetThe default Unicode collation element table order
emojiRecommended ordering for emoji characters
eorEuropean ordering rules
gb2312Pinyin ordering for Latin, gb2312han charset ordering for CJK characters (used in Chinese)
phonebkPhonebook style ordering (such as in German)
phoneticPhonetic ordering (sorting based on pronunciation)
pinyinPinyin ordering for Latin and for CJK characters (used in Chinese)
reformedReformed ordering (such as in Swedish)
searchSpecial collation type for string search
searchjlSpecial collation type for Korean initial consonant search
standardDefault ordering for each language
strokePinyin ordering for Latin, stroke order for CJK characters (used in Chinese)
tradTraditional style ordering (such as in Spanish)
unihanPinyin ordering for Latin, Unihan radical-stroke ordering for CJK characters (used in Chinese)
zhuyin

Pinyin ordering for Latin, zhuyin order for Bopomofo and CJK characters (used in Chinese)

Examples

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

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 inital locale identifier string that is passed into the 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 to the string.

let stringColl = new Intl.Locale("en-Latn-US-u-co-emoji");
console.log(stringColl.collation); // Prints "emoji"

Adding a collation type via the configuration object argument

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.

let configColl = new Intl.Locale("en-Latn-US", {collation: "emoji"});
console.log(configColl.collation); // Prints "emoji"

Specifications

Specification
ECMAScript Internationalization API (ECMA-402)

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:145 次

字数:7071

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文