• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# intl Development
2
3The **intl** 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](../reference/apis/js-apis-i18n.md) module provides enhanced I18N capabilities through supplementary interfaces that are not defined in ECMA 402. It works with the Intl module to provide a complete suite of I18N capabilities.
6
7## Setting Locale Information
8
9[Locale](../reference/apis/js-apis-intl.md#locale) provides APIs to maximize or minimize the locale information.
10
11### Available APIs
12
13| Class| API| Description|
14| -------- | -------- | -------- |
15| Locale | constructor()<sup>8+</sup> | Instantiates a **Locale** object.|
16| Locale | constructor(locale:string,options?:LocaleOptions) | Instantiates a **Locale** object based on the locale parameter and options.|
17| Locale | toString():string | Converts locale information into a string.|
18| Locale | maximize():Locale | Maximizes locale information.|
19| Locale | minimize():Locale | Minimizes locale information.|
20
21### How to Develop
22
231. Import the **intl** module.
24
25   Importing an incorrect bundle can lead to unexpected API behavior.
26
27   ```js
28   import Intl from '@ohos.intl';
29   ```
30
312. Instantiates a **Locale** object.
32
33   Create a **Locale** object using the **Locale** constructor. This API receives a string representing the locale and an optional [attribute](../reference/apis/js-apis-intl.md#localeoptions) list. (Note that **intl** is the name of the imported module.)
34
35   A **Locale** object consists of four parts: language, script, region, and extension, which are separated by using a hyphen (-).
36   -  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.
37   -  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.
38   -  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.
39   -  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 latn digital system and chinese calendar system are used. Extensions can also be passed via the second parameter.
40      | Extended Parameter ID| Description|
41      | -------- | -------- |
42      | ca | Calendar algorithm.|
43      | co | Collation type.|
44      | hc | Hour cycle.|
45      | nu | Numbering system.|
46      | kn | Whether numeric collation is used when sorting or comparing strings.|
47      | kf | Whether capitalization is considered when sorting or comparing strings.|
48
49
50   ```js
51   let locale = "zh-CN";
52   let options = {caseFirst: "false", calendar: "chinese", collation: "pinyin"};
53   let localeObj = new Intl.Locale(locale, options);
54   ```
55
563. Obtain the string representing a **Locale** object.
57
58     Call **toString** to obtain the string representing a **Locale** object, including the language, region, and other options.
59
60   ```js
61   let localeStr = localeObj.toString(); // localeStr = "zh-CN-u-ca-chinese-co-pinyin-kf-false
62   ```
63
644. Maximize locale information.
65
66     Call **maximize** to maximize locale information; that is, supplement the missing script and region information.
67
68   ```js
69   let maximizedLocale = localeObj.maximize();
70   let maximizedLocaleStr = maximizedLocale.toString(); // localeStr = "zh-Hans-CN-u-ca-chinese-co-pinyin-kf-false
71   ```
72
735. Minimize locale information.
74
75     Call **minimize** to minimize locale information; that is, delete the unnecessary script and region information.
76
77   ```js
78   let minimizedLocale = localeObj.minimize();
79   let minimizedLocaleStr = minimizedLocale.toString(); // zh-u-ca-chinese-co-pinyin-kf-false
80   ```
81
82## Formatting the Date and Time
83
84[DateTimeFormat](../reference/apis/js-apis-intl.md#datetimeformat) provides APIs to format the date and time for the specified locale.
85
86### Available APIs
87
88| Class| API| Description|
89| -------- | -------- | -------- |
90| DateTimeFormat | constructor()<sup>8+</sup> | Creates a **DateTimeFormat** object.|
91| DateTimeFormat | constructor(locale:string\|Array&lt;string&gt;,options?:DateTimeOptions) | Creates a **DateTimeFormat** object and sets the locale and other formatting-related attributes.|
92| DateTimeFormat | format(date:Date):string | Calculates the date and time based on the locale and other formatting-related attributes of the **DateTimeFormat** object.|
93| DateTimeFormat | formatRange(startDate:Date,endDate:Date):string | Calculates the period based on the locale and other formatting-related attributes of the **DateTimeFormat** object.|
94| DateTimeFormat | resolvedOptions():DateTimeOptions | Obtains the related attributes of the **DateTimeFormat** object.|
95
96### How to Develop
97
981. Import the **intl** module.
99
100   Importing an incorrect bundle can lead to unexpected API behavior.
101
102   ```js
103   import Intl from '@ohos.intl';
104   ```
105
1062. Instantiate a **DateTimeFormat** object.
107
108   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.
109
110   ```js
111   let dateTimeFormat = new Intl.DateTimeFormat();
112   ```
113
114     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#datetimeoptions9).
115
116   ```js
117   let options = {dateStyle: "full", timeStyle: "full"};
118   let dateTimeFormat = new Intl.DateTimeFormat("zh-CN", options);
119   ```
120
1213. Format the date and time.
122
123     Call **format** to format a **Date** object. A string is returned as the formatting result.
124
125   ```js
126   let options = {dateStyle: "full", timeStyle: "full"};
127   let dateTimeFormat = new Intl.DateTimeFormat("zh-CN", options);
128   let date = new Date(2022, 12, 12, 12, 12, 12);
129   let formatResult = dateTimeFormat.format(date); // formatResult = "January 12, 2023, Thursday, 12:12:12 pm, China Standard Time"
130   ```
131
1324. Format a period.
133
134     Call **formatRange** to format a period. This API requires the input of two **Date** objects, which respectively indicate the start date and end date of a period. A string is returned as the formatting result.
135
136   ```js
137   let startDate = new Date(2021, 11, 17, 3, 24, 0);
138   let endDate = new Date(2021, 11, 18, 3, 24, 0);
139   let datefmt = new Intl.DateTimeFormat("en-GB");
140   let formatRangeResult = datefmt.formatRange(startDate, endDate); // formatRangeResult = "17/12/2021-18/12/2021"
141   ```
142
1435. Access the attributes of the **DateTimeFormat** object.
144
145     Call **resolvedOptions** to obtain an object that contains all related attributes and values of the **DateTimeFormat** object.
146
147   ```js
148   let options = {dateStyle: "full", timeStyle: "full"};
149   let dateTimeFormat = new Intl.DateTimeFormat("zh-CN", options);
150   let resolvedOptions = dateTimeFormat.resolvedOptions(); // resolvedOptions = {"locale": "zh-CN", "calendar": "gregorian", "dateStyle":"full", "timeStyle":"full", "timeZone": "CST"}
151   ```
152
153## Formatting Numbers
154
155[NumberFormat](../reference/apis/js-apis-intl.md#numberformat) provides APIs to implement the number formatting specific to a locale.
156
157### Available APIs
158
159| Class| API| Description|
160| -------- | -------- | -------- |
161| NumberFormat | constructor()<SUP>8+</SUP> | Creates a **NumberFormat** object for the specified locale.|
162| NumberFormat | constructor(locale:string\|Array&lt;string&gt;,options?:NumberOptions) | Creates a **NumberFormat** object and sets the locale and other formatting-related attributes.|
163| NumberFormat | format(number:number):string | Calculates the number based on the locale and other formatting-related attributes of the **NumberFormat** object.|
164| NumberFormat | resolvedOptions():NumberOptions | Obtains the attributes of the **NumberFormat** object.|
165
166### How to Develop
167
1681. Import the **intl** module.
169
170   Importing an incorrect bundle can lead to unexpected API behavior.
171
172   ```js
173   import Intl from '@ohos.intl';
174   ```
175
1762. Instantiate a **NumberFormat** object.
177
178   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 (**intl** is the name of the imported module).
179
180   ```js
181   let numberFormat = new Intl.NumberFormat();
182   ```
183
184     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#numberoptions9).
185
186   ```js
187   let options = {compactDisplay: "short", notation: "compact"};
188   let numberFormat = new Intl.NumberFormat("zh-CN", options);
189   ```
190
1913. Format a number.
192
193     Call **format** to format a number. A string is returned as the formatting result.
194
195   ```js
196   let options = {compactDisplay: "short", notation: "compact"};
197   let numberFormat = new Intl.NumberFormat("zh-CN", options);
198   let number = 1234.5678;
199   let formatResult = numberFormat.format(number); // formatResult = "1235"
200   ```
201
2024. Access the attributes of the **NumberFormat** object.
203
204     Call **resolvedOptions** to obtain an object that contains all related attributes and values of the **NumberFormat** object.
205
206   ```js
207   let options = {compactDisplay: "short", notation: "compact"};
208   let numberFormat = new Intl.NumberFormat("zh-CN", options);
209   let resolvedOptions = numberFormat.resolvedOptions();  // resolvedOptions = {"locale": "zh-CN", "compactDisplay": "short", "notation": "compact", "numberingSystem": "Latn"}
210   ```
211
212## Sorting Strings
213
214Users in different regions have different requirements for string sorting. [Collator](../reference/apis/js-apis-intl.md#collator8) provides APIs to sort character strings specific to a locale.
215
216### Available APIs
217
218| Class| API| Description|
219| -------- | -------- | -------- |
220| Collator | constructor()<sup>8+</sup> | Creates a **Collator** object.|
221| Collator | constructor(locale:string\|Array&lt;string&gt;,options?:CollatorOptions)<sup>8+</sup> | Creates a **Collator** object and sets the locale and other related attributes.|
222| Collator | 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.|
223| Collator | resolvedOptions():CollatorOptions<sup>8+</sup> | Obtains the attributes of the **Collator** object.|
224
225### How to Develop
226
2271. Import the **intl** module.
228
229   Importing an incorrect bundle can lead to unexpected API behavior.
230
231   ```js
232   import Intl from '@ohos.intl';
233   ```
234
2352. Instantiate a **Collator** object.
236
237   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 (**intl** is the name of the imported module).
238
239   ```js
240   let collator = new Intl.Collator();
241   ```
242
243     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#collatoroptions9).
244     The **sensitivity** parameter is used to specify the levels of differences that will be used for string comparison. The value **base** indicates that only characters are compared, but not the accent and capitalization. For example, 'a' != 'b', 'a' == '', 'a'=='A'. The value **accent** indicates that the accent is considered, but not the capitalization. For example, 'a' != 'b', 'a' == '', 'a'=='A'. The value **case** indicates that the capitalization is considered, but the accent. For example, 'a' != 'b', 'a' == '', 'a'=='A'. The value **variant** indicates that the accent and capitalization are considered. For example, 'a' != 'b', 'a' == '', 'a'=='A'.
245
246   ```js
247   let collator= new Intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort", sensitivity: "case"});
248   ```
249
2503. Compare two strings.
251
252     Call **compare** to compare two input strings. This API 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.
253
254   ```js
255   let collator= new Intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort", sensitivity: "case"});
256   let str1 = "first string";
257   let str2 = "second string";
258   let compareResult = collator.compare(str1, str2); // compareResult = -1
259   str1 = "first";
260   str2 = "First";
261   compareResult = collator.compare(str1, str2); // compareResult = -1
262   ```
263
2644. Access the attributes of the **Collator** object.
265
266     Call **resolvedOptions** to obtain an object that contains all related attributes and values of the **Collator** object.
267
268   ```js
269   let collator= new Intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort"});
270   let options = collator.resolvedOptions(); // options = {"localeMatcher": "best fit", "locale": "zh-CN", "usage": "sort", "sensitivity": "variant", "ignorePunctuation": "false", "numeric": false, "caseFirst": "false", "collation": "default"}
271   ```
272
273## Determining the Singular-Plural Type
274
275According to grammars in certain languages, the singular or plural form of a noun depends on the number prior to the noun. [PluralRules](../reference/apis/js-apis-intl.md#pluralrules8) provides APIs to determine the singular-plural type for a specific locale.
276
277### Available APIs
278
279| Class| API| Description|
280| -------- | -------- | -------- |
281| PluralRules | constructor()<sup>8+</sup> | Creates a **PluralRules** object.|
282| PluralRules | constructor(locale:string\|Array&lt;string&gt;,options?:PluralRulesOptions)<sup>8+</sup> | Creates a **PluralRules** object and sets the locale and other related attributes.|
283| PluralRules | 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.|
284
285
286### How to Develop
287
2881. Import the **intl** module.
289
290   Importing an incorrect bundle can lead to unexpected API behavior.
291
292   ```js
293   import Intl from '@ohos.intl';
294   ```
295
2962. Instantiate a **PluralRules** object.
297
298   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 (**intl** is the name of the imported module).
299
300   ```js
301   let pluralRules = new Intl.PluralRules();
302   ```
303
304     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#pluralrulesoptions9).
305
306   ```js
307   let pluralRules = new Intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"});
308   ```
309
3103. Determine the singular-plural type.
311
312     Call **select** to determine the singular-plural type for an input number. This API returns a string as the category of the input number, which can be any of the following: **zero**, **one**, **two**, **few**, **many**, and **other**.
313
314   ```js
315   let pluralRules = new Intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"});
316   let number = 1234.5678;
317   let categoryResult = pluralRules.select(number); // categoryResult = "other"
318   ```
319
320## Formatting the Relative Time
321
322[RelativeTimeFormat](../reference/apis/js-apis-intl.md#relativetimeformat8) provides APIs to format the relative time for a specific locale.
323
324### Available APIs
325
326| Class| API| Description|
327| -------- | -------- | -------- |
328| RelativeTimeFormat | constructor()<sup>8+</sup> | Creates a **RelativeTimeFormat** object.|
329| RelativeTimeFormat | 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.|
330| RelativeTimeFormat | 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.|
331| RelativeTimeFormat | formatToParts(value:number,unit:string):Array&lt;object&gt;<sup>8+</sup> | Obtains each part of the relative time format based on the locale and other formatting-related attributes of the **RelativeTimeFormat** object.|
332| RelativeTimeFormat | resolvedOptions():RelativeTimeFormatResolvedOptions<sup>8+</sup> | Obtains the attributes of the **RelativeTimeFormat** object.|
333
334### How to Develop
335
3361. Import the **intl** module.
337
338   Importing an incorrect bundle can lead to unexpected API behavior.
339
340   ```js
341   import Intl from '@ohos.intl';
342   ```
343
3442. Instantiate a **RelativeTimeFormat** object.
345
346   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 (**intl** is the name of the imported module).
347
348   ```js
349   let relativeTimeFormat = new Intl.RelativeTimeFormat();
350   ```
351
352     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#relativetimeformatinputoptions9).
353
354   ```js
355   let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"});
356   ```
357
3583. Format the relative time.
359
360     Call **format** to format the relative time. This API receives a numeric value representing the time length and a string-form unit, like **year**, **quarter**, **month**, **week**, **day**, **hour**, **minute**, and **second**. A string is returned as the formatting result.
361
362   ```js
363   let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"});
364   let number = 2;
365   let unit = "year";
366   let formatResult = relativeTimeFormat.format(number, unit); // 2 years later
367   ```
368
3694. Obtain each part of the relative time format.
370
371     On obtaining each part of the relative time format, customize the relative time formatting result.
372
373   ```js
374   let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"});
375   let number = 2;
376   let unit = "year";
377   let formatPartsResult = relativeTimeFormat.formatToParts(number, unit); // formatPartsResult = [{"type": "integer", "value": "2", "unit": "year"}, {"type":"literal", "value": "years later"}]
378   ```
379
3805. Access the attributes of the **RelativeTimeFormat** object.
381
382     Call **resolvedOptions** to obtain an object that contains all related attributes and values of the **RelativeTimeFormat** object. For a full list of attributes, see [RelativeTimeFormatResolvedOptions](../reference/apis/js-apis-intl.md#relativetimeformatresolvedoptions8).
383
384   ```js
385   let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"});
386   let options = relativeTimeFormat.resolvedOptions(); // options = {"locale": "zh-CN", "style": "long", "numeric": "always", "numberingSystem": "latn"}
387   ```