此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
Intl.Locale
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2020年9月.
* Some parts of this feature may have varying levels of support.
Intl.Locale 对象是 Intl 对象的标准内置属性,用于表示 Unicode 区域标识。
In this article
尝试一下
const korean = new Intl.Locale("ko", { script: "Kore", region: "KR", hourCycle: "h23", calendar: "gregory",});const japanese = new Intl.Locale("ja-Jpan-JP-u-ca-japanese-hc-h12");console.log(korean.baseName, japanese.baseName);// Expected output: "ko-Kore-KR" "ja-Jpan-JP"console.log(korean.hourCycle, japanese.hourCycle);// Expected output: "h23" "h12"描述
Intl.Locale 对象是为了更便捷地处理 Unicode 区域设置。Unicode 使用字符串作为区域识别标识。区域标识符由语言标识符和扩展标记组成。语言标识符是区域 (locale) 的核心,包含了语言、脚本和地域子标记 (region subtags)。有关区域设置的其他信息体现在可选的扩展标记中。扩展标记保存有关区域设置方面的信息,例如日历类型、时钟类型和编号系统类型。
传统上,Intl 接口像 Unicode 一样使用字符串来表示区域设置,这是一个简单而轻量且效果好的解决方案。但是,添加一个 Locale 类可以更容易地解析和操作语言、脚本、区域以及扩展标记。
构造函数
Intl.Locale()实例化一个
Locale对象。
实例属性
Intl.Locale.prototype.baseNameReturns basic, core information about the
Localein the form of a substring of the complete data string.Intl.Locale.prototype.calendarReturns the part of the
Localethat indicates the Locale's calendar era.Intl.Locale.prototype.caseFirstReturns whether case is taken into account for the locale's collation rules.
Intl.Locale.prototype.collationReturns the collation type for the
Locale, which is used to order strings according to the locale's rules.Intl.Locale.prototype.hourCycleReturns the time keeping format convention used by the locale.
Intl.Locale.prototype.languageReturns the language associated with the locale.
Intl.Locale.prototype.numberingSystemReturns the numeral system used by the locale.
Intl.Locale.prototype.numericReturns whether the locale has special collation handling for numeric characters.
Intl.Locale.prototype.regionReturns the region of the world (usually a country) associated with the locale.
Intl.Locale.prototype.scriptReturns the script used for writing the particular language used in the locale.
实例方法
Intl.Locale.prototype.maximize()Gets the most likely values for the language, script, and region of the locale based on existing values.
Intl.Locale.prototype.minimize()Gets the most likely values for the language, script, and region of the locale based on existing values.
Intl.Locale.prototype.toString()Returns the Locale's full locale identifier string.
范例
>基本使用
很简单,就是需要给Intl.Locale 构造函数传入一个 locale 标识字符串作为参数:
let us = new Intl.Locale("zh-Hans-CN");使用配置实例化
构造函数支持传入 object 作为配置,object 中可包含多个配置属性。例如,设置hourCycle 属性,用于设置你所需要的小时周期类型:
let zh12hour = new Intl.Locale("zh-Hans-CN", { hourCycle: "h12" });console.log(zh12hour.hourCycle); // Prints "h12"规范
| Specification |
|---|
| ECMAScript® 2026 Internationalization API Specification> # locale-objects> |