1# i18n Development 2 3The **i18n** module provides system-related or enhanced i18n capabilities, such as locale management, phone number formatting, and calendar, through supplementary i18n APIs that are not defined in ECMA 402. For more details about APIs and their usage, see [i18n](../reference/apis/js-apis-i18n.md). 4 5The [intl](intl-guidelines.md) module provides basic i18n capabilities through the standard i18n interfaces defined in ECMA 402. It works with the **i18n** module to provide a complete suite of i18n capabilities. 6 7## Obtaining and Setting i18n Information 8 9The following table lists the APIs used to configure information such as the system language, preferred language, country or region, 24-hour clock, and use of local digits. 10 11### Available APIs 12 13| Class | API | Description | 14| --------- | ---------------------------------------- | --------------------- | 15| System | getDisplayCountry(country:string,locale:string,sentenceCase?:boolean):string<sup>9+</sup> | Obtains the localized representation of a country. | 16| System | getDisplayLanguage(language:string,locale:string,sentenceCase?:boolean):string<sup>9+</sup> | Obtains the localized representation of a language. | 17| System | getSystemLanguages():Array<string><sup>9+</sup> | Obtains the system language list. | 18| System | getSystemCountries(language: string):Array<string><sup>9+</sup> | Obtains the list of countries and regions supported for the specified language. | 19| System | isSuggested(language: string, region?: string): boolean<sup>9+</sup> | Checks whether the system language matches the specified region. | 20| System | getSystemLanguage():string<sup>9+</sup> | Obtains the system language. | 21| System | setSystemLanguage(language: string)<sup>9+</sup> | Sets the system language. | 22| System | getSystemRegion():string<sup>9+</sup> | Obtains the system region. | 23| System | setSystemRegion(region: string)<sup>9+</sup> | Sets the system region. | 24| System | getSystemLocale():string<sup>9+</sup> | Obtains the system locale. | 25| System | setSystemLocale(locale: string)<sup>9+</sup> | Sets the system locale. | 26| System | is24HourClock():boolean<sup>9+</sup> | Checks whether the 24-hour clock is used. | 27| System | set24HourClock():boolean<sup>9+</sup> | Sets the 24-hour clock. | 28| System | addPreferredLanguage(language: string, index?: number)<sup>9+</sup> | Adds a preferred language to the specified position on the preferred language list. | 29| System | removePreferredLanguage(index: number)<sup>9+</sup> | Deletes a preferred language from the specified position on the preferred language list. | 30| System | getPreferredLanguageList()<sup>9+</sup> | Obtains the preferred language list. | 31| System | getFirstPreferredLanguage()<sup>9+</sup> | Obtains the first language in the preferred language list. | 32| System | getAppPreferredLanguage()<sup>9+</sup> | Obtains the preferred language of an application. | 33| System | setUsingLocalDigit(flag: boolean)<sup>9+</sup> | Specifies whether to enable use of local digits. | 34| System | getUsingLocalDigit()<sup>9+</sup> | Checks whether use of local digits is enabled. | 35| | isRTL(locale:string):boolean<sup>9+</sup> | Checks whether the locale uses a right-to-left (RTL) language.| 36 37### How to Develop 381. Import the **i18n** module. 39 40 ```js 41 import I18n from '@ohos.i18n'; 42 ``` 43 442. Obtain and set the system language. 45 46 Call **setSystemLanguage** to set the system language. (This is a system API and can be called only by system applications with the **UPDATE_CONFIGURATION** permission.) 47 Call the **getSystemLanguage** API to obtain the system language. 48 49 ```js 50 try { 51 I18n.System.setSystemLanguage("en"); // Set the system language to en. 52 let language = I18n.System.getSystemLanguage(); // language = "en" 53 } catch(error) { 54 console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); 55 } 56 ``` 57 583. Obtain and set the system locale. 59 60 Call **setSystemRegion** to set the system country or region. (This is a system API and can be called only by system applications with the **UPDATE_CONFIGURATION** permission.) 61 Call **getSystemRegion** to obtain the system country or region. 62 63 ```js 64 try { 65 I18n.System.setSystemRegion("CN"); // Set the system country to CN. 66 let region = I18n.System.getSystemRegion(); // region = "CN" 67 } catch(error) { 68 console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); 69 } 70 ``` 71 724. Obtain and set the system locale. 73 74 Call **setSystemLocale** to set the system locale. (This is a system API and can be called only by system applications with the **UPDATE_CONFIGURATION** permission.) For details about how to set a locale, see [Setting Locale Information](../internationalization/intl-guidelines.md#setting-locale-information). 75 Call **getSystemLocale** to obtain the system locale. 76 77 ```js 78 try { 79 I18n.System.setSystemLocale("zh-Hans-CN"); // Set the system locale to zh-Hans-CN. 80 let locale = I18n.System.getSystemLocale(); // locale = "zh-Hans-CN" 81 } catch(error) { 82 console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); 83 } 84 ``` 85 865. Check whether the locale uses a right-to-left (RTL) language. 87 88 Call **isRTL** to check whether the locale uses an RTL language. 89 90 ```js 91 try { 92 let rtl = I18n.isRTL("zh-CN"); // rtl = false 93 rtl = I18n.isRTL("ar"); // rtl = true 94 } catch(error) { 95 console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); 96 } 97 ``` 98 996. Obtain and set the 24-hour clock. 100 101 Call **set24HourClock** to enable the 24-hour clock. 102 Call **is24HourClock** to check whether the 24-hour clock is enabled. 103 104 ```js 105 try { 106 I18n.System.set24HourClock(true); 107 let hourClock = I18n.System.is24HourClock(); // hourClock = true 108 } catch(error) { 109 console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); 110 } 111 ``` 112 1137. Obtain the localized representation of a language. 114 115 Call **getDisplayLanguage** to obtain the localized representation of a language. **language** indicates the language to be localized, **locale** indicates the locale, and **sentenceCase** indicates whether the first letter of the result must be capitalized. 116 117 ```js 118 try { 119 let language = "en"; 120 let locale = "zh-CN"; 121 let sentenceCase = false; 122 let localizedLanguage = I18n.System.getDisplayLanguage(language, locale, sentenceCase); // localizedLanguage = "English" 123 } catch(error) { 124 console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); 125 } 126 ``` 127 1288. Obtain the localized representation of a country. 129 130 Call **getDisplayCountry** to obtain the localized representation of a country. **country** indicates the country to be localized, **locale** indicates the locale, and **sentenceCase** indicates whether the first letter of the result must be capitalized. 131 132 ```js 133 try { 134 let country = "US"; 135 let locale = "zh-CN"; 136 let sentenceCase = false; 137 let localizedCountry = I18n.System.getDisplayCountry(country, locale, sentenceCase); // localizedCountry = "U.S." 138 } catch(error) { 139 console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); 140 } 141 ``` 142 1439. Obtain the list of system languages and the list of countries supported by a system language. 144 145 Call **getSystemLanguages** to obtain the list of system languages. 146 Call **getSystemCountries** to obtain the list of countries and regions supported by a system language. 147 ```js 148 149 try { 150 let languageList = I18n.System.getSystemLanguages(); // languageList = ["en-Latn-US", "zh-Hans"] 151 let countryList = I18n.System.getSystemCountries("zh"); // countryList = ["ZW", "YT", ..., "CN", "DE"], 240 countries and regions in total 152 } catch(error) { 153 console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); 154 } 155 ``` 156 15710. Check whether the language matches a country or region. 158 159 Call **isSuggested** to check whether the language matches a country or region. 160 161 ```js 162 try { 163 let isSuggest = I18n.System.isSuggested("zh", "CN"); // isSuggest = true 164 } catch(error) { 165 console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); 166 } 167 ``` 168 16911. Obtain and set the preferred language. 170 171 Call **addPreferredLanguage** to add a preferred language to the preferred language list. 172 Call **removePreferredLanguage** to remove a preferred language from the preferred language list. (**addPreferredLanguage** and **removePreferredLanguage** are system APIs and can be called only by system applications with the **UPDATE_CONFIGURATION** permission.) 173 Call **getPreferredLanguageList** to obtain the preferred language list. 174 Call **getFirstPreferredLanguage** to obtain the first preferred language in the preferred language list. 175 Call **getAppPreferredLanguageList** to obtain the preferred language of the application. It is the first language that matches the application resource in the preferred language list. 176 177 ```js 178 try { 179 I18n.System.addPreferredLanguage("en-GB", 0); // Set the first language in the preferred language list to en-GB. 180 let list = I18n.System.getPreferredLanguageList(); // Obtain the preferred language list. Example: list = ["en-GB", ...] 181 I18n.System.removePreferredLanguage(list.length - 1); // Remove the last preferred language from the preferred language list. 182 let firstPreferredLanguage = I18n.System.getFirstPreferredLanguage(); // firstPreferredLanguage = "en-GB" 183 let appPreferredLanguage = I18n.System.getAppPreferredLanguage(); // Set the preferred language of the application to en-GB if the application contains en-GB resources. 184 } catch(error) { 185 console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); 186 } 187 ``` 188 18912. Obtain and set the local digit switch. 190 191 Call **setUsingLocalDigit** to enable the local digit switch. (This is a system API and can be called only by system applications with the UPDATE_CONFIGURATION permission.) 192 Call **getUsingLocalDigit** to check whether the local digit switch is enabled. 193 Currently, use of local digits is supported only for the following languages: **ar**, **as**, **bn**, **fa**, **mr**, **my**, **ne**, **ur**. 194 195```js 196try { 197 I18n.System.setUsingLocalDigit(true); // Enable the local digit switch. 198 let status = I18n.System.getUsingLocalDigit(); // status = true 199} catch(error) { 200 console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); 201} 202``` 203 204## Obtain the calendar information. 205 206[Calendar](../reference/apis/js-apis-i18n.md#calendar8) provides APIs to obtain calendar information, for example, localized representation of the calendar, the start day of a week, and the minimum number of days in the first week of a year. 207 208### Available APIs 209 210| Class | API | Description | 211| --------- | ---------------------------------------- | --------------------- | 212| | getCalendar(locale:string,type?:string):Calendar<sup>8+</sup> | Obtains the **Calendar** object for a specific locale and type.| 213| Calendar | setTime(date:Date): void<sup>8+</sup> | Sets the date for this **Calendar** object. | 214| Calendar | setTime(time:number): void<sup>8+</sup> | Sets the date for this **Calendar** object. | 215| Calendar | set(year:number,month:number,date:number,hour?:number,minute?:number,second?:number): void<sup>8+</sup> | Sets the year, month, day, hour, minute, and second for this **Calendar** object. | 216| Calendar | setTimeZone(timezone:string): void<sup>8+</sup> | Sets the time zone of this **Calendar** object. | 217| Calendar | getTimeZone():string<sup>8+</sup> | Obtains the time zone of this **Calendar** object. | 218| Calendar | getFirstDayOfWeek():number<sup>8+</sup> | Obtains the start day of a week for this **Calendar** object. | 219| Calendar | setFirstDayOfWeek(value:number): void<sup>8+</sup> | Sets the first day of a week for the **Calendar** object. | 220| Calendar | getMinimalDaysInFirstWeek():number<sup>8+</sup> | Obtains the minimum number of days in the first week of a year. | 221| Calendar | setMinimalDaysInFirstWeek(value:number): void<sup>8+</sup> | Sets the minimum number of days in the first week of a year. | 222| Calendar | getDisplayName(locale:string):string<sup>8+</sup> | Obtains the localized display of the **Calendar** object. | 223| Calendar | isWeekend(date?:Date):boolean<sup>8+</sup> | Checks whether a given date is a weekend in the calendar. | 224 225### How to Develop 226 2271. Import the **i18n** module. 228 229 ```js 230 import I18n from '@ohos.i18n'; 231 ``` 232 2332. Instantiate a **Calendar** object. 234 235 Call **getCalendar** to obtain the time zone object of a specific locale and type (**i18n** is the name of the imported module). **type** indicates the valid calendar type, for example, **buddhist**, **chinese**, **coptic**, **ethiopic**, **hebrew**, **gregory**, **indian**, **islamic_civil**, **islamic_tbla**, **islamic_umalqura**, **japanese**, and **persian**. If **type** is left unspecified, the default calendar type of the locale is used. 236 237 ```js 238 let calendar = I18n.getCalendar("zh-CN", "chinese"); // Create the Calendar object for the Chinese lunar calendar. 239 ``` 240 2413. Set the time for the **Calendar** object. 242 243 Call **setTime** to set the time of the **Calendar** object. Two types of parameters are supported. One is a **Date** object, and the other is a value indicating the number of milliseconds elapsed since January 1, 1970, 00:00:00 GMT. 244 245 ```js 246 let date1 = new Date(); 247 calendar.setTime(date1); 248 let date2 = 1000; 249 calendar.setTime(date2); 250 ``` 251 2524. Set the year, month, day, hour, minute, and second for this **Calendar** object. 253 254 Call **set** to set the year, month, day, hour, minute, and second for the **Calendar** object. 255 256 ```js 257 calendar.set(2021, 12, 21, 6, 0, 0); 258 ``` 259 2605. Set and obtain the time zone for the **Calendar** object. 261 262 Call **setTimeZone** and **getTimeZone** to set and obtain the time zone of the **Calendar** object. Note that **setTimeZone** requires an input string to indicate the time zone to be set. 263 264 ```js 265 calendar.setTimeZone("Asia/Shanghai"); 266 let timezone = calendar.getTimeZone(); // timezone = "China Standard Time" 267 ``` 268 2696. Set and obtain the first day of a week for the **Calendar** object. 270 271 Call **setFirstDayOfWeek** and **getFirstDayOfWeek** to set and obtain the start day of a week for the **Calendar** object. **setFirstDayOfWeek** must be set to a value indicating the first day of a week. The value **1** indicates Sunday, and the value **7** indicates Saturday. 272 273 ```js 274 calendar.setFirstDayOfWeek(1); 275 let firstDayOfWeek = calendar.getFirstDayOfWeek(); // firstDayOfWeek = 1 276 ``` 277 2787. Set and obtain the minimum count of days in the first week for the **Calendar** object. 279 Call **setMinimalDaysInFirstWeek** and **getMinimalDaysInFirstWeek** to set and obtain the minimum number of days in the first week for the **Calendar** object. 280 281 ```js 282 calendar.setMinimalDaysInFirstWeek(3); 283 let minimalDaysInFirstWeek = calendar.getMinimalDaysInFirstWeek(); // minimalDaysInFirstWeek = 3 284 ``` 285 2868. Obtain the localized representation of the **Calendar** object. 287 Call **getDisplayName** to obtain the localized representation of the **Calendar** object. 288 289 ```js 290 let localizedName = calendar.getDisplayName("zh-CN"); // localizedName = " Lunar Calendar" 291 ``` 292 2939. Check whether a date is a weekend. 294 295 Call **isWeekend** to determine whether the input date is a weekend. 296 297 ```js 298 let date = new Date(2022, 12, 12, 12, 12, 12); 299 let weekend = calendar.isWeekend(date); // weekend = false 300 ``` 301 302## Formatting a Phone Number 303 304[PhoneNumberFormat](../reference/apis/js-apis-i18n.md#phonenumberformat8) provides APIs to format phone numbers of different countries or regions and check whether the phone number format is correct. 305 306### Available APIs 307 308| Class | API | Description | 309| --------- | ---------------------------------------- | ----------------------- | 310| PhoneNumberFormat | constructor(country:string,options?:PhoneNumberFormatOptions)<sup>8+</sup> | Instantiates a **PhoneNumberFormat** object.| 311| PhoneNumberFormat | isValidNumber(number:string):boolean<sup>8+</sup> | Checks whether the value of **number** is a phone number in the correct format.| 312| PhoneNumberFormat | format(number:string):string<sup>8+</sup> | Formats the value of **number** based on the specified country and style. | 313| PhoneNumberFormat | getLocationName(number: string, locale: string): string<sup>9+</sup> | Obtains the home location of a phone number. | 314 315### How to Develop 316 3171. Import the **i18n** module. 318 319 ```js 320 import I18n from '@ohos.i18n'; 321 ``` 322 3232. Instantiate a **PhoneNumberFormat** object. 324 325 Call the **PhoneNumberFormat** constructor to instantiate a **PhoneNumberFormat** object. The country code and formatting options of the phone number need to be passed into this constructor. The formatting options are optional, including a style option. Values of this option include: **E164**, **INTERNATIONAL**, **NATIONAL**, and **RFC3966**. 326 327 ```js 328 let phoneNumberFormat = new I18n.PhoneNumberFormat("CN", {type: "E164"}); 329 ``` 330 3313. Check whether the phone number format is correct. 332 333 Call **isValidNumber** to check whether the format of the input phone number is correct. 334 335 ```js 336 let validNumber = phoneNumberFormat.isValidNumber("15812341234"); // validNumber = true 337 ``` 338 3394. Format a phone number. 340 341 Call **format** to format the input phone number. 342 343 ```js 344 let formattedNumber = phoneNumberFormat.format("15812341234"); // formattedNumber = "+8615812341234" 345 ``` 346 347## Measurement Conversion 348 349The **I18NUtil** class provides an API to implement measurement conversion. 350 351### Available APIs 352 353| Class | API | Description | 354| --------- | ---------------------------------------- | --------------------------------------- | 355| I18NUtil | unitConvert(fromUnit:UnitInfo,toUnit:UnitInfo,value:number,locale:string,style?:string):string<sup>9+</sup> | Converts one measurement unit into another and formats the unit based on the specified locale and style.| 356 357### How to Develop 358 3591. Import the **i18n** module. 360 361 ```js 362 import I18n from '@ohos.i18n'; 363 ``` 364 3652. Convert a measurement unit. 366 367 Call [unitConvert](../reference/apis/js-apis-i18n.md#unitconvert9) to convert a measurement unit and format the display result. 368 369 ```js 370 let fromUnit = {unit: "cup", measureSystem: "US"}; 371 let toUnit = {unit: "liter", measureSystem: "SI"}; 372 let number = 1000; 373 let locale = "en-US"; 374 let style = "long"; 375 let converttedUnit = I18n.I18NUtil.unitConvert(fromUnit, toUnit, number, locale, style); // converttedUnit = "236.588 liters" 376 ``` 377 378## Alphabet Indexing 379 380[IndexUtil](../reference/apis/js-apis-i18n.md#indexutil8) provides APIs to obtain the alphabet indexes of different locales and calculate the index to which a string belongs. 381 382### Available APIs 383 384| Class | API | Description | 385| --------- | ---------------------------------------- | ----------------------- | 386| | getInstance(locale?:string):IndexUtil<sup>8+</sup> | Instantiates an **IndexUtil** object. | 387| IndexUtil | getIndexList():Array<string><sup>8+</sup> | Obtains the index list of the current locale. | 388| IndexUtil | addLocale(locale:string): void<sup>8+</sup> | Adds the index of a new locale to the index list.| 389| IndexUtil | getIndex(text:string):string<sup>8+</sup> | Obtains the index of a text object. | 390 391### How to Develop 392 3931. Import the **i18n** module. 394 395 ```js 396 import I18n from '@ohos.i18n'; 397 ``` 398 3992. Instantiates an **IndexUtil** object. 400 401 Call **getInstance** to instantiate an **IndexUtil** object for a specific locale. When the **locale** parameter is empty, instantiate an **IndexUtil** object of the default locale. 402 403 404 ```js 405 let indexUtil = I18n.getInstance("zh-CN"); 406 ``` 407 4083. Obtain the index list. 409 410 Call **getIndexList** to obtain the alphabet index list of the current locale. 411 412 ```js 413 let indexList = indexUtil.getIndexList(); // indexList = ["...", "A", "B", "C", ..., "X", "Y", "Z", "..."] 414 ``` 415 4164. Add an index. 417 418 Call **addLocale** to add the alphabet index of a new locale to the current index list. 419 420 ```js 421 indexUtil.addLocale("ar"); 422 ``` 423 4245. Obtain the index of a string. 425 426 Call **getIndex** to obtain the alphabet index of a string. 427 428 ```js 429 let text = "access index"; 430 let index = indexUtil.getIndex(text); // index = "A" 431 ``` 432 433## Obtaining Line Breaks of Text 434 435When a text is displayed in more than one line, use [BreakIterator8](../reference/apis/js-apis-i18n.md#breakiterator8) APIs to obtain the line break positions of the text. 436 437### Available APIs 438 439| Class | API | Description | 440| --------- | ---------------------------------------- | ------------------------------ | 441| | getLineInstance(locale:string):BreakIterator<sup>8+</sup> | Instantiates a **BreakIterator** object. | 442| BreakIterator | setLineBreakText(text:string): void<sup>8+</sup> | Sets the text to be processed. | 443| BreakIterator | getLineBreakText():string<sup>8+</sup> | Obtains the text to be processed. | 444| BreakIterator | current():number<sup>8+</sup> | Obtains the current position of a **BreakIterator** object in the text being processed. | 445| BreakIterator | first():number<sup>8+</sup> | Sets a **BreakIterator** object to the first breakable point. | 446| BreakIterator | last():number<sup>8+</sup> | Sets a **BreakIterator** object to the last breakable point. | 447| BreakIterator | next(index?:number):number<sup>8+</sup> | Moves a **BreakIterator** object to the break point according to **index**. | 448| BreakIterator | previous():number<sup>8+</sup> | Moves a **BreakIterator** object to the previous break point. | 449| BreakIterator | following(offset:number):number<sup>8+</sup> | Moves a **BreakIterator** object to the break point following the position specified by **offset**.| 450| BreakIterator | isBoundary(offset:number):boolean<sup>8+</sup> | Determines whether a position is a break point. | 451 452### How to Develop 453 4541. Import the **i18n** module. 455 456 ```js 457 import I18n from '@ohos.i18n'; 458 ``` 459 4602. Instantiate a **BreakIterator** object. 461 462 Call **getLineInstance** to instantiate a **BreakIterator** object. 463 464 ```js 465 let locale = "en-US"; 466 let breakIterator = I18n.getLineInstance(locale); 467 ``` 468 4693. Set and access the text that requires line breaking. 470 471 Call **setLineBreakText** and **getLineBreakText** to set and access the text that requires line breaking. 472 473 ```js 474 let text = "Apple is my favorite fruit"; 475 breakIterator.setLineBreakText(text); 476 let breakText = breakIterator.getLineBreakText(); // breakText = "Apple is my favorite fruit" 477 ``` 478 4794. Obtain the current position of the **BreakIterator** object. 480 481 Call **current** to obtain the current position of the **BreakIterator** object in the text being processed. 482 483 ```js 484 let pos = breakIterator.current(); // pos = 0 485 ``` 486 4875. Set the position of a **BreakIterator** object. 488 489 The following APIs are provided to adjust the **first**, **last**, **next**, **previous**, or **following** position of the **BreakIterator** object in the text to be processed. 490 491 ```js 492 let firstPos = breakIterator.first(); // Set a BreakIterator object to the first break point, that is, the start position of the text (firstPos = 0). 493 let lastPos = breakIterator.last(); // Sets a BreakIterator object to the last break point, that is, the position after the text end (lastPos = 26). 494 // Move a BreakIterator object forward or backward by a certain number of break points. 495 // If a positive number is input, move backward. If a negative number is input, move forward. If no value is input, move one position backward. 496 // If the object is moved out of the text length range, **-1** is returned. 497 let nextPos = breakIterator.next(-2); // nextPos = 12 498 let previousPos = breakIterator.previous(); // Move a BreakIterator object to the previous break point. When the text length is out of the range, **-1** is returned. Example: previousPos = 9 499 // Move a BreakIterator object to the break point following the position specified by **offset**. If the object is moved out of the text length range, **-1** is returned. 500 let followingPos = breakIterator.following(10); // Example: followingPos = 12 501 ``` 502 5036. Determine whether a position is a break point. 504 505 Call **isBoundary** to determine whether a position is a break point. If yes, **true** is returned and the **BreakIterator** object is moved to this position. If no, **false** is returned and the **BreakIterator** object is moved to a break point after this position. 506 507 ```js 508 let isboundary = breakIterator.isBoundary(5); // isboundary = false 509 ``` 510 511## Obtaining the Time Zone 512 513[TimeZone](../reference/apis/js-apis-i18n.md#timezone) provides APIs to obtain time zone information, such as the time zone ID, localized representation, and time zone offset. 514 515### Available APIs 516 517| Class | API | Description | 518| --------- | ---------------------------------------- | ------------------------------ | 519| | getTimeZone(zoneID?: string): TimeZone<sup>7+</sup> | Obtains a **TimeZone** object. | 520| TimeZone | getID(): string<sup>7+</sup> | Obtains the time zone ID. | 521| TimeZone | getDisplayName(locale?: string, isDST?: boolean): string<sup>7+</sup> | Obtains the localized representation of the time zone. | 522| TimeZone | getRawOffset(): number<sup>7+</sup> | Obtains the offset between the time zone represented by a **TimeZone** object and the UTC time zone. | 523| TimeZone | getOffset(date?: number): number<sup>7+</sup> | Obtains the offset between the time zone represented by a **TimeZone** object and the UTC time zone at a certain time point. | 524| TimeZone | getAvailableIDs(): Array<string><sup>9+</sup> | Obtains the list of time zone IDs supported by the system. | 525| TimeZone | getAvailableZoneCityIDs(): Array<string><sup>9+</sup> | Obtains the list of time zone city IDs supported by the system. | 526| TimeZone | getCityDisplayName(cityID: string, locale: string): string<sup>9+</sup> | Obtains the localized representation of a time zone city in the specified locale. | 527| TimeZone | getTimezoneFromCity(cityID: string): TimeZone<sup>9+</sup> | Obtains the **TimeZone** object corresponding to the specified time zone ID.| 528 529### How to Develop 530 5311. Import the **i18n** module. 532 533 ```js 534 import I18n from '@ohos.i18n'; 535 ``` 536 5372. Instantiate the **TimeZone** object, and obtain the time zone information. 538 539 Call **getTimeZone** to obtain the **TimeZone** object. 540 541 ```js 542 let timezone = I18n.getTimeZone(); // If you use the default settings, you'll obtain the TimeZone object corresponding to the system time zone. 543 ``` 544 545 Obtain the time zone ID, localized representation, time zone offset, and time zone offset at a certain time point. 546 547 ```js 548 let timezoneID = timezone.getID(); // timezoneID = "Asia/Shanghai" 549 let timezoneDisplayName = timezone.getDisplayName(); // timezoneDisplayName = "China Standard Time" 550 let rawOffset = timezone.getRawOffset(); // rawOffset = 28800000 551 let offset = timezone.getOffset(new Date().getTime()); // offset = 28800000 552 ``` 553 5543. Obtain the list of time zone IDs supported by the system. 555 556 Call **getAvailableIDs** to obtain the list of time zone IDs supported by the system. 557 You can use the time zone ID in the time zone ID list as an input parameter of the **getTimeZone** API to create a **TimeZone** object. 558 559 ```js 560 let timezoneIDs = I18n.TimeZone.getAvailableIDs(); // timezoneIDs = ["America/Adak", ...], which contains 24 time zone IDs in total 561 let timezone = I18n.getTimeZone(timezoneIDs[0]); 562 let timezoneDisplayName = timezone.getDisplayName(); // timezoneDisplayName = "Hawaii-Aleutian Standard Time" 563 ``` 564 5654. Obtain the list of time zone city IDs supported by the system. 566 567 Call **getAvailableZoneCityIDs** to obtain the list of time zone city IDs supported by the system. 568 Call **getCityDisplayName** to obtain the localized representation of the time zone city ID. 569 Call **getTimezoneFromCity** to create a **TimeZone** object based on the time zone city ID. 570 571 ```js 572 let zoneCityIDs = I18n.TimeZone.getAvailableZoneCityIDs(); // ["Auckland", "Magadan", ...] 573 let cityDisplayName = I18n.TimeZone.getCityDisplayName(zoneCityIDs[0], "zh-Hans"); // cityDisplayName = "Auckland (New Zealand)" 574 let timezone = I18n.TimeZone.getTimezoneFromCity(zoneCityIDs[0]); 575 let timezoneDisplayName = timezone.getDisplayName(); // timezoneDisplayName = "New Zealand Standard Time" 576 ``` 577 578## Obtain the **Transliterator** object. 579 580Call [Transliterator](../reference/apis/js-apis-i18n.md#transliterator9) APIs to create a **Transliterator** object and obtain the transliterated string. 581 582### Available APIs 583 584| Class | API | Description | 585| --------- | ---------------------------------------- | ------------------------------ | 586| Transliterator | getAvailableIDs():Array<string><sup>9+</sup> | Obtains the transliterator ID list. | 587| Transliterator | getInstance(): Transliterator<sup>9+</sup> | Creates a **Transliterator** object. | 588| Transliterator | transform(text: string): string<sup>9+</sup> | Obtains a transliterated string. | 589 590### How to Develop 591 5921. Import the **i18n** module. 593 594 ```js 595 import I18n from '@ohos.i18n'; 596 ``` 597 5982. Obtains the transliterator ID list. 599 600 Call **getAvailableIDs** to obtain the transliterator ID list. 601 An ID is in the **source-destination** format. For example, **ASCII-Latin** means to convert the transliterator ID from ASCII to Latin. 602 603 ```js 604 let ids = I18n.Transliterator.getAvailableIDs(); // ids = ["ASCII-Latin", "Accents-Any", ... ], 671 languages in total 605 ``` 606 6073. Create a **Transliterator** object, and obtain the transliterated string. 608 609 You can use the ID in the transliterator ID list as an input parameter of the **getInstance** API to create a **Transliterator** object. 610 Call **transform** to obtain the transliterated string. 611 612 ```js 613 let transliterator = I18n.Transliterator.getInstance("Any-Latn"); // Any-Latn means to convert any text to Latn text. 614 let transformText = transliterator.transform ("Hello"); // transformText = "nǐ hǎo" 615 ``` 616 617## Obtaining the Character Type 618 619[Unicode](../reference/apis/js-apis-i18n.md#unicode9) provides APIs to obtain character information, for example, whether a character is a digit or a space. 620 621### Available APIs 622 623| Class | API | Description | 624| --------- | ---------------------------------------- | ------------------------------ | 625| Unicode | isDigit(char: string): boolean<sup>9+</sup> | Checks whether the input character is a digit. | 626| Unicode | isSpaceChar(char: string): boolean<sup>9+</sup> | Checks whether the input character is a space. | 627| Unicode | isWhitespace(char: string): boolean<sup>9+</sup> | Checks whether the input character is a white space. | 628| Unicode | isRTL(char: string): boolean<sup>9+</sup> | Checks whether the input character is of the RTL language. | 629| Unicode | isIdeograph(char: string): boolean<sup>9+</sup> | Checks whether the input character is an ideographic character. | 630| Unicode | isLetter(char: string): boolean<string><sup>9+</sup> | Checks whether the input character is a letter. | 631| Unicode | isLowerCase(char: string): boolean<string><sup>9+</sup> | Checks whether the input character is a lowercase letter. | 632| Unicode | isUpperCase(char: string): boolean<sup>9+</sup> | Checks whether the input character is an uppercase letter. | 633| Unicode | getType(char: string): string<sup>9+</sup> | Obtains the type of a character.| 634 635### How to Develop 636 6371. Import the **i18n** module. 638 639 ```js 640 import I18n from '@ohos.i18n'; 641 ``` 642 6432. Check the input character has a certain attribute. 644 645 Check whether the input character is a digit. 646 647 ```js 648 let isDigit = I18n.Unicode.isDigit("1"); // isDigit = true 649 isDigit = I18n.Unicode.isDigit("a"); // isDigit = false 650 ``` 651 652 Check whether the input character is a space. 653 654 ```js 655 let isSpaceChar = I18n.Unicode.isSpaceChar(" "); // isSpaceChar = true 656 isSpaceChar = I18n.Unicode.isSpaceChar("\n"); // isSpaceChar = false 657 ``` 658 659 Check whether the input character is a white space. 660 661 ```js 662 let isWhitespace = I18n.Unicode.isWhitespace(" "); // isWhitespace = true 663 isWhitespace = I18n.Unicode.isWhitespace("\n"); // isWhitespace = true 664 ``` 665 666 Check whether the input character is of the RTL language. 667 668 ```js 669 let isRTL = I18n.Unicode.isRTL(""); // isRTL = true (Arabic characters are written from left to right.) 670 isRTL = I18n.Unicode.isRTL("a"); // isRTL = false 671 ``` 672 673 Check whether the input character is an ideographic character. 674 675 ```js 676 let isIdeograph = I18n.Unicode.isIdeograph("Hello"); // isIdeograph = true 677 isIdeograph = I18n.Unicode.isIdeograph("a"); // isIdeograph = false 678 ``` 679 680 Check whether the input character is a letter. 681 682 ```js 683 let isLetter = I18n.Unicode.isLetter("a"); // isLetter = true 684 isLetter = I18n.Unicode.isLetter("."); // isLetter = false 685 ``` 686 687 Check whether the input character is a lowercase letter. 688 689 ```js 690 let isLowerCase = I18n.Unicode.isLowerCase("a"); // isLetter = true 691 isLowerCase = I18n.Unicode.isLowerCase("A"); // isLetter = false 692 ``` 693 694 Check whether the input character is an uppercase letter. 695 696 ```js 697 let isUpperCase = I18n.Unicode.isUpperCase("a"); // isUpperCase = false 698 isUpperCase = I18n.Unicode.isUpperCase("A"); // isUpperCase = true 699 ``` 700 7013. Obtain the character type. 702 703 Call **getType** to obtain the character type. 704 705 ```js 706 let type = I18n.Unicode.getType("a"); // type = U_LOWER_CASE_LETTER 707 ``` 708 709## Obtaining the Sequence of Year, Month, and Day in a Date 710 711### Available APIs 712 713| Class | API | Description | 714| --------- | ---------------------------------------- | ------------------------------ | 715| I18NUtil | getDateOrder(locale: string): string<sup>9+</sup> | Checks the sequence of year, month, and day in a date. | 716 717### How to Develop 718 7191. Import the **i18n** module. 720 721 ```js 722 import I18n from '@ohos.i18n'; 723 ``` 724 7252. Check the sequence of year, month, and day in a date. 726 727 Call **getDateOrder** to obtain the sequence of year, month, and day in the date of the specified locale. 728 The API returns a string consisting of three parts, **y**, **L**, and **d**, which indicate the year, month, and day, respectively. The three parts are separated by using a hyphen (-), for example, **y-L-d**. 729 730 ```js 731 let order = I18n.I18NUtil.getDateOrder("zh-CN"); // order = "y-L-d" indicates that the sequence of year, month, and day in Chinese is year-month-day. 732 ``` 733