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