• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Internationalization Development (Intl)
2
3This module provides basic I18N capabilities, such as time and date formatting, number formatting, and string sorting, through the standard I18N APIs defined in ECMA 402. For more details about APIs and their usage, see [Intl](../reference/apis/js-apis-intl.md).
4
5The [I18N](i18n-guidelines.md) module provides enhanced I18N capabilities through supplementary APIs that are not defined in ECMA 402. It works with the Intl module to provide a complete suite of I18N capabilities.
6
7> **NOTE**
8>
9> In the code snippets in this document, **intl** refers to the name of the imported module.
10
11## Setting Locale Information
12
13Use [Locale](../reference/apis/js-apis-intl.md#locale) APIs to maximize or minimize locale information.
14
15
16### Available APIs
17
18| Module | API | Description |
19| -------- | -------- | -------- |
20| ohos.intl | constructor()<sup>8+</sup> | Instantiates a **Locale** object. |
21| ohos.intl | constructor(locale?: string, options?: options) | Instantiates a **Locale** object based on the locale parameter and options. |
22| ohos.intl | toString(): string | Converts locale information into a string. |
23| ohos.intl | maximize(): Locale | Maximizes locale information. |
24| ohos.intl | minimize(): Locale | Minimizes locale information. |
25
26
27### How to Develop
28
291. Instantiate a **Locale** object.
30
31   Create a **Locale** object by using the **Locale** constructor. This method receives a string representing the locale and an optional [Attributes](../reference/apis/js-apis-intl.md#localeoptions) list.
32
33   A **Locale** object consists of four parts: language, script, region, and extension, which are separated by using a hyphen (-).
34   -  Language: mandatory. It is represented by a two-letter or three-letter code as defined in ISO-639. For example, **en** indicates English and **zh** indicates Chinese.
35   -  Script: optional. It is represented by a four-letter code as defined in ISO-15924. The first letter is in uppercase, and the remaining three letters are in lowercase. For example, **Hant** represents the traditional Chinese, and **Hans** represents the simplified Chinese.
36   -  Country or region: optional. It is represented by two-letter code as defined in ISO-3166. Both letters are in uppercase. For example, **CN** represents China, and **US** represents the United States.
37   -  Extensions: optional. Each extension consists of two parts, key and value. Currently, the extensions listed in the following table are supported (see BCP 47 Extensions). Extensions can be in any sequence and are written in the format of **-key-value**. They are appended to the language, script, and region by using **-u**. For example, **zh-u-nu-latn-ca-chinese** indicates that the Latin numbering system and Chinese calendar system are used. Extensions can also be passed via the second parameter.
38      | Extended Parameter ID| Description|
39      | -------- | -------- |
40      | ca | Calendar algorithm.|
41      | co | Collation type.|
42      | hc | Hour cycle.|
43      | nu | Numbering system.|
44      | kn | Whether numeric collation is used when sorting or comparing strings.|
45      | kf | Whether upper case or lower case is considered when sorting or comparing strings.|
46
47
48   ```js
49   var locale = "zh-CN";
50   var options = {caseFirst: false, calendar: "chinese", collation: "pinyin"};
51   var localeObj = new intl.Locale(locale, options);
52   ```
53
542. Obtain the string representing a **Locale** object.
55
56   Call the **toString** method to obtain the string representing a **Locale** object, which includes the language, region, and other options.
57
58   ```js
59   var localeStr = localeObj.toString();
60   ```
61
623. Maximize locale information.
63
64   Call the **maximize** method to maximize locale information; that is, supplement the missing script and region information.
65
66   ```js
67   var maximizedLocale = localeObj.maximize();
68   ```
69
704. Minimize locale information.
71
72   Call the **minimize** method to minimize locale information; that is, delete the unnecessary script and region information.
73
74   ```js
75   var minimizedLocale = localeObj.minimize();
76   ```
77
78
79## Formatting the Date and Time
80
81Use [DateTimeFormat](../reference/apis/js-apis-intl.md#datetimeformat) APIs to format the date and time for a specific locale.
82
83
84### Available APIs
85
86| Module | API | Description |
87| -------- | -------- | -------- |
88| ohos.intl | constructor()<sup>8+</sup> | Creates a **DateTimeFormat** object. |
89| ohos.intl | constructor(locale: string \| Array&lt;string&gt;, options?: DateTimeOptions) | Creates a **DateTimeFormat** object and sets the locale and other formatting-related attributes. |
90| ohos.intl | format(date: Date): string | Calculates the date and time based on the locale and other formatting-related attributes of the **DateTimeFormat** object. |
91| ohos.intl | formatRange(startDate: Date, endDate: Date): string | Calculates the period based on the locale and other formatting-related attributes of the **DateTimeFormat** object. |
92| ohos.intl | resolvedOptions(): DateTimeOptions | Obtains the related attributes of the **DateTimeFormat** object. |
93
94
95### How to Develop
96
971. Instantiate a **DateTimeFormat** object.
98
99   Use the default constructor of **DateTimeFormat** to obtain the system default locale by accessing the system language and region settings, and set it as the locale in the **DateTimeFormat** object.
100
101
102   ```js
103   var dateTimeFormat = new intl.DateTimeFormat();
104   ```
105
106   Alternatively, use your own locale and formatting parameters to create a **DateTimeFormat** object. Formatting parameters are optional. For a full list of formatting parameters, see [DateTimeOptions](../reference/apis/js-apis-intl.md#datetimeoptions).
107
108   ```js
109   var options = {dateStyle: "full", timeStyle: "full"};
110   var dateTimeFormat = new intl.DateTimeFormat("zh-CN", options);
111   ```
112
1132. Format the date and time.
114
115   Call the **format** method to format the date and time in the **DateTimeFormat** object. This method returns a string representing the formatting result.
116
117   ```js
118   var date = new Date();
119   var formatResult = dateTimeFormat.format(date);
120   ```
121
1223. Format a period.
123
124   Call the **formatRange** method to format the period in the **DateTimeFormat** object. This method requires input of two **Date** objects, which respectively indicate the start date and end date of a period. This method returns a string representing the formatting result.
125
126   ```js
127   var startDate = new Date(2021, 11, 17, 3, 24, 0);
128   var endDate = new Date(2021, 11, 18, 3, 24, 0);
129   var datefmt = new Intl.DateTimeFormat("en-GB");
130   datefmt.formatRange(startDate, endDate);
131   ```
132
1334. Obtain attributes of the **DateTimeFormat** object.
134
135   Call the **resolvedOptions** method to obtain attributes of the **DateTimeFormat** object. This method will return an array that contains all attributes and values of the object.
136
137   ```js
138   var options = dateTimeFormat.resolvedOptions();
139   ```
140
141
142## Formatting Numbers
143
144Use [NumberFormat](../reference/apis/js-apis-intl.md#numberformat) APIs to format numbers for a specific locale.
145
146
147### Available APIs
148
149| Module | API | Description |
150| -------- | -------- | -------- |
151| ohos.intl | constructor()<sup>8+</sup> | Creates a **NumberFormat** object. |
152| ohos.intl | constructor(locale: string \| Array&lt;string&gt;, options?: NumberOptions) | Creates a **NumberFormat** object and sets the locale and other formatting-related attributes. |
153| ohos.intl | format(number: number): string | Calculates the number based on the locale and other formatting-related attributes of the **NumberFormat** object. |
154| ohos.intl | resolvedOptions(): NumberOptions | Obtains attributes of the **NumberFormat** object. |
155
156
157### How to Develop
158
1591. Instantiate a **NumberFormat** object.
160
161   Use the default constructor of **NumberFormat** to obtain the system default locale by accessing the system language and region settings, and set it as the locale in the **NumberFormat** object.
162
163
164   ```js
165   var numberFormat = new intl.NumberFormat();
166   ```
167
168   Alternatively, use your own locale and formatting parameters to create a **NumberFormat** object. Formatting parameters are optional. For a full list of formatting parameters, see [NumberOptions](../reference/apis/js-apis-intl.md#numberoptions).
169
170   ```js
171   var options = {compactDisplay: "short", notation: "compact"};
172   var numberFormat = new intl.NumberFormat("zh-CN", options);
173   ```
174
1752. Format a number.
176
177   Call the **format** method to format a number. A string is returned as the formatting result.
178
179   ```js
180   var number = 1234.5678
181   var formatResult = numberFormat.format(number);
182   ```
183
1843. Obtain attributes of the **NumberFormat** object.
185
186   Call the **resolvedOptions** method to obtain attributes of the **NumberFormat** object. This method will return an array that contains all attributes and values of the object.
187
188   ```js
189   var options = numberFormat.resolvedOptions();
190   ```
191
192
193## Sorting Strings
194
195Use [Collator](../reference/apis/js-apis-intl.md#collator8) APIs to sort strings based on a specific locale. Users in different regions have different preferences for string sorting.
196
197
198### Available APIs
199
200| Module | API | Description |
201| -------- | -------- | -------- |
202| ohos.intl | constructor()<sup>8+</sup> | Creates a **Collator** object. |
203| ohos.intl | constructor(locale: string \| Array&lt;string&gt;, options?: CollatorOptions)<sup>8+</sup> | Creates a **Collator** object and sets the locale and other related attributes. |
204| ohos.intl | compare(first: string, second: string): number<sup>8+</sup> | Calculates the comparison result of two strings based on the locale and other attributes of the **Collator** object. |
205| ohos.intl | resolvedOptions(): CollatorOptions<sup>8+</sup> | Obtains attributes of the **Collator** object. |
206
207
208### How to Develop
209
2101. Instantiate a **Collator** object.
211
212   Use the default constructor of **Collator** to obtain the system default locale by accessing the system language and region settings, and set it as the locale in the **Collator** object.
213
214
215   ```js
216   var collator = new intl.Collator();
217   ```
218
219   Alternatively, use your own locale and formatting parameters to create a **Collator** object. For a full list of parameters, see [CollatorOptions](../reference/apis/js-apis-intl.md#collatoroptions8).
220
221   ```js
222   var collator= new intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort"});
223   ```
224
2252. Compare two strings.
226
227   Call the **compare** method to compare two input strings. This method returns a value as the comparison result. The return value **-1** indicates that the first string is shorter than the second string, the return value **1** indicates that the first string is longer than the second string, and the return value **0** indicates that the two strings are of equal lengths. This allows you to sort character strings based on the comparison result.
228
229   ```js
230   var str1 = "first string";
231   var str2 = "second string";
232   var compareResult = collator.compare(str1, str2);
233   ```
234
2353. Obtain attributes of the **Collator** object.
236
237   Call the **resolvedOptions** method to obtain attributes of the **Collator** object. This method will return an array that contains all attributes and values of the object.
238
239   ```js
240   var options = collator.resolvedOptions();
241   ```
242
243
244## Determining the Singular-Plural Type
245
246Use [PluralRules](../reference/apis/js-apis-intl.md#pluralrules8) APIs to determine the singular-plural type for a specific locale. According to the grammar of certain languages, the singular or plural form of a noun depends on its preceding number.
247
248
249### Available APIs
250
251| Module | API | Description |
252| -------- | -------- | -------- |
253| ohos.intl | constructor()<sup>8+</sup> | Creates a **PluralRules** object. |
254| ohos.intl | constructor(locale: string \| Array&lt;string&gt;, options?: PluralRulesOptions)<sup>8+</sup> | Creates a **PluralRules** object and sets the locale and other related attributes. |
255| ohos.intl | select(n: number): string<sup>8+</sup> | Determines the singular-plural type based on the locale and other formatting-related attributes of the **PluralRules** object. |
256
257
258### How to Develop
259
2601. Instantiate a **PluralRules** object.
261
262   Use the default constructor of **PluralRules** to obtain the system default locale by accessing the system language and region settings, and set it as the locale in the **PluralRules** object.
263
264
265   ```js
266   var pluralRules = new intl.PluralRules();
267   ```
268
269   Alternatively, use your own locale and formatting parameters to create a **PluralRules** object. For a full list of parameters, see [PluralRulesOptions](../reference/apis/js-apis-intl.md#pluralrulesoptions8).
270
271   ```js
272   var pluralRules = new intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"});
273   ```
274
2752. Determine the singular-plural type.
276
277   Call the **select** method to determine the singular-plural type of an input number. This method will return a string representing the singular-plural type, which can be any of the following: **zero**, **one**, **two**, **few**, **many**, and **other**.
278
279   ```js
280   var number = 1234.5678
281   var categoryResult = plurals.select(number);
282   ```
283
284
285## Formatting the Relative Time
286
287Use [RelativeTimeFormat](../reference/apis/js-apis-intl.md#relativetimeformat8) APIs to format the relative time for a specific locale.
288
289
290### Available APIs
291
292| Module | API | Description |
293| -------- | -------- | -------- |
294| ohos.intl | constructor()<sup>8+</sup> | Creates a **RelativeTimeFormat** object. |
295| ohos.intl | constructor(locale: string \| Array&lt;string&gt;, options?: RelativeTimeFormatInputOptions)<sup>8+</sup> | Creates a **RelativeTimeFormat** object and sets the locale and other formatting-related attributes. |
296| ohos.intl | format(value: number, unit: string): string<sup>8+</sup> | Calculates the relative time format based on the locale and other formatting-related attributes of the **RelativeTimeFormat** object. |
297| ohos.intl | formatToParts(value: number, unit: string): Array&lt;object&gt;<sup>8+</sup> | Returns each part of the relative time format based on the locale and other formatting-related attributes of the **RelativeTimeFormat** object. |
298| ohos.intl | resolvedOptions(): RelativeTimeFormatResolvedOptions<sup>8+</sup> | Obtains attributes of the **RelativeTimeFormat** object. |
299
300
301### How to Develop
302
3031. Instantiate a **RelativeTimeFormat** object.
304
305   Use the default constructor of **RelativeTimeFormat** to obtain the system default locale by accessing the system language and region settings, and set it as the locale in the **RelativeTimeFormat** object.
306
307
308   ```js
309   var relativeTimeFormat = new intl.RelativeTimeFormat();
310   ```
311
312   Alternatively, use your own locale and formatting parameters to create a **RelativeTimeFormat** object. Formatting parameters are optional. For a full list of formatting parameters, see [RelativeTimeFormatInputOptions](../reference/apis/js-apis-intl.md#relativetimeformatinputoptions8).
313
314   ```js
315   var relativeTimeFormat = new intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"});
316   ```
317
3182. Format the relative time.
319
320   Call the **format** method to format the relative time. This method receives a numeric value representing the time length and a string-form unit, like **year**, **quarter**, **month**, **week**, **day**, **hour**, **minute**, and **second**. This method returns a string representing the formatting result.
321
322   ```js
323   var number = 2;
324   var unit = "year"
325   var formatResult = relativeTimeFormat.format(number, unit);
326   ```
327
3283. Obtain each part of the relative time format.
329
330   Upon obtaining each part of the relative time format, customize the relative time formatting result.
331
332   ```js
333   var number = 2;
334   var unit = "year"
335   var formatResult = relativeTimeFormat.formatToParts(number, unit);
336   ```
337
3384. Obtain attributes of the **RelativeTimeFormat** object.
339
340   Call the **resolvedOptions** method to obtain attributes of the **RelativeTimeFormat** object. This method will return an array that contains all attributes and values of the object. For a full list of attributes, see [RelativeTimeFormatResolvedOptions](../reference/apis/js-apis-intl.md#relativetimeformatresolvedoptions8).
341
342   ```js
343   var options = numberFormat.resolvedOptions();
344   ```
345
346## Samples
347
348The following sample is provided to help you better understand how to develop internationalization capabilities:
349
350-[`International`: Internationalization (JS) (API8)](https://gitee.com/openharmony/applications_app_samples/tree/master/UI/International)
351
352-[`International`: Internationalization (eTS) (API8) (Full SDK)](https://gitee.com/openharmony/applications_app_samples/tree/master/common/International)
353