試一試
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 語言環境資料標記語言規範中的 潛在子標記