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 |
瀏覽器相容性
載入中…