js-apis-intl.md 32.3 KB
Newer Older
S
shawn_he 已提交
1
# Internationalization – Intl
Z
zengyawen 已提交
2

S
shawn_he 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
S
shawn_he 已提交
4
> - 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.
S
shawn_he 已提交
5
>
S
shawn_he 已提交
6
> - 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).
S
shawn_he 已提交
7 8

## Modules to Import
Z
zengyawen 已提交
9 10 11 12 13

```
import Intl from '@ohos.intl';
```

S
shawn_he 已提交
14 15 16 17 18 19

## Locale


### Attributes

S
shawn_he 已提交
20 21
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
22 23 24 25 26 27 28 29 30 31 32 33
| Name             | Type   | Readable  | Writable  | Description                                      |
| --------------- | ------- | ---- | ---- | ---------------------------------------- |
| language        | string  | Yes   | No   | Language associated with the locale, for example, **zh**.                   |
| script          | string  | Yes   | No   | Script type of the language, for example, **Hans**.                         |
| region          | string  | Yes   | No   | Region associated with the locale, for example, **CN**.                        |
| baseName        | string  | Yes   | No   | Basic key information about the locale, which consists of the language, script, and region, for example, **zh-Hans-CN**. |
| caseFirst       | string  | Yes   | No   | Whether case is taken into account for the locale's collation rules. The value can be **upper**, **lower**, or **false**.|
| 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**, **islamicc**.|
| 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**.|
| hourCycle       | string  | Yes   | No   | Time system for the locale. The value can be any of the following: **h12**, **h23**, **h11**, **h24**.|
| 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**.|
| numeric         | boolean | Yes   | No   | Whether to apply special collation rules for numeric characters.                     |
S
shawn_he 已提交
34 35 36 37 38 39 40 41 42 43


### constructor<sup>8+</sup>

constructor()

Creates a Locale object.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
44
**Example**
S
shawn_he 已提交
45 46 47
  ```
  var locale = new Intl.Locale();
  ```
S
shawn_he 已提交
48 49 50 51


### constructor

S
shawn_he 已提交
52
constructor(locale: string, options?: LocaleOptions)
S
shawn_he 已提交
53 54 55 56 57

Creates a Locale object.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
58 59 60 61 62
**Parameters**
| Name    | Type           | Mandatory  | Description                          |
| ------- | ------------- | ---- | ---------------------------- |
| locale  | string        | Yes   | A string containing locale information, including the language, optional script, and region.|
| options | LocaleOptions | No   | Options for creating the **Locale** object.                |
S
shawn_he 已提交
63

S
shawn_he 已提交
64
**Example**
S
shawn_he 已提交
65 66 67 68 69 70 71 72
  ```
  var locale = new Intl.Locale("zh-CN");
  ```


### toString

toString(): string
Z
zengyawen 已提交
73 74 75

Converts locale information to a string.

S
shawn_he 已提交
76 77
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
78 79 80 81
**Return Value**
| Type    | Description         |
| ------ | ----------- |
| string | String containing locale information.|
S
shawn_he 已提交
82

S
shawn_he 已提交
83
**Example**
S
shawn_he 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97
  ```
  var locale = new Intl.Locale("zh-CN");
  locale.toString();
  ```


### maximize

maximize(): Locale

