试一试
const english = new Intl.Locale("en");
const korean = new Intl.Locale("ko");
const arabic = new Intl.Locale("ar");
console.log(english.maximize().baseName);
// Expected output: "en-Latn-US"
console.log(korean.maximize().baseName);
// Expected output: "ko-Kore-KR"
console.log(arabic.maximize().baseName);
// Expected output: "ar-Arab-EG"
语法
js
maximize()
参数
无。
返回值
返回一个 Intl.Locale 实例,其 baseName 属性返回的是在locale.baseName 上执行 添加潜在子标记 (Add Likely Subtags) 算法的结果。
描述
有时,根据不完整的语言 ID 识别最可能的语言环境语言标识符子标记会很方便。添加潜在子标记算法为我们提供了此功能。例如,给定语言 ID "en",该算法将返回 "en-Latn-US",因为英语只能用拉丁字母书写,并且最有可能在美国使用,因为美国是世界上最大的英语国家。此功能通过 maximize() 方法提供给 JavaScript 程序员。maximize() 仅影响构成 语言标识符 的主要子标记:语言、脚本和地区子标记。语言标识符中 "-u" 后面的其他子标记称为扩展子标记,不受 maximize() 方法的影响。这些子标记的示例包括 hourCycle、calendar 和 numeric。
示例
使用 maximize
js
const myLocale = new Intl.Locale("fr", {
hourCycle: "h12",
calendar: "gregory",
});
console.log(myLocale.baseName); // Prints "fr"
console.log(myLocale.toString()); // Prints "fr-u-ca-gregory-hc-h12"
const myLocMaximized = myLocale.maximize();
// Prints "fr-Latn-FR". The "Latn" and "FR" tags are added,
// since French is only written in the Latin script and is most likely to be spoken in France.
console.log(myLocMaximized.baseName);
// Prints "fr-Latn-FR-u-ca-gregory-hc-h12".
// Note that the extension tags (after "-u") remain unchanged.
console.log(myLocMaximized.toString());
规范
| 规范 |
|---|
| ECMAScript® 2026 国际化 API 规范 # sec-Intl.Locale.prototype.maximize |
浏览器兼容性
加载中…
另见
Intl.LocalebaseName- Unicode 语言环境数据标记语言规范中的 潜在子标记