此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Intl.getCanonicalLocales()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨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 (Firefox/Safari): RangeError: invalid language tag: "EN_US"
  // Expected output (Chrome): RangeError: Incorrect locale information provided
}

语法

js
Intl.getCanonicalLocales(locales)

参数

locales

想要规范化的字符串数组。

返回值

一个包含规范区域语言代码的数组。

示例

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

Intl.getCanonicalLocales("EN_US");
// RangeError:'EN_US' is not a structurally valid language tag

规范

Specification
ECMAScript® 2026 Internationalization API Specification
# sec-intl.getcanonicallocales

浏览器兼容性

参见