Intl.supportedValuesOf()
Intl.supportedValuesOf()
静态方法返回一个数组,其中包含实现支持的日历、排序规则、货币、编号系统或单位值。
¥The Intl.supportedValuesOf()
static method returns an array containing the supported calendar, collation, currency, numbering systems, or unit values supported by the implementation.
重复项被省略,数组按字典升序排序(或更准确地说,使用 Array.prototype.sort()
和 undefined
比较函数)。
¥Duplicates are omitted and the array is sorted in ascending lexicographical order (or more precisely, using Array.prototype.sort()
with an undefined
compare function).
该方法可用于对特定实现中是否支持值进行功能测试,并仅在必要时下载填充程序。它还可用于构建允许用户选择其首选本地化值的 UI,例如当从 WebGL 或服务器端创建 UI 时。
¥The method can be used to feature-test whether values are supported in a particular implementation and download a polyfill only if necessary. It can also be used to build UIs that allow users to select their preferred localized values, for example when the UI is created from WebGL or server-side.
Try it
语法
参数
¥Parameters
key
-
一个键字符串,指示要返回的值的类别。这是以下之一:
"calendar"
,"collation"
,"currency"
,"numberingSystem"
,"timeZone"
,"unit"
.
返回值
¥Return value
唯一字符串值的排序数组,指示给定键的实现支持的值。
¥A sorted array of unique string values indicating the values supported by the implementation for the given key.
注意:虽然 IANA 数据库会不时更改,但 Unicode CLDR 数据库(浏览器使用)会保留旧时区名称以确保稳定性。某些浏览器可能会使用旧名称,而其他浏览器会用新名称覆盖它。请参阅
Intl.Locale.prototype.getTimeZones
了解更多信息。¥Note: While the IANA database changes from time to time, the Unicode CLDR database (which browsers use) keeps old time zone names for stability purposes. Some browsers may use the legacy name, while others override it with the new name. See
Intl.Locale.prototype.getTimeZones
for more information.
例外情况
示例
功能测试
获取键的所有值
¥Get all values for key
要获取日历支持的值,请使用键 "calendar"
调用该方法。然后你可以迭代返回的数组,如下所示:
¥To get the supported values for calendar you call the method with the key "calendar"
.
You can then iterate through the returned array as shown below:
Intl.supportedValuesOf("calendar").forEach((calendar) => {
// "buddhist", "chinese", "coptic", "dangi", etc.
});
注意:为日历值返回的数组将始终包含值 "gregory"(公历)。
¥Note: The array returned for calendar values will always include the value "gregory" (gregorian).
其他值均以同样的方式获取:
¥The other values are all obtained in the same way:
Intl.supportedValuesOf("collation").forEach((collation) => {
// "compat", "dict", "emoji", etc.
});
Intl.supportedValuesOf("currency").forEach((currency) => {
// "ADP", "AED", "AFA", "AFN", "ALK", "ALL", "AMD", etc.
});
Intl.supportedValuesOf("numberingSystem").forEach((numberingSystem) => {
// "adlm", "ahom", "arab", "arabext", "bali", etc.
});
Intl.supportedValuesOf("timeZone").forEach((timeZone) => {
// "Africa/Abidjan", "Africa/Accra", "Africa/Addis_Ababa", "Africa/Algiers", etc.
});
Intl.supportedValuesOf("unit").forEach((unit) => {
// "acre", "bit", "byte", "celsius", "centimeter", etc.
});
无效的键抛出 RangeError
规范
Specification |
---|
ECMAScript Internationalization API Specification # sec-intl.supportedvaluesof |
浏览器兼容性
BCD tables only load in the browser