• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.intl (Internationalization)
2
3 The **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.
4The [i18n](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.
5
6>  **NOTE**
7>  - The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9>  - This module provides basic i18n capabilities, such as time and date formatting, number formatting, and string sorting, through the standard i18n interfaces defined in ECMA 402. For details about the enhanced i18n capabilities, see [i18n](js-apis-i18n.md).
10
11
12## Modules to Import
13
14```js
15import Intl from '@ohos.intl';
16```
17Importing an incorrect bundle can lead to unexpected API behavior.
18
19## Locale
20
21
22### Attributes
23
24**System capability**: SystemCapability.Global.I18n
25
26| Name             | Type     | Readable  | Writable  | Description                                      |
27| --------------- | ------- | ---- | ---- | ---------------------------------------- |
28| language        | string  | Yes   | No   | Language associated with the locale, for example, **zh**.                   |
29| script          | string  | Yes   | No   | Script type of the language, for example, **Hans**.                         |
30| region          | string  | Yes   | No   | Region associated with the locale, for example, **CN**.                        |
31| baseName        | string  | Yes   | No   | Basic key information about the locale, which consists of the language, script, and region, for example, **zh-Hans-CN**. |
32| caseFirst       | string  | Yes   | No   | Whether case is taken into account for the locale's collation rules. The value can be **upper**, **lower**, or **false**.|
33| calendar        | string  | Yes   | No   | Calendar for the locale. The value can be any of the following: **buddhist**, **chinese**, **coptic**, **dangi**, **ethioaa**, **ethiopic**, **gregory**, **hebrew**, **indian**, **islamic**, **islamic-umalqura**, **islamic-tbla**, **islamic-civil**, **islamic-rgsa**, **iso8601**, **japanese**, **persian**, **roc**, or **islamicc**.|
34| collation       | string  | Yes   | No   | Rule for sorting regions. The value can be any of the following: **big5han**, **compat**, **dict**, **direct**, **ducet**, **eor**, **gb2312**, **phonebk**, **phonetic**, **pinyin**, **reformed**, **searchjl**, **stroke**, **trad**, **unihan**, **zhuyin**.|
35| hourCycle       | string  | Yes   | No   | Time system for the locale. The value can be any of the following: **h12**, **h23**, **h11**, or **h24**.|
36| numberingSystem | string  | Yes   | No   | Numbering system for the locale. The value can be any of the following: **adlm**, **ahom**, **arab**, **arabext**, **bali**, **beng**, **bhks**, **brah**, **cakm**, **cham**, **deva**, **diak**, **fullwide**, **gong**, **gonm**, **gujr**, **guru**, **hanidec**, **hmng**, **hmnp**, **java**, **kali**, **khmr**, **knda**, **lana**, **lanatham**, **laoo**, **latn**, **lepc**, **limb**, **mathbold**, **mathdbl**, **mathmono**, **mathsanb**, **mathsans**, **mlym**, **modi**, **mong**, **mroo**, **mtei**, **mymr**, **mymrshan**, **mymrtlng**, **newa**, **nkoo**, **olck**, **orya**, **osma**, **rohg**, **saur**, **segment**, **shrd**, **sind**, **sinh**, **sora**, **sund**, **takr**, **talu**, **tamldec**, **telu**, **thai**, **tibt**, **tirh**, **vaii**, **wara**, **wcho**.|
37| numeric         | boolean | Yes   | No   | Whether to apply special collation rules for numeric characters.                     |
38
39
40### constructor<sup>8+</sup>
41
42constructor()
43
44Creates a **Locale** object.
45
46**System capability**: SystemCapability.Global.I18n
47
48**Example**
49  ```js
50  // The default constructor uses the current system locale to create a Locale object.
51  let locale = new Intl.Locale();
52  // Return the current system locale.
53  let localeID = locale.toString();
54  ```
55
56
57### constructor
58
59constructor(locale: string, options?: LocaleOptions)
60
61Creates a **Locale** object.
62
63**System capability**: SystemCapability.Global.I18n
64
65**Parameters**
66
67| Name                 | Type                              | Mandatory  | Description                          |
68| -------------------- | -------------------------------- | ---- | ---------------------------- |
69| locale               | string                           | Yes   | A string containing locale information, including the language, optional script, and region. For details about the international standards and combination modes for the language, script, and country or region, see [intl Development](../../internationalization/intl-guidelines.md#setting-locale-information).|
70| options<sup>9+</sup> | [LocaleOptions](#localeoptions9) | No   | Options for creating the **Locale** object.                |
71
72**Example**
73  ```js
74  // Create a Locale object named zh-CN.
75  let locale = new Intl.Locale("zh-CN");
76  let localeID = locale.toString(); // localeID = "zh-CN"
77  ```
78
79
80### toString
81
82toString(): string
83
84Obtains the string representation of a **Locale** object.
85
86**System capability**: SystemCapability.Global.I18n
87
88**Return value**
89
90| Type    | Description         |
91| ------ | ----------- |
92| string | String representation of the **Locale** object.|
93
94**Example**
95  ```js
96  // Create a Locale object named en-GB.
97  let locale = new Intl.Locale("en-GB");
98  let localeID = locale.toString(); // localeID = "en-GB"
99  ```
100
101
102### maximize
103
104maximize(): Locale
105
106Maximizes information of the **Locale** object. If the script and locale information is missing, add the information.
107
108**System capability**: SystemCapability.Global.I18n
109
110**Return value**
111
112| Type               | Description        |
113| ----------------- | ---------- |
114| [Locale](#locale) | **Locale** object with the maximized information.|
115
116**Example**
117  ```js
118  // Create a Locale object named zh.
119  let locale = new Intl.Locale("zh");
120  // Complete the script and region of the Locale object.
121  let maximizedLocale = locale.maximize();
122  let localeID = maximizedLocale.toString(); // localeID = "zh-Hans-CN"
123
124  // Create a Locale object named en-US.
125  locale = new Intl.Locale("en-US");
126  // Complete the script of the Locale object.
127  maximizedLocale = locale.maximize();
128  localeID = maximizedLocale.toString(); // localeID = "en-Latn-US"
129  ```
130
131
132### minimize
133
134minimize(): Locale
135
136Minimizes information of the **Locale** object. If the script and locale information is present, delete the information.
137
138**System capability**: SystemCapability.Global.I18n
139
140**Return value**
141
142| Type               | Description        |
143| ----------------- | ---------- |
144| [Locale](#locale) | **Locale** object with the minimized information.|
145
146**Example**
147  ```js
148  // Create a Locale object named zh-Hans-CN.
149  let locale = new Intl.Locale("zh-Hans-CN");
150  // Remove the script and region of the Locale object.
151  let minimizedLocale = locale.minimize();
152  let localeID = minimizedLocale.toString(); // localeID = "zh"
153
154  // Create a Locale object named en-US.
155  locale = new Intl.Locale("en-US");
156  // Remove the region of the Locale object.
157  minimizedLocale = locale.minimize();
158  localeID = minimizedLocale.toString(); // localeID = "en"
159  ```
160
161
162## LocaleOptions<sup>9+</sup>
163
164Represents the locale options.
165
166**System capability**: SystemCapability.Global.I18n
167
168| Name             | Type     | Readable  | Writable  | Description                                      |
169| --------------- | ------- | ---- | ---- | ---------------------------------------- |
170| calendar        | string  | Yes   | Yes   | Calendar for the locale. The value can be any of the following: **buddhist**, **chinese**, **coptic**, **dangi**, **ethioaa**, **ethiopic**, **gregory**, **hebrew**, **indian**, **islamic**, **islamic-umalqura**, **islamic-tbla**, **islamic-civil**, **islamic-rgsa**, **iso8601**, **japanese**, **persian**, **roc**, or **islamicc**.|
171| collation       | string  | Yes   | Yes   | Collation rule. The value can be any of the following: **big5han**, **compat**, **dict**, **direct**, **ducet**, **emoji**, **eor**, **gb2312**, **phonebk**, **phonetic**, **pinyin**, **reformed**, **search**, **searchjl**, **standard**, **stroke**, **trad**, **unihan**, **zhuyin**.|
172| hourCycle       | string  | Yes   | Yes   | Time system for the locale. The value can be any of the following: **h11**, **h12**, **h23**, or **h24**.|
173| numberingSystem | string  | Yes   | Yes   | Numbering system for the locale. The value can be any of the following: **adlm**, **ahom**, **arab**, **arabext**, **bali**, **beng**, **bhks**, **brah**, **cakm**, **cham**, **deva**, **diak**, **fullwide**, **gong**, **gonm**, **gujr**, **guru**, **hanidec**, **hmng**, **hmnp**, **java**, **kali**, **khmr**, **knda**, **lana**, **lanatham**, **laoo**, **latn**, **lepc**, **limb**, **mathbold**, **mathdbl**, **mathmono**, **mathsanb**, **mathsans**, **mlym**, **modi**, **mong**, **mroo**, **mtei**, **mymr**, **mymrshan**, **mymrtlng**, **newa**, **nkoo**, **olck**, **orya**, **osma**, **rohg**, **saur**, **segment**, **shrd**, **sind**, **sinh**, **sora**, **sund**, **takr**, **talu**, **tamldec**, **telu**, **thai**, **tibt**, **tirh**, **vaii**, **wara**, **wcho**.|
174| numeric         | boolean | Yes   | Yes   | Whether to use the 12-hour clock.                              |
175| caseFirst       | string  | Yes   | Yes   | Whether upper case or lower case is sorted first. The value can be **upper**, **lower**, or **false**.|
176
177
178## DateTimeFormat
179
180
181### constructor<sup>8+</sup>
182
183constructor()
184
185Creates a **DateTimeOptions** object for the specified locale.
186
187**System capability**: SystemCapability.Global.I18n
188
189**Example**
190  ```js
191  // Use the current system locale to create a DateTimeFormat object.
192  let datefmt= new Intl.DateTimeFormat();
193  ```
194
195
196### constructor
197
198constructor(locale: string | Array&lt;string&gt;, options?: DateTimeOptions)
199
200Creates a **DateTimeOptions** object for the specified locale.
201
202**System capability**: SystemCapability.Global.I18n
203
204**Parameters**
205
206| Name                 | Type                                  | Mandatory  | Description                          |
207| -------------------- | ------------------------------------ | ---- | ---------------------------- |
208| locale               | string \| Array&lt;string&gt;        | Yes   | A string containing locale information, including the language, optional script, and region.|
209| options<sup>9+</sup> | [DateTimeOptions](#datetimeoptions9) | No   | Options for creating a **DateTimeFormat** object.             |
210
211**Example**
212  ```js
213  // Use locale zh-CN to create a DateTimeFormat object. Set dateStyle to full and timeStyle to medium.
214  let datefmt= new Intl.DateTimeFormat("zh-CN", { dateStyle: 'full', timeStyle: 'medium' });
215  ```
216
217
218**Example**
219  ```js
220  // Use the locale list ["ban", "zh"] to create a DateTimeFormat object. Because ban is an invalid locale ID, locale zh is used to create the DateTimeFormat object.
221  let datefmt= new Intl.DateTimeFormat(["ban", "zh"], { dateStyle: 'full', timeStyle: 'medium' });
222  ```
223
224
225### format
226
227format(date: Date): string
228
229Formats the specified date and time.
230
231**System capability**: SystemCapability.Global.I18n
232
233**Parameters**
234
235| Name | Type  | Mandatory  | Description     |
236| ---- | ---- | ---- | ------- |
237| date | Date | Yes   | Date and time to be formatted.|
238
239**Return value**
240
241| Type    | Description          |
242| ------ | ------------ |
243| string | A string containing the formatted date and time.|
244
245**Example**
246  ```js
247  let date = new Date(2021, 11, 17, 3, 24, 0);
248  // Use locale en-GB to create a DateTimeFormat object.
249  let datefmt = new Intl.DateTimeFormat("en-GB");
250  let formattedDate = datefmt.format(date); // formattedDate "17/12/2021"
251
252  // Use locale en-GB to create a DateTimeFormat object. Set dateStyle to full and timeStyle to medium.
253  datefmt = new Intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' });
254  formattedDate = datefmt.format(date); // formattedDate "Friday, 17 December 2021 at 03:24:00"
255  ```
256
257
258### formatRange
259
260formatRange(startDate: Date, endDate: Date): string
261
262Formats the specified date range.
263
264**System capability**: SystemCapability.Global.I18n
265
266**Parameters**
267
268| Name      | Type  | Mandatory  | Description      |
269| --------- | ---- | ---- | -------- |
270| startDate | Date | Yes   | Start date and time to be formatted.|
271| endDate   | Date | Yes   | End date and time to be formatted.|
272
273**Return value**
274
275| Type    | Description            |
276| ------ | -------------- |
277| string | A string containing the formatted date and time range.|
278
279**Example**
280  ```js
281  let startDate = new Date(2021, 11, 17, 3, 24, 0);
282  let endDate = new Date(2021, 11, 18, 3, 24, 0);
283  // Use locale en-GB to create a DateTimeFormat object.
284  let datefmt = new Intl.DateTimeFormat("en-GB");
285  let formattedDateRange = datefmt.formatRange(startDate, endDate); // formattedDateRange = "17/12/2021-18/12/2021"
286  ```
287
288
289### resolvedOptions
290
291resolvedOptions(): DateTimeOptions
292
293Obtains the formatting options for **DateTimeFormat** object.
294
295**System capability**: SystemCapability.Global.I18n
296
297**Return value**
298
299| Type                                  | Description                           |
300| ------------------------------------ | ----------------------------- |
301| [DateTimeOptions](#datetimeoptions9) | Formatting options for **DateTimeFormat** objects.|
302
303**Example**
304  ```js
305  let datefmt = new Intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' });
306  // Obtain the options of the DateTimeFormat object.
307  let options = datefmt.resolvedOptions();
308  let dateStyle = options.dateStyle; // dateStyle = "full"
309  let timeStyle = options.timeStyle; // timeStyle = "medium"
310  ```
311
312
313## DateTimeOptions<sup>9+</sup>
314
315Provides the options for the **DateTimeFormat** object.
316
317**System capability**: SystemCapability.Global.I18n
318
319| Name             | Type     | Readable  | Writable  | Description                                      |
320| --------------- | ------- | ---- | ---- | ---------------------------------------- |
321| locale          | string  | Yes   | No   | Locale, for example, **zh-Hans-CN**.                |
322| dateStyle       | string  | Yes   | Yes   | Date display format. The value can be **long**, **short**, **medium**, or **full**.|
323| timeStyle       | string  | Yes   | Yes   | Time display format. The value can be **long**, **short**, **medium**, or **full**.|
324| hourCycle       | string  | Yes   | Yes   | Time system for the locale. The value can be any of the following: **h11**, **h12**, **h23**, or **h24**.|
325| timeZone        | string  | Yes   | Yes   | Time zone represented by a valid IANA time zone ID.                     |
326| numberingSystem | string  | Yes   | Yes   | Numbering system for the locale. The value can be any of the following: **adlm**, **ahom**, **arab**, **arabext**, **bali**, **beng**, **bhks**, **brah**, **cakm**, **cham**, **deva**, **diak**, **fullwide**, **gong**, **gonm**, **gujr**, **guru**, **hanidec**, **hmng**, **hmnp**, **java**, **kali**, **khmr**, **knda**, **lana**, **lanatham**, **laoo**, **latn**, **lepc**, **limb**, **mathbold**, **mathdbl**, **mathmono**, **mathsanb**, **mathsans**, **mlym**, **modi**, **mong**, **mroo**, **mtei**, **mymr**, **mymrshan**, **mymrtlng**, **newa**, **nkoo**, **olck**, **orya**, **osma**, **rohg**, **saur**, **segment**, **shrd**, **sind**, **sinh**, **sora**, **sund**, **takr**, **talu**, **tamldec**, **telu**, **thai**, **tibt**, **tirh**, **vaii**, **wara**, **wcho**.|
327| hour12          | boolean | Yes   | Yes   | Whether to use the 12-hour clock.                              |
328| weekday         | string  | Yes   | Yes   | Workday display format. The value can be **long**, **short**, or **narrow**.|
329| era             | string  | Yes   | Yes   | Era display format. The value can be **long**, **short**, or **narrow**.|
330| year            | string  | Yes   | Yes   | Year display format. The value can be **numeric** or **2-digit**.  |
331| month           | string  | Yes   | Yes   | Month display format. The value can be any of the following: **numeric**, **2-digit**, **long**, **short**, **narrow**.|
332| day             | string  | Yes   | Yes   | Day display format. The value can be **numeric** or **2-digit**. |
333| hour            | string  | Yes   | Yes   | Hour display format. The value can be **numeric** or **2-digit**. |
334| minute          | string  | Yes   | Yes   | Minute display format. The value can be **numeric** or **2-digit**. |
335| second          | string  | Yes   | Yes   | Seconds display format. The value can be **numeric** or **2-digit**. |
336| timeZoneName    | string  | Yes   | Yes   | Localized representation of a time zone name.                             |
337| dayPeriod       | string  | Yes   | Yes   | Time period display format. The value can be **long**, **short**, or **narrow**.|
338| localeMatcher   | string  | Yes   | Yes   | Locale matching algorithm. The value can be **lookup** or **best fit**.|
339| formatMatcher   | string  | Yes   | Yes   | Format matching algorithm. The value can be **basic** or **best fit**.|
340
341
342## NumberFormat
343
344
345### constructor<sup>8+</sup>
346
347constructor()
348
349Creates a **NumberFormat** object for the specified locale.
350
351**System capability**: SystemCapability.Global.I18n
352
353**Example**
354  ```js
355  // Use the current system locale to create a NumberFormat object.
356  let numfmt = new Intl.NumberFormat();
357  ```
358
359
360### constructor
361
362constructor(locale: string | Array&lt;string&gt;, options?: NumberOptions)
363
364Creates a **NumberFormat** object for the specified locale.
365
366**System capability**: SystemCapability.Global.I18n
367
368**Parameters**
369
370| Name                 | Type                              | Mandatory  | Description                          |
371| -------------------- | -------------------------------- | ---- | ---------------------------- |
372| locale               | string \| Array&lt;string&gt;    | Yes   | A string containing locale information, including the language, optional script, and region.|
373| options<sup>9+</sup> | [NumberOptions](#numberoptions9) | No   | Options for creating a **NumberFormat** object.               |
374
375**Example**
376  ```js
377  // Use locale en-GB to create a NumberFormat object. Set style to decimal and notation to scientific.
378  let numfmt = new Intl.NumberFormat("en-GB", {style:'decimal', notation:"scientific"});
379  ```
380
381
382### format
383
384format(number: number): string;
385
386Formats a number.
387
388**System capability**: SystemCapability.Global.I18n
389
390**Parameters**
391
392| Name   | Type    | Mandatory  | Description  |
393| ------ | ------ | ---- | ---- |
394| number | number | Yes   | Number to be formatted.|
395
396**Return value**
397
398| Type    | Description        |
399| ------ | ---------- |
400| string | Formatted number.|
401
402
403**Example**
404  ```js
405  // Use locale list ["en-GB", "zh"] to create a NumberFormat object. Because en-GB is a valid locale ID, it is used to create the NumberFormat object.
406  let numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
407  let formattedNumber = numfmt.format(1223); // formattedNumber = 1.223E3
408  ```
409
410
411### resolvedOptions
412
413resolvedOptions(): NumberOptions
414
415Obtains the options of the **NumberFormat** object.
416
417**System capability**: SystemCapability.Global.I18n
418
419**Return value**
420
421| Type                              | Description                         |
422| -------------------------------- | --------------------------- |
423| [NumberOptions](#numberoptions9) | Formatting options for **NumberFormat** objects.|
424
425
426**Example**
427  ```js
428  let numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
429  // Obtain the options of the NumberFormat object.
430  let options = numfmt.resolvedOptions();
431  let style = options.style; // style = decimal
432  let notation = options.notation; // notation = scientific
433  ```
434
435
436## NumberOptions<sup>9+</sup>
437
438Defines the device capability.
439
440**System capability**: SystemCapability.Global.I18n
441
442| Name                      | Type     | Readable  | Writable  | Description                                      |
443| ------------------------ | ------- | ---- | ---- | ---------------------------------------- |
444| locale                   | string  | Yes   | No   | Locale, for example, **zh-Hans-CN**.              |
445| currency                 | string  | Yes   | Yes   | Currency unit, for example, **EUR**, **CNY**, or **USD**.        |
446| currencySign             | string  | Yes   | Yes   | Currency unit symbol. The value can be **symbol**, **narrowSymbol**, **code**, or **name**.|
447| currencyDisplay          | string  | Yes   | Yes   | Currency display mode. The value can be **symbol**, **narrowSymbol**, **code**, or **name**.|
448| unit                     | string  | Yes   | Yes   | Unit name, for example, **meter**, **inch**, or **hectare**.       |
449| unitDisplay              | string  | Yes   | Yes   | Unit display format. The value can be **long**, **short**, or **narrow**.|
450| unitUsage                | string  | Yes   | Yes   | Unit usage scenario. The value can be any of the following: **default**, **area-land-agricult**, **area-land-commercl**, **area-land-residntl**, **length-person**, **length-person-small**, **length-rainfall**, **length-road**, **length-road-small**, **length-snowfall**, **length-vehicle**, **length-visiblty**, **length-visiblty-small**, **length-person-informal**, **length-person-small-informal**, **length-road-informal**, **speed-road-travel**, **speed-wind**, **temperature-person**, **temperature-weather**, **volume-vehicle-fuel**.|
451| signDisplay              | string  | Yes   | Yes   | Number sign display format. The value can be **auto**, **never**, **always**, or **expectZero**.|
452| compactDisplay           | string  | Yes   | Yes   | Compact display format. The value can be **long** or **short**.     |
453| notation                 | string  | Yes   | Yes   | Number formatting specification. The value can be **standard**, **scientific**, **engineering**, or **compact**.|
454| localeMatcher            | string  | Yes   | Yes   | Locale matching algorithm. The value can be **lookup** or **best fit**.|
455| style                    | string  | Yes   | Yes   | Number display format. The value can be **decimal**, **currency**, **percent**, or **unit**.|
456| numberingSystem          | string  | Yes   | Yes   | Numbering system for the locale. The value can be any of the following: **adlm**, **ahom**, **arab**, **arabext**, **bali**, **beng**, **bhks**, **brah**, **cakm**, **cham**, **deva**, **diak**, **fullwide**, **gong**, **gonm**, **gujr**, **guru**, **hanidec**, **hmng**, **hmnp**, **java**, **kali**, **khmr**, **knda**, **lana**, **lanatham**, **laoo**, **latn**, **lepc**, **limb**, **mathbold**, **mathdbl**, **mathmono**, **mathsanb**, **mathsans**, **mlym**, **modi**, **mong**, **mroo**, **mtei**, **mymr**, **mymrshan**, **mymrtlng**, **newa**, **nkoo**, **olck**, **orya**, **osma**, **rohg**, **saur**, **segment**, **shrd**, **sind**, **sinh**, **sora**, **sund**, **takr**, **talu**, **tamldec**, **telu**, **thai**, **tibt**, **tirh**, **vaii**, **wara**, **wcho**.|
457| useGrouping              | boolean | Yes   | Yes   | Whether to use grouping for display.                                 |
458| minimumIntegerDigits     | number  | Yes   | Yes   | Minimum number of digits allowed in the integer part of a number. The value ranges from **1** to **21**.                 |
459| minimumFractionDigits    | number  | Yes   | Yes   | Minimum number of digits in the fraction part of a number. The value ranges from **0** to **20**.                 |
460| maximumFractionDigits    | number  | Yes   | Yes   | Maximum number of digits in the fraction part of a number. The value ranges from **1** to **21**.                 |
461| minimumSignificantDigits | number  | Yes   | Yes   | Minimum number of the least significant digits. The value ranges from **1** to **21**.                 |
462| maximumSignificantDigits | number  | Yes   | Yes   | Maximum number of the least significant digits. The value ranges from **1** to **21**.                 |
463
464
465## Collator<sup>8+</sup>
466
467
468### constructor<sup>8+</sup>
469
470constructor()
471
472Creates a **Collator** object.
473
474**System capability**: SystemCapability.Global.I18n
475
476**Example**
477  ```js
478  // Use the system locale to create a Collator object.
479  let collator = new Intl.Collator();
480  ```
481
482
483### constructor<sup>8+</sup>
484
485constructor(locale: string | Array&lt;string&gt;, options?: CollatorOptions)
486
487Creates a **Collator** object.
488
489**System capability**: SystemCapability.Global.I18n
490
491**Parameters**
492
493| Name                 | Type                                  | Mandatory  | Description                          |
494| -------------------- | ------------------------------------ | ---- | ---------------------------- |
495| locale               | string \| Array&lt;string&gt;        | Yes   | A string containing locale information, including the language, optional script, and region.|
496| options<sup>9+</sup> | [CollatorOptions](#collatoroptions9) | No   | Options for creating a **Collator** object.                |
497
498**Example**
499  ```js
500  // Use locale zh-CN to create a Collator object. Set localeMatcher to lookup and usage to sort.
501  let collator = new Intl.Collator("zh-CN", {localeMatcher: "lookup", usage: "sort"});
502  ```
503
504
505### compare<sup>8+</sup>
506
507compare(first: string, second: string): number
508
509Compares two strings based on the sorting policy of the **Collator** object.
510
511**System capability**: SystemCapability.Global.I18n
512
513**Parameters**
514
515| Name   | Type    | Mandatory  | Description          |
516| ------ | ------ | ---- | ------------ |
517| first  | string | Yes   | First string to compare. |
518| second | string | Yes   | Second string to compare.|
519
520**Return value**
521
522| Type    | Description                                      |
523| ------ | ---------------------------------------- |
524| number | Comparison result. If the value is a negative number, the first string is before the second string. If the value of number is **0**, the first string is equal to the second string. If the value of number is a positive number, the first string is after the second string.|
525
526**Example**
527  ```js
528  // Use locale en-GB to create a Collator object.
529  let collator = new Intl.Collator("en-GB");
530  // Compare the sequence of the first and second strings.
531  let compareResult = collator.compare("first", "second"); // compareResult = -1
532  ```
533
534
535### resolvedOptions<sup>8+</sup>
536
537resolvedOptions(): CollatorOptions
538
539Returns properties reflecting the locale and collation options of a **Collator** object.
540
541**System capability**: SystemCapability.Global.I18n
542
543**Return value**
544
545| Type                                  | Description               |
546| ------------------------------------ | ----------------- |
547| [CollatorOptions](#collatoroptions9) | Properties of the **Collator** object.|
548
549**Example**
550  ```js
551  let collator = new Intl.Collator("zh-Hans", { usage: 'sort', ignorePunctuation: true });
552  // Obtain the options of the Collator object.
553  let options = collator.resolvedOptions();
554  let usage = options.usage; // usage = "sort"
555  let ignorePunctuation = options.ignorePunctuation; // ignorePunctuation = true
556  ```
557
558
559## CollatorOptions<sup>9+</sup>
560
561Represents the properties of a **Collator** object.
562
563**System capability**: SystemCapability.Global.I18n
564
565| Name               | Type     | Readable  | Writable  | Description                                      |
566| ----------------- | ------- | ---- | ---- | ---------------------------------------- |
567| localeMatcher     | string  | Yes   | Yes   | Locale matching algorithm. The value can be **lookup** or **best fit**.|
568| usage             | string  | Yes   | Yes   | Whether the comparison is for sorting or for searching. The value can be **sort** or **search**.       |
569| sensitivity       | string  | Yes   | Yes   | Differences in the strings that lead to non-zero return values. The value can be **base**, **accent**, **case**, or **letiant**.|
570| ignorePunctuation | boolean | Yes   | Yes   | Whether punctuation is ignored. The value can be **true** or **false**.       |
571| collation         | string  | Yes   | Yes   | Rule for sorting regions. The value can be any of the following: **big5han**, **compat**, **dict**, **direct**, **ducet**, **eor**, **gb2312**, **phonebk**, **phonetic**, **pinyin**, **reformed**, **searchjl**, **stroke**, **trad**, **unihan**, **zhuyin**.|
572| numeric           | boolean | Yes   | Yes   | Whether numeric collation is used. The value can be **true** or **false**.         |
573| caseFirst         | string  | Yes   | Yes   | Whether upper case or lower case is sorted first. The value can be **upper**, **lower**, or **false**.|
574
575
576## PluralRules<sup>8+</sup>
577
578
579### constructor<sup>8+</sup>
580
581constructor()
582
583Creates a **PluralRules** object to obtain the singular-plural type of numbers.
584
585**System capability**: SystemCapability.Global.I18n
586
587**Example**
588  ```js
589  // Use the system locale to create a PluralRules object.
590  let pluralRules = new Intl.PluralRules();
591  ```
592
593
594### constructor<sup>8+</sup>
595
596constructor(locale: string | Array&lt;string&gt;, options?: PluralRulesOptions)
597
598Creates a **PluralRules** object to obtain the singular-plural type of numbers.
599
600**System capability**: SystemCapability.Global.I18n
601
602**Parameters**
603
604| Name                 | Type                                      | Mandatory  | Description                          |
605| -------------------- | ---------------------------------------- | ---- | ---------------------------- |
606| locale               | string \| Array&lt;string&gt;            | Yes   | A string containing locale information, including the language, optional script, and region.|
607| options<sup>9+</sup> | [PluralRulesOptions](#pluralrulesoptions9) | No   | Options for creating a **PluralRules** object.               |
608
609**Example**
610  ```js
611  // Use locale zh-CN to create a PluralRules object. Set localeMatcher to lookup and type to cardinal.
612  let pluralRules= new Intl.PluralRules("zh-CN", {"localeMatcher": "lookup", "type": "cardinal"});
613  ```
614
615
616### select<sup>8+</sup>
617
618select(n: number): string
619
620Obtains a string that represents the singular-plural type of the specified number.
621
622**System capability**: SystemCapability.Global.I18n
623
624**Parameters**
625
626| Name | Type    | Mandatory  | Description          |
627| ---- | ------ | ---- | ------------ |
628| n    | number | Yes   | Number for which the singular-plural type is to be obtained.|
629
630**Return value**
631
632| Type    | Description                                      |
633| ------ | ---------------------------------------- |
634| string | Singular-plural type. The value can be any of the following: **zero**, **one**, **two**, **few**, **many**, **others**.|
635
636**Example**
637  ```js
638  // Use locale zh-Hans to create a PluralRules object.
639  let zhPluralRules = new Intl.PluralRules("zh-Hans");
640  // Determine the singular-plural type corresponding to number 1 in locale zh-Hans.
641  let plural = zhPluralRules.select(1); // plural = other
642
643  // Use locale en-US to create a PluralRules object.
644  let enPluralRules = new Intl.PluralRules("en-US");
645  // Determine the singular-plural type corresponding to number 1 in locale en-US.
646  plural = enPluralRules.select(1); // plural = one
647  ```
648
649
650## PluralRulesOptions<sup>9+</sup>
651
652Represents the properties of a **PluralRules** object.
653
654**System capability**: SystemCapability.Global.I18n
655
656| Name                      | Type    | Readable  | Writable  | Description                                      |
657| ------------------------ | ------ | ---- | ---- | ---------------------------------------- |
658| localeMatcher            | string | Yes   | Yes   | Locale matching algorithm. The value can be **lookup** or **best fit**.|
659| type                     | string | Yes   | Yes   | Sorting type. The value can be **cardinal** or **ordinal**.  |
660| minimumIntegerDigits     | number | Yes   | Yes   | Minimum number of digits allowed in the integer part of a number. The value ranges from **1** to **21**.                 |
661| minimumFractionDigits    | number | Yes   | Yes   | Minimum number of digits in the fraction part of a number. The value ranges from **0** to **20**.                 |
662| maximumFractionDigits    | number | Yes   | Yes   | Maximum number of digits in the fraction part of a number. The value ranges from **1** to **21**.                 |
663| minimumSignificantDigits | number | Yes   | Yes   | Minimum number of the least significant digits. The value ranges from **1** to **21**.                 |
664| maximumSignificantDigits | number | Yes   | Yes   | Maximum number of the least significant digits. The value ranges from **1** to **21**.                 |
665
666
667## RelativeTimeFormat<sup>8+</sup>
668
669
670### constructor<sup>8+</sup>
671
672constructor()
673
674Creates a **RelativeTimeFormat** object.
675
676**System capability**: SystemCapability.Global.I18n
677
678**Example**
679  ```js
680  // Use the system locale to create a RelativeTimeFormat object.
681  let relativetimefmt = new Intl.RelativeTimeFormat();
682  ```
683
684
685### constructor<sup>8+</sup>
686
687constructor(locale: string | Array&lt;string&gt;, options?: RelativeTimeFormatInputOptions)
688
689Creates a **RelativeTimeFormat** object.
690
691**System capability**: SystemCapability.Global.I18n
692
693**Parameters**
694
695| Name                 | Type                                      | Mandatory  | Description                          |
696| -------------------- | ---------------------------------------- | ---- | ---------------------------- |
697| locale               | string \| Array&lt;string&gt;            | Yes   | A string containing locale information, including the language, optional script, and region.|
698| options<sup>9+</sup> | [RelativeTimeFormatInputOptions](#relativetimeformatinputoptions9) | No   | Options for creating a **RelativeTimeFormat** object.           |
699
700**Example**
701  ```js
702  // Use locale zh-CN to create a RelativeTimeFormat object. Set localeMatcher to lookup, numeric to always, and style to long.
703  let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {"localeMatcher": "lookup", "numeric": "always", "style": "long"});
704  ```
705
706
707### format<sup>8+</sup>
708
709format(value: number, unit: string): string
710
711Formats the value and unit based on the specified locale and formatting options.
712
713**System capability**: SystemCapability.Global.I18n
714
715**Parameters**
716
717| Name  | Type    | Mandatory  | Description                                      |
718| ----- | ------ | ---- | ---------------------------------------- |
719| value | number | Yes   | Value to format.                             |
720| unit  | string | Yes   | Unit to format. The value can be any of the following: **year**, **quarter**, **month**, **week**, **day**, **hour**, **minute**, **second**.|
721
722**Return value**
723
724| Type    | Description        |
725| ------ | ---------- |
726| string | Relative time after formatting.|
727
728**Example**
729  ```js
730  // Use locale zh-CN to create a RelativeTimeFormat object.
731  let relativetimefmt = new Intl.RelativeTimeFormat("zh-CN");
732  // Obtain the localized representation (in unit of quarter) of number 3 in locale zh-CN.
733  let formatResult = relativetimefmt.format(3, "quarter"); // formatResult = "3 quarters later"
734  ```
735
736
737### formatToParts<sup>8+</sup>
738
739formatToParts(value: number, unit: string): Array&lt;object&gt;
740
741Obtains an array of RelativeTimeFormat objects in parts for locale-aware formatting.
742
743**System capability**: SystemCapability.Global.I18n
744
745**Parameters**
746
747| Name  | Type    | Mandatory  | Description                                      |
748| ----- | ------ | ---- | ---------------------------------------- |
749| value | number | Yes   | Value to format.                             |
750| unit  | string | Yes   | Unit to format. The value can be any of the following: **year**, **quarter**, **month**, **week**, **day**, **hour**, **minute**, **second**.|
751
752**Return value**
753
754| Type                 | Description                         |
755| ------------------- | --------------------------- |
756| Array&lt;object&gt; | An array of **RelativeTimeFormat** objects in parts.|
757
758**Example**
759  ```js
760  // Use locale en to create a RelativeTimeFormat object. Set numeric to auto.
761  let relativetimefmt = new Intl.RelativeTimeFormat("en", {"numeric": "auto"});
762  let parts = relativetimefmt.formatToParts(10, "seconds"); // parts = [ {type: "literal", value: "in"}, {type: "integer", value: 10, unit: "second"}, {type: "literal", value: "seconds"} ]
763  ```
764
765
766### resolvedOptions<sup>8+</sup>
767
768resolvedOptions(): RelativeTimeFormatResolvedOptions
769
770Obtains the formatting options for **RelativeTimeFormat** objects.
771
772**System capability**: SystemCapability.Global.I18n
773
774**Return value**
775
776| Type                                      | Description                               |
777| ---------------------------------------- | --------------------------------- |
778| [RelativeTimeFormatResolvedOptions](#relativetimeformatresolvedoptions8) | Formatting options for **RelativeTimeFormat** objects.|
779
780**Example**
781  ```js
782  // Use locale en-GB to create a RelativeTimeFormat object.
783  let relativetimefmt= new Intl.RelativeTimeFormat("en-GB", { style: "short" });
784  // Obtain the options of the RelativeTimeFormat object.
785  let options = relativetimefmt.resolvedOptions();
786  let style = options.style; // style = "short"
787  ```
788
789
790## RelativeTimeFormatInputOptions<sup>9+</sup>
791
792Represents the properties of a **RelativeTimeFormat** object.
793
794**System capability**: SystemCapability.Global.I18n
795
796| Name           | Type    | Readable  | Writable  | Description                                      |
797| ------------- | ------ | ---- | ---- | ---------------------------------------- |
798| localeMatcher | string | Yes   | Yes   | Locale matching algorithm. The value can be **lookup** or **best fit**.|
799| numeric       | string | Yes   | Yes   | Format of the output message. The value can be **always** or **auto**.     |
800| style         | string | Yes   | Yes   | Length of an internationalized message. The value can be **long**, **short**, or **narrow**.|
801
802
803## RelativeTimeFormatResolvedOptions<sup>8+</sup>
804
805Represents the properties of a **RelativeTimeFormat** object.
806
807**System capability**: SystemCapability.Global.I18n
808
809| Name             | Type    | Readable  | Writable  | Description                                      |
810| --------------- | ------ | ---- | ---- | ---------------------------------------- |
811| locale          | string | Yes   | Yes   | A string containing locale information, including the language, optional script, and region.            |
812| numeric         | string | Yes   | Yes   | Format of the output message. The value can be **always** or **auto**.     |
813| style           | string | Yes   | Yes   | Length of an internationalized message. The value can be **long**, **short**, or **narrow**.|
814| numberingSystem | string | Yes   | Yes   | Numbering system.                                |
815