Maximizes information of the **Locale** object. If the script and locale information is missing, add the information.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
98 99 100 101
**Return Value**
| Type               | Description        |
| ----------------- | ---------- |
| [Locale](#locale) | **Locale** object with the maximized information.|
S
shawn_he 已提交
102

S
shawn_he 已提交
103
**Example**
S
shawn_he 已提交
104 105 106 107 108
  ```
  var locale = new Intl.Locale("zh-CN");
  locale.maximize();
  ```

Z
zengyawen 已提交
109

S
shawn_he 已提交
110
### minimize
Z
zengyawen 已提交
111

S
shawn_he 已提交
112
minimize(): Locale
Z
zengyawen 已提交
113

S
shawn_he 已提交
114
Minimizes information of the **Locale** object. If the script and locale information is present, delete the information.
Z
zengyawen 已提交
115

S
shawn_he 已提交
116
**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
117

S
shawn_he 已提交
118 119 120 121
**Return Value**
| Type               | Description        |
| ----------------- | ---------- |
| [Locale](#locale) | **Locale** object with the minimized information.|
Z
zengyawen 已提交
122

S
shawn_he 已提交
123
**Example**
S
shawn_he 已提交
124 125 126 127
  ```
  var locale = new Intl.Locale("zh-CN");
  locale.minimize();
  ```
Z
zengyawen 已提交
128 129


S
shawn_he 已提交
130 131 132 133 134 135
## LocaleOptions

Represents the locale options.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
136 137
| Name             | Type   | Readable  | Writable  | Description                                      |
| --------------- | ------- | ---- | ---- | ---------------------------------------- |
N
Neil Chen 已提交
138
| 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**, **islamicc**.|
S
shawn_he 已提交
139 140 141 142 143
| 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**.|
| hourCycle       | string  | Yes   | Yes   | Time system for the locale. The value can be any of the following: **h11**, **h12**, **h23**, **h24**.|
| 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**.|
| numeric         | boolean | Yes   | Yes   | Whether to use the 12-hour clock.                              |
| caseFirst       | string  | Yes   | Yes   | Whether upper case or lower case is sorted first. The value can be **upper**, **lower**, or **false**.|
S
shawn_he 已提交
144 145


S
shawn_he 已提交
146
## DateTimeFormat
Z
zengyawen 已提交
147

S
shawn_he 已提交
148

S
shawn_he 已提交
149
### constructor<sup>8+</sup>
S
shawn_he 已提交
150

S
shawn_he 已提交
151
constructor()
S
shawn_he 已提交
152 153 154 155 156

Creates a **DateTimeOptions** object for the specified locale.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
157
**Example**
S
shawn_he 已提交
158
  ```
S
shawn_he 已提交
159
  var datefmt= new Intl.DateTimeFormat();
S
shawn_he 已提交
160 161 162 163 164
  ```


### constructor

S
shawn_he 已提交
165
constructor(locale: string | Array&lt;string&gt;, options?: DateTimeOptions)
S
shawn_he 已提交
166 167 168 169 170

Creates a **DateTimeOptions** object for the specified locale.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
171 172 173 174 175
**Parameters**
| Name    | Type                                 | Mandatory  | Description                          |
| ------- | ----------------------------------- | ---- | ---------------------------- |
| locale  | string \| Array&lt;string&gt;       | Yes   | A string containing locale information, including the language, optional script, and region.|
| options | [DateTimeOptions](#datetimeoptions) | No   | Options for creating a **DateTimeFormat** object.             |
S
shawn_he 已提交
176

S
shawn_he 已提交
177
**Example**
S
shawn_he 已提交
178 179 180 181 182
  ```
  var datefmt= new Intl.DateTimeFormat("zh-CN", { dateStyle: 'full', timeStyle: 'medium' });
  ```


S
shawn_he 已提交
183
**Example**
S
shawn_he 已提交
184 185 186 187 188 189 190 191
  ```
  var datefmt= new Intl.DateTimeFormat(["ban", "zh"], { dateStyle: 'full', timeStyle: 'medium' });
  ```


### format

format(date: Date): string
Z
zengyawen 已提交
192 193 194

Formats the specified date and time.

S
shawn_he 已提交
195 196
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
197 198 199 200
**Parameters**
| Name | Type  | Mandatory  | Description     |
| ---- | ---- | ---- | ------- |
| date | Date | Yes   | Date and time to be formatted.|
S
shawn_he 已提交
201

S
shawn_he 已提交
202 203 204 205
**Return Value**
| Type    | Description          |
| ------ | ------------ |
| string | A string containing the formatted date and time.|
S
shawn_he 已提交
206

S
shawn_he 已提交
207
**Example**
S
shawn_he 已提交
208 209 210 211 212 213 214 215 216
  ```
  var date = new Date(2021, 11, 17, 3, 24, 0);
  var datefmt = new Intl.DateTimeFormat("en-GB");
  datefmt.format(date);
  ```


### formatRange

S
shawn_he 已提交
217
formatRange(startDate: Date, endDate: Date): string
Z
zengyawen 已提交
218 219 220

Formats the specified date range.

S
shawn_he 已提交
221 222
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
223 224 225 226 227
**Parameters**
| Name      | Type  | Mandatory  | Description      |
| --------- | ---- | ---- | -------- |
| startDate | Date | Yes   | Start date and time to be formatted.|
| endDate   | Date | Yes   | End date and time to be formatted.|
S
shawn_he 已提交
228

S
shawn_he 已提交
229 230 231 232
**Return Value**
| Type    | Description            |
| ------ | -------------- |
| string | A string containing the formatted date and time range.|
S
shawn_he 已提交
233

S
shawn_he 已提交
234
**Example**
S
shawn_he 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  ```
  var startDate = new Date(2021, 11, 17, 3, 24, 0);
  var endDate = new Date(2021, 11, 18, 3, 24, 0);
  var datefmt = new Intl.DateTimeFormat("en-GB");
  datefmt.formatRange(startDate, endDate);
  ```


### resolvedOptions

resolvedOptions(): DateTimeOptions

Obtains the formatting options for **DateTimeFormat** object.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
251 252 253 254
**Return Value**
| Type                                 | Description                           |
| ----------------------------------- | ----------------------------- |
| [DateTimeOptions](#datetimeoptions) | Formatting options for **DateTimeFormat** objects.|
S
shawn_he 已提交
255

S
shawn_he 已提交
256
**Example**
S
shawn_he 已提交
257 258 259 260 261 262 263 264 265 266
  ```
  var datefmt = new Intl.DateTimeFormat("en-GB");
  datefmt.resolvedOptions();
  ```


## DateTimeOptions

Provides the options for the **DateTimeFormat** object.

S
shawn_he 已提交
267 268
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
| Name             | Type   | Readable  | Writable  | Description                                      |
| --------------- | ------- | ---- | ---- | ---------------------------------------- |
| locale          | string  | Yes   | No   | Locale, for example, **zh-Hans-CN**.                |
| dateStyle       | string  | Yes   | Yes   | Date display format. The value can be **long**, **short**, **medium**, or **full**.|
| timeStyle       | string  | Yes   | Yes   | Time display format. The value can be **long**, **short**, **medium**, or **full**.|
| hourCycle       | string  | Yes   | Yes   | Time system for the locale. The value can be any of the following: **h11**, **h12**, **h23**, **h24**.|
| timeZone        | string  | Yes   | Yes   | Time zone represented by a valid IANA time zone ID.                     |
| 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**.|
| hour12          | boolean | Yes   | Yes   | Whether to use the 12-hour clock.                              |
| weekday         | string  | Yes   | Yes   | Workday display format. The value can be **long**, **short**, or **narrow**.|
| era             | string  | Yes   | Yes   | Era display format. The value can be **long**, **short**, or **narrow**.|
| year            | string  | Yes   | Yes   | Year display format. The value can be **numeric** or **2-digit**.  |
| month           | string  | Yes   | Yes   | Month display format. The value can be any of the following: **numeric**, **2-digit**, **long**, **short**, **narrow**.|
| day             | string  | Yes   | Yes   | Day display format. The value can be **numeric** or **2-digit**. |
| hour            | string  | Yes   | Yes   | Hour display format. The value can be **numeric** or **2-digit**. |
| minute          | string  | Yes   | Yes   | Minute display format. The value can be **numeric** or **2-digit**. |
| second          | string  | Yes   | Yes   | Seconds display format. The value can be **numeric** or **2-digit**. |
| timeZoneName    | string  | Yes   | Yes   | Localized representation of a time zone name.                             |
| dayPeriod       | string  | Yes   | Yes   | Time period display format. The value can be **long**, **short**, or **narrow**.|
| localeMatcher   | string  | Yes   | Yes   | Locale matching algorithm. The value can be **lookup** or **best fit**.|
| formatMatcher   | string  | Yes   | Yes   | Format matching algorithm. The value can be **basic** or **best fit**.|
S
shawn_he 已提交
290 291 292 293 294


## NumberFormat


S
shawn_he 已提交
295
### constructor<sup>8+</sup>
S
shawn_he 已提交
296

S
shawn_he 已提交
297
constructor()
S
shawn_he 已提交
298 299 300 301 302

Creates a **NumberFormat** object for the specified locale.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
303
**Example**
S
shawn_he 已提交
304
  ```
S
shawn_he 已提交
305
  var numfmt = new Intl.NumberFormat();
S
shawn_he 已提交
306 307 308 309 310
  ```


### constructor

S
shawn_he 已提交
311
constructor(locale: string | Array&lt;string&gt;, options?: NumberOptions)
S
shawn_he 已提交
312 313 314 315 316

Creates a **NumberFormat** object for the specified locale.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
317
Parameters
S
shawn_he 已提交
318 319 320 321
| Name    | Type                             | Mandatory  | Description                          |
| ------- | ------------------------------- | ---- | ---------------------------- |
| locale  | string \| Array&lt;string&gt;   | Yes   | A string containing locale information, including the language, optional script, and region.|
| options | [NumberOptions](#numberoptions) | No   | Options for creating a **NumberFormat** object.               |
S
shawn_he 已提交
322

S
shawn_he 已提交
323
**Example**
S
shawn_he 已提交
324
  ```
S
shawn_he 已提交
325
  var numfmt = new Intl.NumberFormat("en-GB", {style:'decimal', notation:"scientific"});
S
shawn_he 已提交
326 327 328 329 330 331
  ```


### format

format(number: number): string;
Z
zengyawen 已提交
332 333 334

Formats a number.

S
shawn_he 已提交
335 336
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
337 338 339 340
**Parameters**
| Name   | Type    | Mandatory  | Description  |
| ------ | ------ | ---- | ---- |
| number | number | Yes   | Number to be formatted.|
S
shawn_he 已提交
341

S
shawn_he 已提交
342 343 344 345
**Return Value**
| Type    | Description        |
| ------ | ---------- |
| string | Formatted number.|
S
shawn_he 已提交
346 347


S
shawn_he 已提交
348
**Example**
S
shawn_he 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362
  ```
  var numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
  numfmt.format(1223);
  ```


### resolvedOptions

resolvedOptions(): NumberOptions

Obtains the options of the **NumberFormat** object.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
363 364 365 366
**Return Value**
| Type                             | Description                         |
| ------------------------------- | --------------------------- |
| [NumberOptions](#numberoptions) | Formatting options for **NumberFormat** objects.|
S
shawn_he 已提交
367 368


S
shawn_he 已提交
369
**Example**
S
shawn_he 已提交
370 371 372 373 374 375 376
  ```
  var numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
  numfmt.resolvedOptions();
  ```


## NumberOptions
Z
zengyawen 已提交
377 378 379

Provides the device capability.

S
shawn_he 已提交
380 381
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
382 383 384 385 386 387 388
| Name                      | Type   | Readable  | Writable  | Description                                      |
| ------------------------ | ------- | ---- | ---- | ---------------------------------------- |
| locale                   | string  | Yes   | No   | Locale, for example, **zh-Hans-CN**.              |
| currency                 | string  | Yes   | Yes   | Currency unit, for example, **EUR**, **CNY**, or **USD**.        |
| currencySign             | string  | Yes   | Yes   | Currency unit symbol. The value can be **symbol**, **narrowSymbol**, **code**, or **name**.|
| currencyDisplay          | string  | Yes   | Yes   | Currency display mode. The value can be **symbol**, **narrowSymbol**, **code**, or **name**.|
| unit                     | string  | Yes   | Yes   | Unit name, for example, **meter**, **inch**, or **hectare**.       |
S
shawn_he 已提交
389
| unitDisplay              | string  | Yes   | Yes   | Unit display format. The value can be **long**, **short**, or **narrow**.|
S
shawn_he 已提交
390
| unitUsage<sup>8+</sup>                | 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**.|
S
shawn_he 已提交
391 392 393 394 395 396 397 398 399 400 401 402
| signDisplay              | string  | Yes   | Yes   | Number sign display format. The value can be **auto**, **never**, **always**, or **expectZero**.|
| compactDisplay           | string  | Yes   | Yes   | Compact display format. The value can be **long** or **short**.     |
| notation                 | string  | Yes   | Yes   | Number formatting specification. The value can be **standard**, **scientific**, **engineering**, or **compact**.|
| localeMatcher            | string  | Yes   | Yes   | Locale matching algorithm. The value can be **lookup** or **best fit**.|
| style                    | string  | Yes   | Yes   | Number display format. The value can be **decimal**, **currency**, **percent**, or **unit**.|
| 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**.|
| useGrouping              | boolean | Yes   | Yes   | Whether to use grouping for display.                                 |
| minimumIntegerDigits     | number  | Yes   | Yes   | Minimum number of digits allowed in the integer part of a number. The value ranges from **1** to **21**.                 |
| minimumFractionDigits    | number  | Yes   | Yes   | Minimum number of digits in the fraction part of a number. The value ranges from **0** to **20**.                 |
| maximumFractionDigits    | number  | Yes   | Yes   | Maximum number of digits in the fraction part of a number. The value ranges from **1** to **21**.                 |
| minimumSignificantDigits | number  | Yes   | Yes   | Minimum number of the least significant digits. The value ranges from **1** to **21**.                 |
| maximumSignificantDigits | number  | Yes   | Yes   | Maximum number of the least significant digits. The value ranges from **1** to **21**.                 |
Z
zengyawen 已提交
403 404


S
shawn_he 已提交
405 406 407 408 409 410 411 412 413 414 415
## Collator<sup>8+</sup>


### constructor<sup>8+</sup>

constructor()

Creates a Collator object.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
416
**Example**
S
shawn_he 已提交
417 418 419 420 421 422 423
  ```
  var collator = new Intl.Collator();
  ```


### constructor<sup>8+</sup>

S
shawn_he 已提交
424
constructor(locale: string | Array&lt;string&gt;, options?: CollatorOptions)
S
shawn_he 已提交
425 426 427 428 429

Creates a Collator object.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
430
**Parameters**
S
shawn_he 已提交
431

S
shawn_he 已提交
432 433 434 435
| Name    | Type                                 | Mandatory  | Description                          |
| ------- | ----------------------------------- | ---- | ---------------------------- |
| locale  | string \| Array&lt;string&gt;       | Yes   | A string containing locale information, including the language, optional script, and region.|
| options | [CollatorOptions](#collatoroptions) | No   | Options for creating a **Collator** object.                |
S
shawn_he 已提交
436

S
shawn_he 已提交
437
**Example**
S
shawn_he 已提交
438
  ```
S
shawn_he 已提交
439
  var collator = new Intl.Collator("zh-CN", {localeMatcher: "lookup", usage: "sort"});
S
shawn_he 已提交
440 441 442 443 444 445 446 447 448 449 450
  ```


### compare<sup>8+</sup>

compare(first: string, second: string): number

Compares two strings based on the sorting policy of the **Collator** object.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
451 452 453 454 455
**Parameters**
| Name   | Type    | Mandatory  | Description          |
| ------ | ------ | ---- | ------------ |
| first  | string | Yes   | First string to compare. |
| second | string | Yes   | Second string to compare.|
S
shawn_he 已提交
456

S
shawn_he 已提交
457 458 459 460
**Return Value**
| Type    | Description                                      |
| ------ | ---------------------------------------- |
| 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.|
S
shawn_he 已提交
461

S
shawn_he 已提交
462
**Example**
S
shawn_he 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476
  ```
  var collator = new Intl.Collator("zh-Hans");
  collator.compare("first", "second");
  ```


### resolvedOptions<sup>8+</sup>

resolvedOptions(): CollatorOptions

Returns properties reflecting the locale and collation options of a **Collator** object.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
477 478 479 480
**Return Value**
| Type                                 | Description               |
| ----------------------------------- | ----------------- |
| [CollatorOptions](#collatoroptions) | Properties of the **Collator** object.|
S
shawn_he 已提交
481

S
shawn_he 已提交
482
**Example**
S
shawn_he 已提交
483 484 485 486 487 488 489 490 491 492
  ```
  var collator = new Intl.Collator("zh-Hans");
  var options = collator.resolvedOptions();
  ```


## CollatorOptions<sup>8+</sup><a name=collatoroptions></a>

Represents the properties of a **Collator** object.

S
shawn_he 已提交
493 494
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
495 496 497 498 499 500 501 502 503
| Name               | Type   | Readable  | Writable  | Description                                      |
| ----------------- | ------- | ---- | ---- | ---------------------------------------- |
| localeMatcher     | string  | Yes   | Yes   | Locale matching algorithm. The value can be **lookup** or **best fit**.|
| usage             | string  | Yes   | Yes   | Whether the comparison is for sorting or for searching. The value can be **sort** or **search**.       |
| sensitivity       | string  | Yes   | Yes   | Differences in the strings that lead to non-zero return values. The value can be **base**, **accent**, **case**, or **variant**.|
| ignorePunctuation | boolean | Yes   | Yes   | Whether punctuation is ignored. The value can be **true** or **false**.       |
| 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**.|
| numeric           | boolean | Yes   | Yes   | Whether numeric collation is used. The value can be **true** or **false**.         |
| caseFirst         | string  | Yes   | Yes   | Whether upper case or lower case is sorted first. The value can be **upper**, **lower**, or **false**.|
S
shawn_he 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516


## PluralRules<sup>8+</sup>


### constructor<sup>8+</sup>

constructor()

Create a **PluralRules** object.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
517
**Example**
S
shawn_he 已提交
518 519 520 521 522 523 524
  ```
  var pluralRules = new Intl.PluralRules();
  ```


### constructor<sup>8+</sup>

S
shawn_he 已提交
525
constructor(locale: string | Array&lt;string&gt;, options?: PluralRulesOptions)
S
shawn_he 已提交
526 527 528 529

Create a **PluralRules** object.

**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
530 531

Parameters
S
shawn_he 已提交
532 533 534 535
| Name    | Type                                      | Mandatory  | Description                          |
| ------- | ---------------------------------------- | ---- | ---------------------------- |
| locale  | string \| Array&lt;string&gt;            | Yes   | A string containing locale information, including the language, optional script, and region.|
| options | [PluralRulesOptions](#pluralrulesoptions) | No   | Options for creating a **PluralRules** object.               |
S
shawn_he 已提交
536

S
shawn_he 已提交
537
**Example**
S
shawn_he 已提交
538 539 540
  ```
  var pluralRules= new Intl.PluraRules("zh-CN", {"localeMatcher": "lookup", "type": "cardinal"});
  ```
Z
zengyawen 已提交
541

S
shawn_he 已提交
542 543 544 545

### select<sup>8+</sup>

select(n: number): string
Z
zengyawen 已提交
546 547 548

Obtains a string that represents the singular-plural type of the specified number.

S
shawn_he 已提交
549 550
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
551 552 553 554
**Parameters**
| Name | Type    | Mandatory  | Description          |
| ---- | ------ | ---- | ------------ |
| n    | number | Yes   | Number for which the singular-plural type is to be obtained.|
S
shawn_he 已提交
555

S
shawn_he 已提交
556 557 558 559
**Return Value**
| Type    | Description                                      |
| ------ | ---------------------------------------- |
| string | Singular-plural type. The value can be any of the following: **one**, **two**, **few**, **many**, **others**.|
S
shawn_he 已提交
560

S
shawn_he 已提交
561
**Example**
S
shawn_he 已提交
562 563 564 565 566 567 568 569 570 571
  ```
  var pluralRules = new Intl.PluralRules("zh-Hans");
  pluralRules.select(1);
  ```


## PluralRulesOptions<sup>8+</sup><a name=pluralrulesoptions></a>

Represents the properties of a **PluralRules** object.

S
shawn_he 已提交
572 573
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
574 575 576 577 578 579 580 581 582
| Name                      | Type  | Readable  | Writable  | Description                                      |
| ------------------------ | ------ | ---- | ---- | ---------------------------------------- |
| localeMatcher            | string | Yes   | Yes   | Locale matching algorithm. The value can be **lookup** or **best fit**.|
| type                     | string | Yes   | Yes   | Sorting type. The value can be **cardinal** or **ordinal**.  |
| minimumIntegerDigits     | number | Yes   | Yes   | Minimum number of digits allowed in the integer part of a number. The value ranges from **1** to **21**.                 |
| minimumFractionDigits    | number | Yes   | Yes   | Minimum number of digits in the fraction part of a number. The value ranges from **0** to **20**.                 |
| maximumFractionDigits    | number | Yes   | Yes   | Maximum number of digits in the fraction part of a number. The value ranges from **1** to **21**.                 |
| minimumSignificantDigits | number | Yes   | Yes   | Minimum number of the least significant digits. The value ranges from **1** to **21**.                 |
| maximumSignificantDigits | number | Yes   | Yes   | Maximum number of the least significant digits. The value ranges from **1** to **21**.                 |
S
shawn_he 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595


## RelativeTimeFormat<sup>8+</sup>


### constructor<sup>8+</sup>

constructor()

Creates a **RelativeTimeFormat** object.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
596
**Example**
S
shawn_he 已提交
597 598 599 600 601 602 603
  ```
  var relativetimefmt = new Intl.RelativeTimeFormat();
  ```


### constructor<sup>8+</sup>

S
shawn_he 已提交
604
constructor(locale: string | Array&lt;string&gt;, options?: RelativeTimeFormatInputOptions)
S
shawn_he 已提交
605 606 607 608

Creates a **RelativeTimeFormat** object.

**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
609 610

Parameters
S
shawn_he 已提交
611 612 613 614
| Name    | Type                                      | Mandatory  | Description                          |
| ------- | ---------------------------------------- | ---- | ---------------------------- |
| locale  | string \| Array&lt;string&gt;            | Yes   | A string containing locale information, including the language, optional script, and region.|
| options | [RelativeTimeFormatInputOptions](#relativetimeformatinputoptions) | No   | Options for creating a **RelativeTimeFormat** object.           |
S
shawn_he 已提交
615

S
shawn_he 已提交
616
**Example**
S
shawn_he 已提交
617 618 619 620
  ```
  var relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {"localeMatcher": "lookup", "numeric": "always", "style": "long"});
  ```

Z
zengyawen 已提交
621

S
shawn_he 已提交
622 623
### format<sup>8+</sup>

S
shawn_he 已提交
624
format(value: number, unit: string): string
Z
zengyawen 已提交
625 626 627

Formats the value and unit based on the specified locale and formatting options.

S
shawn_he 已提交
628 629
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
630 631 632 633 634
**Parameters**
| Name  | Type    | Mandatory  | Description                                      |
| ----- | ------ | ---- | ---------------------------------------- |
| value | number | Yes   | Value to format.                             |
| unit  | string | Yes   | Unit to format. The value can be any of the following: **year**, **quarter**, **month**, **week**, **day**, **hour**, **minute**, **second**.|
S
shawn_he 已提交
635

S
shawn_he 已提交
636 637 638 639
**Return Value**
| Type    | Description        |
| ------ | ---------- |
| string | Relative time after formatting.|
S
shawn_he 已提交
640

S
shawn_he 已提交
641
**Example**
S
shawn_he 已提交
642 643 644 645 646 647 648 649
  ```
  var relativetimefmt = new Intl.RelativeTimeFormat("zh-CN");
  relativetimefmt.format(3, "quarter")
  ```


### formatToParts<sup>8+</sup>

S
shawn_he 已提交
650
formatToParts(value: number, unit: string): Array&lt;object&gt;
S
shawn_he 已提交
651 652 653 654 655

Returns an array of RelativeTimeFormat objects in parts for locale-aware formatting.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
656 657 658 659 660
**Parameters**
| Name  | Type    | Mandatory  | Description                                      |
| ----- | ------ | ---- | ---------------------------------------- |
| value | number | Yes   | Value to format.                             |
| unit  | string | Yes   | Unit to format. The value can be any of the following: **year**, **quarter**, **month**, **week**, **day**, **hour**, **minute**, **second**.|
S
shawn_he 已提交
661

S
shawn_he 已提交
662 663 664 665
**Return Value**
| Type                 | Description                         |
| ------------------- | --------------------------- |
| Array&lt;object&gt; | An array of **RelativeTimeFormat** objects in parts.|
S
shawn_he 已提交
666

S
shawn_he 已提交
667
**Example**
S
shawn_he 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681
  ```
  var relativetimefmt = new Intl.RelativeTimeFormat("en", {"numeric": "auto"});
  var parts = relativetimefmt.format(10, "seconds");
  ```


### resolvedOptions<sup>8+</sup>

resolvedOptions(): RelativeTimeFormatResolvedOptions

Obtains the formatting options for **RelativeTimeFormat** objects.

**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
682 683 684 685
**Return Value**
| Type                                      | Description                               |
| ---------------------------------------- | --------------------------------- |
| [RelativeTimeFormatResolvedOptions](#relativetimeformatresolvedoptions) | Formatting options for **RelativeTimeFormat** objects.|
S
shawn_he 已提交
686

S
shawn_he 已提交
687
**Example**
S
shawn_he 已提交
688 689 690 691 692 693 694 695 696 697
  ```
  var relativetimefmt= new Intl.RelativeTimeFormat("en-GB");
  relativetimefmt.resolvedOptions();
  ```


## RelativeTimeFormatInputOptions<sup>8+</sup><a name=relativetimeformatinputoptions></a>

Represents the properties of a **RelativeTimeFormat** object.

S
shawn_he 已提交
698 699
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
700 701 702 703 704
| Name           | Type  | Readable  | Writable  | Description                                      |
| ------------- | ------ | ---- | ---- | ---------------------------------------- |
| localeMatcher | string | Yes   | Yes   | Locale matching algorithm. The value can be **lookup** or **best fit**.|
| numeric       | string | Yes   | Yes   | Format of the output message. The value can be **always** or **auto**.     |
| style         | string | Yes   | Yes   | Length of an internationalized message. The value can be **long**, **short**, or **narrow**.|
S
shawn_he 已提交
705 706 707 708 709 710


## RelativeTimeFormatResolvedOptions<sup>8+</sup><a name=relativetimeformatresolvedoptions></a>

Represents the properties of a **RelativeTimeFormat** object.

S
shawn_he 已提交
711 712
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
713 714 715 716 717 718
| Name             | Type  | Readable  | Writable  | Description                                      |
| --------------- | ------ | ---- | ---- | ---------------------------------------- |
| locale          | string | Yes   | Yes   | A string containing locale information, including the language, optional script, and region.            |
| numeric         | string | Yes   | Yes   | Format of the output message. The value can be **always** or **auto**.     |
| style           | string | Yes   | Yes   | Length of an internationalized message. The value can be **long**, **short**, or **narrow**.|
| numberingSystem | string | Yes   | Yes   | Numbering system.                                |