blob: 2714245ace05424c48b13cc90f9062b06e98de2a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
---
title: Intl.Locale
slug: Web/JavaScript/Reference/Global_Objects/Intl/Locale
translation_of: Web/JavaScript/Reference/Global_Objects/Intl/Locale
---
<div>{{JSRef}}</div>
<p><strong><code>Intl.Locale</code></strong> 对象是 Intl 对象的标准内置属性,用于表示 Unicode 区域标识。</p>
<div>{{EmbedInteractiveExample("pages/js/intl-locale.html")}}</div>
<h2 id="描述">描述</h2>
<p><code><strong>Intl.Locale</strong></code> 对象是为了更便捷地处理 Unicode 区域设置。Unicode 使用字符串作为区域识别标识。区域标识符由<em>语言标识符</em>和<em>扩展标记</em>组成。语言标识符是区域(locale)的核心,包含了语言、脚本和地域子标记(region subtags)。有关区域设置的其他信息体现在可选的扩展标记中。扩展标记保存有关区域设置方面的信息,例如日历类型、时钟类型和编号系统类型。</p>
<p>传统上,Intl 接口像 Unicode 一样使用字符串来表示区域设置,这是一个简单而轻量且效果好的解决方案。但是,添加一个Locale 类可以更容易地解析和操作语言、脚本、区域以及扩展标记。</p>
<h2 id="构造函数">构造函数</h2>
<dl>
<dt>{{jsxref("Locale/Locale", "Intl.Locale()")}}</dt>
<dd>实例化一个 <code>Locale</code> 对象。</dd>
</dl>
<h2 id="实例属性">实例属性</h2>
<dl>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/baseName"><code>Intl.Locale.prototype.baseName</code></a></dt>
<dd>Returns basic, core information about the <code>Locale</code> in the form of a substring of the complete data string.</dd>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/calendar"><code>Intl.Locale.prototype.calendar</code></a></dt>
<dd>Returns the part of the <code>Locale</code> that indicates the Locale's calendar era.</dd>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/caseFirst"><code>Intl.Locale.prototype.caseFirst</code></a></dt>
<dd>Returns whether case is taken into account for the locale's collation rules.</dd>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/collation"><code>Intl.Locale.prototype.collation</code></a></dt>
<dd>Returns the collation type for the <code>Locale</code>, which is used to order strings according to the locale's rules.</dd>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/hourCycle"><code>Intl.Locale.prototype.hourCycle</code></a></dt>
<dd>Returns the time keeping format convention used by the locale.</dd>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/language"><code>Intl.Locale.prototype.language</code></a></dt>
<dd>Returns the language associated with the locale.</dd>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/numberingSystem"><code>Intl.Locale.prototype.numberingSystem</code></a></dt>
<dd>Returns the numeral system used by the locale.</dd>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/numeric"><code>Intl.Locale.prototype.numeric</code></a></dt>
<dd>Returns whether the locale has special collation handling for numeric characters.</dd>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/region"><code>Intl.Locale.prototype.region</code></a></dt>
<dd>Returns the region of the world (usually a country) associated with the locale.</dd>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/script"><code>Intl.Locale.prototype.script</code></a></dt>
<dd>Returns the script used for writing the particular language used in the locale.</dd>
</dl>
<h2 id="实例方法">实例方法</h2>
<dl>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/maximize"><code>Intl.Locale.prototype.maximize()</code></a></dt>
<dd>Gets the most likely values for the language, script, and region of the locale based on existing values.</dd>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/minimize"><code>Intl.Locale.prototype.minimize()</code></a></dt>
<dd>Gets the most likely values for the language, script, and region of the locale based on existing values.</dd>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/toString"><code>Intl.Locale.prototype.toString()</code></a></dt>
<dd>Returns the Locale's full locale identifier string.</dd>
</dl>
<h2 id="范例">范例</h2>
<h3 id="基本使用">基本使用</h3>
<p>很简单,就是需要给{{jsxref("Locale/Locale", "Intl.Locale")}} 构造函数传入一个 locale 标识字符串作为参数:</p>
<pre class="brush: js notranslate">let us = new Intl.Locale('zh-Hans-CN');</pre>
<h3 id="使用配置实例化">使用配置实例化</h3>
<p>构造函数支持传入 object 作为配置,object 中可包含多个配置属性。例如,设置 <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/hourCycle">hourCycle</a></code> 属性,用于设置您所需要的小时周期类型:</p>
<pre class="brush: js notranslate">let zh12hour = new Intl.Locale("zh-Hans-CN", {hourCycle: "h12"});
console.log(zh12hour.hourCycle); // Prints "h12"</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td><a href="https://tc39.github.io/proposal-intl-locale">Intl.Locale proposal</a></td>
<td>Stage 3</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<div class="hidden">本页面兼容性表的数据由结构化数据生成。如果你想提供数据,请转到<a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>并且向我们发推送(pull)请求。</div>
<p>{{Compat("javascript.builtins.Intl.Locale")}}</p>
</div>
<h2 id="参阅">参阅</h2>
<ul>
<li> <a href="https://github.com/zbraniecki/Intl.js/tree/intllocale">The Intl.Locale Polyfill</a></li>
</ul>
|