Intl.getCanonicalLocales()

Baseline 已廣泛支援

此功能已非常成熟,可在多種裝置和瀏覽器版本上使用。自 2017 年 10 月以來,它已在各大瀏覽器中可用。

Intl.getCanonicalLocales() 靜態方法返回一個包含規範化區域設定名稱的陣列。重複項將被省略,並且元素將作為結構有效的語言標籤進行驗證。

試一試

console.log(Intl.getCanonicalLocales("EN-US"));
// Expected output: Array ["en-US"]

console.log(Intl.getCanonicalLocales(["EN-US", "Fr"]));
// Expected output: Array ["en-US", "fr"]

try {
  Intl.getCanonicalLocales("EN_US");
} catch (err) {
  console.log(err.toString());
  // Expected output: RangeError: invalid language tag: "EN_US"
}

語法

js
Intl.getCanonicalLocales(locales)

引數

locales

要獲取規範化區域設定名稱的 String 值列表。

返回值

包含規範化區域設定名稱的陣列。

示例

使用 getCanonicalLocales

js
Intl.getCanonicalLocales("EN-US"); // ["en-US"]
Intl.getCanonicalLocales(["EN-US", "Fr"]); // ["en-US", "fr"]

Intl.getCanonicalLocales("EN_US");
// RangeError: invalid language tag: "EN_US"

規範

規範
ECMAScript® 2026 國際化 API 規範
# sec-intl.getcanonicallocales

瀏覽器相容性

另見