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

View in English Always switch to English

Intl.getCanonicalLocales()

基线 广泛可用

自 2017年10月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

Intl.getCanonicalLocales() 静态方法返回一个包含规范的区域设置名称的数组。重复的元素将会被去除,每一个元素都会被验证为格式有效的语言标签。

尝试一下

console.log(Intl.getCanonicalLocales("EN-US"));
// 期望的输出:Array ["en-US"]

console.log(Intl.getCanonicalLocales(["EN-US", "Fr"]));
// 期望的输出:Array ["en-US", "fr"]

try {
  Intl.getCanonicalLocales("EN_US");
} catch (err) {
  console.log(err.toString());
  // 期望的输出:RangeError: invalid language tag: "EN_US"
}

语法

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: invalid language tag: "EN_US"

规范

规范
ECMAScript® 2027 Internationalization API Specification
# sec-intl.getcanonicallocales

浏览器兼容性

参见