Intl.DateTimeFormat.supportedLocalesOf()
Intl.DateTimeFormat.supportedLocalesOf() 静态方法返回一个数组,其中包含提供的区域设置中在日期和时间格式化方面得到支持的区域设置,而无需回退到运行时的默认区域设置。
试一试
const locales = ["ban", "id-u-co-pinyin", "de-ID"];
const options = { localeMatcher: "lookup" };
console.log(Intl.DateTimeFormat.supportedLocalesOf(locales, options));
// Expected output: Array ["id-u-co-pinyin", "de-ID"]
// (Note: the exact output may be browser-dependent)
语法
js
Intl.DateTimeFormat.supportedLocalesOf(locales)
Intl.DateTimeFormat.supportedLocalesOf(locales, options)
参数
locales-
一个带有 BCP 47 语言标签的字符串,或者此类字符串的数组。关于
locales参数的一般形式和解释,请参阅Intl主页上的参数说明。 options可选-
一个可能具有以下属性的对象
localeMatcher-
要使用的区域设置匹配算法。可能的值是
"lookup"和"best fit";默认为"best fit"。有关此选项的信息,请参阅 Intl 页面。
返回值
一个字符串数组,代表给定区域设置标签的一个子集,这些标签在日期和时间格式化方面得到支持,而无需回退到运行时的默认区域设置。
示例
使用 supportedLocalesOf()
假设一个运行时支持印尼语和德语,但不支持巴厘语的日期和时间格式化,supportedLocalesOf 会原样返回印尼语和德语语言标签,尽管 pinyin 排序与日期和时间格式化无关,也未用于印尼语,并且不太可能支持专门用于印度尼西亚的德语。请注意此处指定了 "lookup" 算法——"best fit" 匹配器可能会认为印尼语足够匹配巴厘语,因为大多数巴厘语使用者也懂印尼语,因此也会返回巴厘语语言标签。
js
const locales = ["ban", "id-u-co-pinyin", "de-ID"];
const options = { localeMatcher: "lookup" };
console.log(Intl.DateTimeFormat.supportedLocalesOf(locales, options));
// ["id-u-co-pinyin", "de-ID"]
规范
| 规范 |
|---|
| ECMAScript® 2026 国际化 API 规范 # sec-intl.datetimeformat.supportedlocalesof |
浏览器兼容性
加载中…