Intl.Locale.prototype.region

Intl.Locale 实例的 region 访问器属性返回与此语言环境关联的世界区域(通常是国家/地区)。

¥The region accessor property of Intl.Locale instances returns the region of the world (usually a country) associated with this locale.

描述

¥Description

区域是语言环境的核心属性之一。它允许选择不同国家/地区中相同语言之间的差异。例如,英国和美国使用英语,但这两个国家之间的拼写和其他语言约定存在差异。了解语言环境有助于 JavaScript 程序员确保从世界不同地区查看时,其网站和应用的内容能够正确显示。region 属性的值在构造时设置,可以通过语言环境标识符的 region 子标记(如果存在 script,则为第三部分,否则为第二部分)或通过 Intl.Locale() 构造函数的 region 选项进行设置。如果两者都存在,则后者优先;如果两者都不存在,则该属性的值为 undefined

¥Region is one of the core attributes of a locale. It allows selection for differences between the same language in, say, different countries. For example, English is spoken in the United Kingdom and the United States of America, but there are differences in spelling and other language conventions between those two countries. Knowing the locale's region helps JavaScript programmers make sure that the content from their sites and applications is correctly displayed when viewed from different areas of the world. The region property's value is set at construction time, either through the region subtag (third part if script is present, second part otherwise) of the locale identifier or through the region option of the Intl.Locale() constructor. The latter takes priority if they are both present; and if neither is present, the property has value undefined.

region 的集合访问器是 undefined。你不能直接更改此属性。

¥The set accessor of region is undefined. You cannot change this property directly.

示例

¥Examples

与其他语言环境子标签一样,区域可以通过语言环境字符串或构造函数的配置对象参数添加到 Intl.Locale 对象。

¥Like other locale subtags, the region can be added to the Intl.Locale object via the locale string, or a configuration object argument to the constructor.

通过语言环境字符串添加区域

¥Adding a region via the locale string

如果存在区域,则该区域是有效 Unicode 语言标识符字符串的第三部分(如果存在 script,则为第二部分),并且可以添加到传递到 Intl.Locale() 构造函数中的初始区域标识符字符串中。请注意,区域不是区域设置标识符的必需部分。

¥The region, if present, is the third part (if script is present, second part otherwise) of a valid Unicode language identifier string, and can be added to the initial locale identifier string that is passed into the Intl.Locale() constructor. Note that the region is not a required part of a locale identifier.

js
const locale = new Intl.Locale("en-Latn-US");
console.log(locale.region); // Prints "US"

通过配置对象参数添加区域

¥Adding a region via the configuration object argument

Intl.Locale() 构造函数有一个可选的配置对象参数。将配置对象的 region 属性设置为所需的区域,然后将其传递到构造函数中。

¥The Intl.Locale() constructor has an optional configuration object argument. Set the region property of the configuration object to your desired region, and then pass it into the constructor.

js
const locale = new Intl.Locale("fr-Latn", { region: "FR" });
console.log(locale.region); // Prints "FR"

规范

Specification
ECMAScript Internationalization API Specification
# sec-Intl.Locale.prototype.region

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看