Intl.Locale.prototype.toString()

Baseline 已廣泛支援

此功能已成熟,並可在許多裝置和瀏覽器版本上使用。自 2020 年 9 月起,所有瀏覽器均已提供此功能。

toString() 方法用於 Intl.Locale 例項,它會返回此 Locale 的完整 locale 識別符號字串

試一試

const french = new Intl.Locale("fr-Latn-FR", {
  calendar: "gregory",
  hourCycle: "h12",
});
const korean = new Intl.Locale("ko-Kore-KR", {
  numeric: true,
  caseFirst: "upper",
});

console.log(french.toString());
// Expected output: "fr-Latn-FR-u-ca-gregory-hc-h12"

console.log(korean.toString());
// Expected output: "ko-Kore-KR-u-kf-upper-kn"

語法

js
toString()

引數

無。

返回值

locale的 Unicode locale 識別符號字串。

描述

Locale 物件是 Unicode locale 識別符號概念在 JavaScript 中的表示。關於特定 locale(語言、指令碼、日曆型別等)的資訊可以編碼在 locale 識別符號字串中。為了方便處理這些 locale 識別符號,JavaScript 中引入了 Locale 物件。在 Locale 物件上呼叫 toString 方法會返回該特定 Locale 的識別符號字串。toString 方法允許將 Locale 例項作為引數傳遞給現有的 Intl 建構函式,進行 JSON 序列化,或在任何其他需要精確字串表示的上下文中。

示例

使用 toString

js
const myLocale = new Intl.Locale("fr-Latn-FR", {
  hourCycle: "h12",
  calendar: "gregory",
});
console.log(myLocale.baseName); // Prints "fr-Latn-FR"
console.log(myLocale.toString()); // Prints "fr-Latn-FR-u-ca-gregory-hc-h12"

規範

規範
ECMAScript® 2026 國際化 API 規範
# sec-Intl.Locale.prototype.toString

瀏覽器相容性

另見