Intl.RelativeTimeFormat.supportedLocalesOf()

Baseline 已广泛支持

此功能已成熟,并可在许多设备和浏览器版本上使用。自 2020 年 9 月起,所有浏览器均已提供此功能。

Intl.RelativeTimeFormat.supportedLocalesOf() 静态方法返回一个数组,其中包含所提供区域设置中支持相对时间格式而无需回退到运行时默认区域设置的区域设置。

试一试

const locales = ["ban", "id-u-co-pinyin", "de-ID"];
const options = { localeMatcher: "lookup" };

console.log(Intl.RelativeTimeFormat.supportedLocalesOf(locales, options));
// Expected output: Array ["id-u-co-pinyin", "de-ID"]
// (Note: the exact output may be browser-dependent)

语法

js
Intl.RelativeTimeFormat.supportedLocalesOf(locales)
Intl.RelativeTimeFormat.supportedLocalesOf(locales, options)

参数

locales

一个带有 BCP 47 语言标签的字符串,或者此类字符串的数组。关于 locales 参数的一般形式和解释,请参阅 Intl 主页上的参数说明

options 可选

一个可能具有以下属性的对象

localeMatcher

要使用的 locale 匹配算法。可能的值为 "lookup""best fit";默认值为 "best fit"。有关此选项的信息,请参阅 Intl 页面。

返回值

一个字符串数组,表示给定语言标签的子集,这些语言标签在相对时间格式化中得到支持,而无需回退到运行时的默认语言设置。

示例

使用 supportedLocalesOf()

假设运行时支持印尼语和德语,但不支持巴厘语的相对时间格式化,supportedLocalesOf 将按原样返回印尼语和德语的语言标签,尽管拼音排序与相对时间格式化无关,也未与印尼语一起使用,并且不太可能支持专门针对印度尼西亚的德语。请注意此处对 "lookup" 算法的规范——"best fit" 匹配器可能会决定印尼语足以匹配巴厘语,因为大多数巴厘语使用者也懂印尼语,因此也会返回巴厘语语言标签。

js
const locales = ["ban", "id-u-co-pinyin", "de-ID"];
const options = { localeMatcher: "lookup" };
console.log(Intl.RelativeTimeFormat.supportedLocalesOf(locales, options));
// ["id-u-co-pinyin", "de-ID"]

规范

规范
ECMAScript® 2026 国际化 API 规范
# sec-Intl.RelativeTimeFormat.supportedLocalesOf

浏览器兼容性

另见