Intl.Locale.prototype.language

Intl.Locale 实例的 language 访问器属性返回与该语言环境关联的语言。

¥The language accessor property of Intl.Locale instances returns the language associated with this locale.

描述

¥Description

语言是语言环境的核心属性之一。Unicode 规范将语言环境的语言标识符视为语言和区域(以区分方言和变体,例如英式英语与美式英语)。Intl.Localelanguage 属性严格返回区域设置的语言子标签。language 属性的值在构造时设置,可以通过语言环境标识符的 language 子标签(第一部分)或通过 Intl.Locale() 构造函数的 language 选项设置。如果两者都存在,后者优先。

¥Language is one of the core attributes of a locale. The Unicode specification treats the language identifier of a locale as the language and the region together (to make a distinction between dialects and variations, e.g. British English vs. American English). The language property of a Intl.Locale returns strictly the locale's language subtag. The language property's value is set at construction time, either through the language subtag (first part) of the locale identifier or through the language option of the Intl.Locale() constructor. The latter takes priority if they are both present.

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

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

示例

¥Examples

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

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

通过语言环境字符串设置语言

¥Setting the language via the locale string

为了成为有效的 Unicode 区域设置标识符,字符串必须以语言子标签开头。Intl.Locale() 构造函数的主要参数必须是有效的 Unicode 区域设置标识符,因此每当使用构造函数时,都必须向其传递带有语言子标签的标识符。

¥In order to be a valid Unicode locale identifier, a string must start with the language subtag. The main argument to the Intl.Locale() constructor must be a valid Unicode locale identifier, so whenever the constructor is used, it must be passed an identifier with a language subtag.

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

通过配置对象参数覆盖语言

¥Overriding language via the configuration object argument

虽然必须指定语言子标签,但 Intl.Locale() 构造函数有一个可选的配置对象参数,它可以覆盖语言子标签。

¥While the language subtag must be specified, the Intl.Locale() constructor has an optional configuration object argument, which can override the language subtag.

js
const locale = new Intl.Locale("en-Latn-US", { language: "es" });
console.log(locale.language); // Prints "es"

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also