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 ```ts 41 import I18n from '@ohos.i18n'; 42 ``` 43 442. Obtain and set the system language. 45 46 Call [setSystemLanguage](../reference/apis/js-apis-i18n.md#setsystemlanguage9) 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 [getSystemLanguage](../reference/apis/js-apis-i18n.md#getsystemlanguages9) to obtain the system language. 48 49 ```ts 50 import { BusinessError } from '@ohos.base'; 51 52 try { 53 I18n.System.setSystemLanguage("en"); // Set the system language to en. 54 let language = I18n.System.getSystemLanguage(); // language = "en" 55 } catch(error) { 56 let err: BusinessError = error as BusinessError; 57 console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); 58 } 59 ``` 60 613. Obtain and set the system locale. 62 63 Call [setSystemRegion](../reference/apis/js-apis-i18n.md#getsystemregion9) 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.) 64 Call [getSystemRegion](../reference/apis/js-apis-i18n.md#setsystemregion9) to obtain the system country or region. 65 66 ```ts 67 import { BusinessError } from '@ohos.base'; 68 69 try { 70 I18n.System.setSystemRegion("CN"); // Set the system country to CN. 71 let region = I18n.System.getSystemRegion(); // region = "CN" 72 } catch(error) { 73 let err: BusinessError = error as BusinessError; 74 console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); 75 } 76 ``` 77 784. Obtain and set the system locale. 79 80 Call [setSystemLocale](../reference/apis/js-apis-i18n.md#setsystemlocale9) 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). 81 Call [getSystemLocale](../reference/apis/js-apis-i18n.md#getsystemlocale9) to obtain the system locale. 82 83 ```ts 84 import { BusinessError } from '@ohos.base'; 85 86 try { 87 I18n.System.setSystemLocale("zh-Hans-CN"); // Set the system locale to zh-Hans-CN. 88 let locale = I18n.System.getSystemLocale(); // locale = "zh-Hans-CN" 89 } catch(error) { 90 let err: BusinessError = error as BusinessError; 91 console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); 92 } 93 ``` 94 955. Check whether the locale uses a right-to-left (RTL) language. 96 97 Call [isRTL](../reference/apis/js-apis-i18n.md#i18nisrtl7) interface to check whether the locale uses an RTL language. 98 99 ```ts 100 import { BusinessError } from '@ohos.base'; 101 102 try { 103 let rtl = I18n.isRTL("zh-CN"); // rtl = false 104 rtl = I18n.isRTL("ar"); // rtl = true 105 } catch(error) { 106 let err: BusinessError = error as BusinessError; 107 console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); 108 } 109 ``` 110 1116. Obtain and set the 24-hour clock. 112 113 Call [set24HourClock](../reference/apis/js-apis-i18n.md#set24hourclock9) to enable the 24-hour clock. (This is a system API and can be called only by system applications with the **UPDATE_CONFIGURATION** permission.) 114 Call [is24HourClock](../reference/apis/js-apis-i18n.md#is24hourclock9) to check whether the 24-hour clock is enabled. 115 116 ```ts 117 import { BusinessError } from '@ohos.base'; 118 119 try { 120 I18n.System.set24HourClock(true); 121 let hourClock = I18n.System.is24HourClock(); // hourClock = true 122 } catch(error) { 123 let err: BusinessError = error as BusinessError; 124 console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); 125 } 126 ``` 127 1287. Obtain the localized representation of a language. 129 130 Call [getDisplayLanguage](../reference/apis/js-apis-i18n.md#getdisplaylanguage9) 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. 131 132 ```ts 133 import { BusinessError } from '@ohos.base'; 134 135 try { 136 let language = "en"; 137 let locale = "zh-CN"; 138 let sentenceCase = false; 139 let localizedLanguage = I18n.System.getDisplayLanguage(language, locale, sentenceCase); // localizedLanguage = "English" 140 } catch(error) { 141 let err: BusinessError = error as BusinessError; 142 console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); 143 } 144 ``` 145 1468. Obtain the localized representation of a country. 147 148 Call [getDisplayCountry](../reference/apis/js-apis-i18n.md#getdisplaycountry9) 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. 149 150 ```ts 151 import { BusinessError } from '@ohos.base'; 152 153 try { 154 let country = "US"; 155 let locale = "zh-CN"; 156 let sentenceCase = false; 157 let localizedCountry = I18n.System.getDisplayCountry(country, locale, sentenceCase); // localizedCountry = "U.S." 158 } catch(error) { 159 let err: BusinessError = error as BusinessError; 160 console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); 161 } 162 ``` 163 1649. Obtain the list of system languages and the list of countries supported by a system language. 165 166 Call [getSystemLanguages](../reference/apis/js-apis-i18n.md#getsystemlanguages9) to obtain the list of system languages. 167 Call [getSystemCountries](../reference/apis/js-apis-i18n.md#getsystemcountries9) to obtain the list of countries and regions supported by a system language. 168 ```ts 169 import { BusinessError } from '@ohos.base'; 170 171 try { 172 let languageList = I18n.System.getSystemLanguages(); // languageList = ["en-Latn-US", "zh-Hans"] 173 let countryList = I18n.System.getSystemCountries("zh"); // countryList = ["ZW", "YT", ..., "CN", "DE"], 240 countries and regions in total 174 } catch(error) { 175 let err: BusinessError = error as BusinessError; 176 console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); 177 } 178 ``` 179 18010. Check whether the language matches a country or region. 181 182 Call [isSuggested](../reference/apis/js-apis-i18n.md#issuggested9) to check whether the language matches a country or region. 183 184 ```ts 185 import { BusinessError } from '@ohos.base'; 186 187 try { 188 let isSuggest = I18n.System.isSuggested("zh", "CN"); // isSuggest = true 189 } catch(error) { 190 let err: BusinessError = error as BusinessError; 191 console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); 192 } 193 ``` 194 19511. Obtain and set the preferred language. 196 197 Call [addPreferredLanguage](../reference/apis/js-apis-i18n.md#addpreferredlanguage9) to add a preferred language to the preferred language list. 198 Call [removePreferredLanguage](../reference/apis/js-apis-i18n.md#removepreferredlanguage9) 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.) 199 Call [getPreferredLanguageList](../reference/apis/js-apis-i18n.md#getpreferredlanguagelist9) to obtain the preferred language list. 200 Call [getFirstPreferredLanguage](../reference/apis/js-apis-i18n.md#getfirstpreferredlanguage9) to obtain the first preferred language in the preferred language list. 201 Call [getAppPreferredLanguage](../reference/apis/js-apis-i18n.md#getapppreferredlanguage9) to obtain the preferred language of the application. It is the first language that matches the application resource in the preferred language list. 202 203 ```ts 204 import { BusinessError } from '@ohos.base'; 205 206 try { 207 I18n.System.addPreferredLanguage("en-GB", 0); // Set the first language in the preferred language list to en-GB. 208 let list = I18n.System.getPreferredLanguageList(); // Obtain the preferred language list. Example: list = ["en-GB", ...] 209 I18n.System.removePreferredLanguage(list.length - 1); // Remove the last preferred language from the preferred language list. 210 let firstPreferredLanguage = I18n.System.getFirstPreferredLanguage(); // firstPreferredLanguage = "en-GB" 211 let appPreferredLanguage = I18n.System.getAppPreferredLanguage(); // Set the preferred language of the application to en-GB if the application contains en-GB resources. 212 } catch(error) { 213 let err: BusinessError = error as BusinessError; 214 console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); 215 } 216 ``` 217 21812. Obtain and set the local digit switch. 219 220 Call [setUsingLocalDigit](../reference/apis/js-apis-i18n.md#setusinglocaldigit9) to enable the local digit switch. (This is a system API and can be called only by system applications with the UPDATE_CONFIGURATION permission.) 221 Call [getUsingLocalDigit](../reference/apis/js-apis-i18n.md#getusinglocaldigit9) to check whether the local digit switch is enabled. 222 Currently, use of local digits is supported only for the following languages: **ar**, **as**, **bn**, **fa**, **mr**, **my**, **ne**, **ur**. 223 224```ts 225import { BusinessError } from '@ohos.base'; 226 227try { 228 I18n.System.setUsingLocalDigit(true); // Enable the local digit switch. 229 let status = I18n.System.getUsingLocalDigit(); // status = true 230} catch(error) { 231 let err: BusinessError = error as BusinessError; 232 console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); 233} 234``` 235 236## Obtaining Calendar Information 237 238[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. 239 240### Available APIs 241 242| Class | API | Description | 243| --------- | ---------------------------------------- | --------------------- | 244| | getCalendar(locale:string,type?:string):Calendar<sup>8+</sup> | Obtains the **Calendar** object for a specific locale and type.| 245| Calendar | setTime(date:Date): void<sup>8+</sup> | Sets the date for this **Calendar** object. | 246| Calendar | setTime(time:number): void<sup>8+</sup> | Sets the date for this **Calendar** object. | 247| 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. | 248| Calendar | setTimeZone(timezone:string): void<sup>8+</sup> | Sets the time zone of this **Calendar** object. | 249| Calendar | getTimeZone():string<sup>8+</sup> | Obtains the time zone of this **Calendar** object. | 250| Calendar | getFirstDayOfWeek():number<sup>8+</sup> | Obtains the start day of a week for this **Calendar** object. | 251| Calendar | setFirstDayOfWeek(value:number): void<sup>8+</sup> | Sets the first day of a week for the **Calendar** object. | 252| Calendar | getMinimalDaysInFirstWeek():number<sup>8+</sup> | Obtains the minimum number of days in the first week of a year. | 253| Calendar | setMinimalDaysInFirstWeek(value:number): void<sup>8+</sup> | Sets the minimum number of days in the first week of a year. | 254| Calendar | getDisplayName(locale:string):string<sup>8+</sup> | Obtains the localized display of the **Calendar** object. | 255| Calendar | isWeekend(date?:Date):boolean<sup>8+</sup> | Checks whether a given date is a weekend in the calendar. | 256 257### How to Develop 258 2591. Import the **i18n** module. 260 261 ```ts 262 import I18n from '@ohos.i18n'; 263 ``` 264 2652. Instantiate a **Calendar** object. 266 267 Call [getCalendar](../reference/apis/js-apis-i18n.md#i18ngetcalendar8) 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. 268 269 ```ts 270 let calendar = I18n.getCalendar("zh-CN", "chinese"); // Create the Calendar object for the Chinese lunar calendar. 271 ``` 272 2733. Set the time for the **Calendar** object. 274 275 Call [setTime](../reference/apis/js-apis-i18n.md#settime8) 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. 276 277 ```ts 278 let date1 = new Date(); 279 calendar.setTime(date1); 280 let date2 = 1000; 281 calendar.setTime(date2); 282 ``` 283 2844. Set the year, month, day, hour, minute, and second for this **Calendar** object. 285 286 Call [set](../reference/apis/js-apis-i18n.md#set8) to set the year, month, day, hour, minute, and second for the **Calendar** object. 287 288 ```ts 289 calendar.set(2021, 12, 21, 6, 0, 0); 290 ``` 291 2925. Set and obtain the time zone for the **Calendar** object. 293 294 Call [setTimeZone](../reference/apis/js-apis-i18n.md#settimezone8) and [getTimeZone](../reference/apis/js-apis-i18n.md#gettimezone8) respectively 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. 295 296 ```ts 297 calendar.setTimeZone("Asia/Shanghai"); 298 let timezone = calendar.getTimeZone(); // timezone = "China Standard Time" 299 ``` 300 3016. Set and obtain the first day of a week for the **Calendar** object. 302 303 Call [setFirstDayOfWeek](../reference/apis/js-apis-i18n.md#setfirstdayofweek8) and [getFirstDayOfWeek](../reference/apis/js-apis-i18n.md#getfirstdayofweek8) respectively 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. 304 305 ```ts 306 calendar.setFirstDayOfWeek(1); 307 let firstDayOfWeek = calendar.getFirstDayOfWeek(); // firstDayOfWeek = 1 308 ``` 309 3107. Set and obtain the minimum count of days in the first week for the **Calendar** object. 311 312 Call [setMinimalDaysInFirstWeek](../reference/apis/js-apis-i18n.md#setminimaldaysinfirstweek8) and [getMinimalDaysInFirstWeek](../reference/apis/js-apis-i18n.md#getminimaldaysinfirstweek8) to set and obtain the minimum number of days in the first week for the **Calendar** object. 313 314 ```ts 315 calendar.setMinimalDaysInFirstWeek(3); 316 let minimalDaysInFirstWeek = calendar.getMinimalDaysInFirstWeek(); // minimalDaysInFirstWeek = 3 317 ``` 318 3198. Obtain the localized representation of the **Calendar** object. 320 321 Call [getDisplayName](../reference/apis/js-apis-i18n.md#getdisplayname8) to obtain the localized display of the calendar object. 322 323 ```ts 324 let localizedName = calendar.getDisplayName("zh-CN"); // localizedName = " Lunar Calendar" 325 ``` 326 3279. Check whether a date is a weekend. 328 329 Call [isWeekend](../reference/apis/js-apis-i18n.md#isweekend8) to check whether the input date is a weekend. 330 331 ```ts 332 let date = new Date(2022, 12, 12, 12, 12, 12); 333 let weekend = calendar.isWeekend(date); // weekend = false 334 ``` 335 336## Formatting a Phone Number 337 338 [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. 339 340### Available APIs 341 342| Class | API | Description | 343| --------- | ---------------------------------------- | ----------------------- | 344| PhoneNumberFormat | constructor(country:string,options?:PhoneNumberFormatOptions)<sup>8+</sup> | Instantiates a **PhoneNumberFormat** object.| 345| PhoneNumberFormat | isValidNumber(number:string):boolean<sup>8+</sup> | Checks whether the value of **number** is a phone number in the correct format.| 346| PhoneNumberFormat | format(number:string):string<sup>8+</sup> | Formats the value of **number** based on the specified country and style. | 347| PhoneNumberFormat | getLocationName(number: string, locale: string): string<sup>9+</sup> | Obtains the home location of a phone number. | 348 349### How to Develop 350 3511. Import the **i18n** module. 352 353 ```ts 354 import I18n from '@ohos.i18n'; 355 ``` 356 3572. Instantiate a **PhoneNumberFormat** object. 358 359 Call the constructor used to instantiate a [PhoneNumberFormat](../reference/apis/js-apis-i18n.md#phonenumberformat8) object. You need to pass the country code and formatting options of the phone number into this constructor. The formatting options are optional, including a style option. Values of this option include: **E164**, **INTERNATIONAL**, **NATIONAL**, and **RFC3966**. 360 361 ```ts 362 let phoneNumberFormat = new I18n.PhoneNumberFormat("CN", {type: "E164"}); 363 ``` 364 3653. Check whether the phone number format is correct. 366 367 Call [isValidNumber](../reference/apis/js-apis-i18n.md#isvalidnumber8) to check whether the format of the input phone number is correct. 368 369 ```ts 370 let validNumber = phoneNumberFormat.isValidNumber("123****8911"); // validNumber = true 371 ``` 372 3734. Format a phone number. 374 375 Call [format](../reference/apis/js-apis-i18n.md#format8) to format the input phone number. 376 377 ```ts 378 let formattedNumber = phoneNumberFormat.format("123****8911"); // formattedNumber = "+86123****8911" 379 ``` 380 381## Measurement Conversion 382 383**I18NUtil** provides an API to implement measurement conversion. 384 385### Available APIs 386 387| Class | API | Description | 388| --------- | ---------------------------------------- | --------------------------------------- | 389| 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.| 390 391### How to Develop 392 3931. Import the **i18n** module. 394 395 ```ts 396 import I18n from '@ohos.i18n'; 397 ``` 398 3992. Convert a measurement unit. 400 401 Call [unitConvert](../reference/apis/js-apis-i18n.md#unitconvert9) to convert a measurement unit and format the display result. 402 403 ```ts 404 let fromUnit: I18n.UnitInfo = {unit: "cup", measureSystem: "US"}; 405 let toUnit: I18n.UnitInfo = {unit: "liter", measureSystem: "SI"}; 406 let number = 1000; 407 let locale = "en-US"; 408 let style = "long"; 409 let converttedUnit = I18n.I18NUtil.unitConvert(fromUnit, toUnit, number, locale, style); // converttedUnit = "236.588 liters" 410 ``` 411 412## Alphabet Indexing 413 414[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. 415 416### Available APIs 417 418| Class | API | Description | 419| --------- | ---------------------------------------- | ----------------------- | 420| | getInstance(locale?:string):IndexUtil<sup>8+</sup> | Instantiates an **IndexUtil** object. | 421| IndexUtil | getIndexList():Array<string><sup>8+</sup> | Obtains the index list of the current locale. | 422| IndexUtil | addLocale(locale:string): void<sup>8+</sup> | Adds the index of a new locale to the index list.| 423| IndexUtil | getIndex(text:string):string<sup>8+</sup> | Obtains the index of a text object. | 424 425### How to Develop 426 4271. Import the **i18n** module. 428 429 ```ts 430 import I18n from '@ohos.i18n'; 431 ``` 432 4332. Instantiates an **IndexUtil** object. 434 435 Call [getInstance](../reference/apis/js-apis-i18n.md#getinstance8) to instantiate an **IndexUtil** object for a specific locale. When the **locale** parameter is empty, instantiate an **IndexUtil** object of the default locale. 436 437 ```ts 438 let indexUtil = I18n.getInstance("zh-CN"); 439 ``` 440 4413. Obtain the index list. 442 443 Call [getIndexList](../reference/apis/js-apis-i18n.md#getindexlist8) to obtain the alphabet index list of the current locale. 444 445 ```ts 446 let indexList = indexUtil.getIndexList(); // indexList = ["...", "A", "B", "C", ..., "X", "Y", "Z", "..."] 447 ``` 448 4494. Add an index. 450 451 Call [addLocale](../reference/apis/js-apis-i18n.md#addlocale8) to add the alphabet index of a new locale to the current index list. 452 453 ```ts 454 indexUtil.addLocale("ar"); 455 ``` 456 4575. Obtain the index of a string. 458 459 Call [getIndex](../reference/apis/js-apis-i18n.md#getindex8) to obtain the alphabet index of a string. 460 461 ```ts 462 let text = "access index"; 463 let index = indexUtil.getIndex(text); // index = "A" 464 ``` 465 466## Obtaining Line Breaks of Text 467 468When 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. 469 470### Available APIs 471 472| Class | API | Description | 473| --------- | ---------------------------------------- | ------------------------------ | 474| | getLineInstance(locale:string):BreakIterator<sup>8+</sup> | Instantiates a **BreakIterator** object. | 475| BreakIterator | setLineBreakText(text:string): void<sup>8+</sup> | Sets the text to be processed. | 476| BreakIterator | getLineBreakText():string<sup>8+</sup> | Obtains the text to be processed. | 477| BreakIterator | current():number<sup>8+</sup> | Obtains the current position of a **BreakIterator** object in the text being processed. | 478| BreakIterator | first():number<sup>8+</sup> | Sets a **BreakIterator** object to the first breakable point. | 479| BreakIterator | last():number<sup>8+</sup> | Sets a **BreakIterator** object to the last breakable point. | 480| BreakIterator | next(index?:number):number<sup>8+</sup> | Moves a **BreakIterator** object to the break point according to **index**. | 481| BreakIterator | previous():number<sup>8+</sup> | Moves a **BreakIterator** object to the previous break point. | 482| BreakIterator | following(offset:number):number<sup>8+</sup> | Moves a **BreakIterator** object to the break point following the position specified by **offset**.| 483| BreakIterator | isBoundary(offset:number):boolean<sup>8+</sup> | Determines whether a position is a break point. | 484 485### How to Develop 486 4871. Import the **i18n** module. 488 489 ```ts 490 import I18n from '@ohos.i18n'; 491 ``` 492 4932. Instantiate a **BreakIterator** object. 494 495 Call [getLineInstance](../reference/apis/js-apis-i18n.md#i18ngetlineinstance8) to instantiate a **BreakIterator** object. 496 497 ```ts 498 let locale = "en-US"; 499 let breakIterator = I18n.getLineInstance(locale); 500 ``` 501 5023. Set and access the text that requires line breaking. 503 504 Call [setLineBreakText](../reference/apis/js-apis-i18n.md#setlinebreaktext8) and [getLineBreakText](../reference/apis/js-apis-i18n.md#getlinebreaktext8) respectively to set and access the text that requires line breaking. 505 506 ```ts 507 let text = "Apple is my favorite fruit"; 508 breakIterator.setLineBreakText(text); 509 let breakText = breakIterator.getLineBreakText(); // breakText = "Apple is my favorite fruit" 510 ``` 511 5124. Obtain the current position of the **BreakIterator** object. 513 514 Call [current](../reference/apis/js-apis-i18n.md#current8) to obtain the current position of the **BreakIterator** object in the text being processed. 515 516 ```ts 517 let pos = breakIterator.current(); // pos = 0 518 ``` 519 5205. Set the position of a **BreakIterator** object. 521 522 Call [first](../reference/apis/js-apis-i18n.md#first8), [last](../reference/apis/js-apis-i18n.md#last8), [next](../reference/apis/js-apis-i18n.md#next8), [previous](../reference/apis/js-apis-i18n.md#previous8), or [following](../reference/apis/js-apis-i18n.md#following8) as needed to adjust the the position of the **BreakIterator** object in the text. 523 524 ```ts 525 let firstPos = breakIterator.first(); // Set a BreakIterator object to the first break point, that is, the start position of the text (firstPos = 0). 526 let lastPos = breakIterator.last(); // Sets a BreakIterator object to the last break point, that is, the position after the text end (lastPos = 26). 527 // Move a BreakIterator object forward or backward by a certain number of break points. 528 // 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. 529 // If the object is moved out of the text length range, **-1** is returned. 530 let nextPos = breakIterator.next(-2); // nextPos = 12 531 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 532 // 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. 533 let followingPos = breakIterator.following(10); // Example: followingPos = 12 534 ``` 535 5366. Determine whether a position is a break point. 537 538 Call [isBoundary](../reference/apis/js-apis-i18n.md#isboundary8) to check 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. 539 540 ```ts 541 let isboundary = breakIterator.isBoundary(5); // isboundary = false 542 ``` 543 544## Obtaining the Time Zone 545 546[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. 547 548### Available APIs 549 550| Class | API | Description | 551| --------- | ---------------------------------------- | ------------------------------ | 552| | getTimeZone(zoneID?: string): TimeZone<sup>7+</sup> | Obtains a **TimeZone** object. | 553| TimeZone | getID(): string<sup>7+</sup> | Obtains the time zone ID. | 554| TimeZone | getDisplayName(locale?: string, isDST?: boolean): string<sup>7+</sup> | Obtains the localized representation of the time zone. | 555| TimeZone | getRawOffset(): number<sup>7+</sup> | Obtains the offset between the time zone represented by a **TimeZone** object and the UTC time zone. | 556| 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. | 557| TimeZone | getAvailableIDs(): Array<string><sup>9+</sup> | Obtains the list of time zone IDs supported by the system. | 558| TimeZone | getAvailableZoneCityIDs(): Array<string><sup>9+</sup> | Obtains the list of time zone city IDs supported by the system. | 559| TimeZone | getCityDisplayName(cityID: string, locale: string): string<sup>9+</sup> | Obtains the localized representation of a time zone city in the specified locale. | 560| TimeZone | getTimezoneFromCity(cityID: string): TimeZone<sup>9+</sup> | Obtains the **TimeZone** object corresponding to the specified time zone ID.| 561 562### How to Develop 563 5641. Import the **i18n** module. 565 566 ```ts 567 import I18n from '@ohos.i18n'; 568 ``` 569 5702. Instantiate the **TimeZone** object, and obtain the time zone information. 571 572 Call [getTimeZone](../reference/apis/js-apis-i18n.md#i18ngettimezone7) to obtain the **TimeZone** object. 573 574 ```ts 575 let timezone = I18n.getTimeZone(); // If you use the default settings, you'll obtain the TimeZone object corresponding to the system time zone. 576 ``` 577 578 Obtain the time zone ID, localized representation, time zone offset, and time zone offset at a certain time point. 579 580 ```ts 581 let timezoneID = timezone.getID(); // timezoneID = "Asia/Shanghai" 582 let timezoneDisplayName = timezone.getDisplayName(); // timezoneDisplayName = "China Standard Time" 583 let rawOffset = timezone.getRawOffset(); // rawOffset = 28800000 584 let offset = timezone.getOffset(new Date().getTime()); // offset = 28800000 585 ``` 586 5873. Obtain the list of time zone IDs supported by the system. 588 589 Call [getAvailableIDs](../reference/apis/js-apis-i18n.md#getavailableids9) to obtain the list of time zone IDs supported by the system. 590 You can use an ID in the ID list of **TimeZone** objects as an input parameter of **getTimeZone** to create a **TimeZone** object. 591 592 ```ts 593 let timezoneIDs = I18n.TimeZone.getAvailableIDs(); // timezoneIDs = ["America/Adak", ...], which contains 24 time zone IDs in total 594 let timezone = I18n.getTimeZone(timezoneIDs[0]); 595 let timezoneDisplayName = timezone.getDisplayName(); // timezoneDisplayName = "Hawaii-Aleutian Standard Time" 596 ``` 597 5984. Obtain the list of time zone city IDs supported by the system. 599 600 Call [getAvailableZoneCityIDs](../reference/apis/js-apis-i18n.md#getavailablezonecityids9) to obtain the list of time zone city IDs supported by the system. 601 Call [getCityDisplayName](../reference/apis/js-apis-i18n.md#getcitydisplayname9) to obtain the localized representation of the time zone city ID. 602 Call [getTimezoneFromCity](../reference/apis/js-apis-i18n.md#gettimezonefromcity9) to create a **TimeZone** object based on the time zone city ID. 603 604 ```ts 605 let zoneCityIDs = I18n.TimeZone.getAvailableZoneCityIDs(); // ["Auckland", "Magadan", ...] 606 let cityDisplayName = I18n.TimeZone.getCityDisplayName(zoneCityIDs[0], "zh-Hans"); // cityDisplayName = "Auckland (New Zealand)" 607 let timezone = I18n.TimeZone.getTimezoneFromCity(zoneCityIDs[0]); 608 let timezoneDisplayName = timezone.getDisplayName(); // timezoneDisplayName = "New Zealand Standard Time" 609 ``` 610 611## Obtaining the Transliterator Object 612 613Call [Transliterator](../reference/apis/js-apis-i18n.md#transliterator9) APIs to create a **Transliterator** object and obtain the transliterated string. 614 615### Available APIs 616 617| Class | API | Description | 618| --------- | ---------------------------------------- | ------------------------------ | 619| Transliterator | getAvailableIDs():Array<string><sup>9+</sup> | Obtains the ID list of **Transliterator** objects. | 620| Transliterator | getInstance(): Transliterator<sup>9+</sup> | Creates a **Transliterator** object. | 621| Transliterator | transform(text: string): string<sup>9+</sup> | Obtains a transliterated string. | 622 623### How to Develop 624 6251. Import the **i18n** module. 626 627 ```ts 628 import I18n from '@ohos.i18n'; 629 ``` 630 6312. Obtain the ID list of **Transliterator** objects. 632 633 Call [getAvailableIDs](../reference/apis/js-apis-i18n.md#getavailableids9-1) to obtain the ID list of **Transliterator** objects. 634 An ID is in the **source-destination** format. For example, **ASCII-Latin** means to convert the ID from ASCII to Latin. 635 636 ```ts 637 let ids = I18n.Transliterator.getAvailableIDs(); // ids = ["ASCII-Latin", "Accents-Any", ... ], 671 languages in total 638 ``` 639 6403. Create a **Transliterator** object, and obtain the transliterated string. 641 642 You can use an ID in the ID list of **Transliterator** objects as an input parameter of [getInstance](../reference/apis/js-apis-i18n.md#getinstance9) to create a **Transliterator** object. 643 Call [transform](../reference/apis/js-apis-i18n.md#transform9) to obtain the transliterated string. 644 645 ```ts 646 let transliterator = I18n.Transliterator.getInstance("Any-Latin"); // Any-Latin means to convert any text to Latin text. 647 let transformText = transliterator.transform ("Hello"); // transformText = "nǐ hǎo" 648 let transliterator2 = I18n.Transliterator.getInstance("Latin-ASCII"); // Latin-ASCII means to convert Latin text to ASCII text. 649 transformText = transliterator2.transform(transformText); // transformText = "ni hao" 650 ``` 651 652## Obtaining the Character Type 653 654[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. 655 656### Available APIs 657 658| Class | API | Description | 659| --------- | ---------------------------------------- | ------------------------------ | 660| Unicode | isDigit(char: string): boolean<sup>9+</sup> | Checks whether the input character is a digit. | 661| Unicode | isSpaceChar(char: string): boolean<sup>9+</sup> | Checks whether the input character is a space. | 662| Unicode | isWhitespace(char: string): boolean<sup>9+</sup> | Checks whether the input character is a white space. | 663| Unicode | isRTL(char: string): boolean<sup>9+</sup> | Checks whether the input character is of the RTL language. | 664| Unicode | isIdeograph(char: string): boolean<sup>9+</sup> | Checks whether the input character is an ideographic character. | 665| Unicode | isLetter(char: string): boolean<string><sup>9+</sup> | Checks whether the input character is a letter. | 666| Unicode | isLowerCase(char: string): boolean<string><sup>9+</sup> | Checks whether the input character is a lowercase letter. | 667| Unicode | isUpperCase(char: string): boolean<sup>9+</sup> | Checks whether the input character is an uppercase letter. | 668| Unicode | getType(char: string): string<sup>9+</sup> | Obtains the type of a character.| 669 670### How to Develop 671 6721. Import the **i18n** module. 673 674 ```ts 675 import I18n from '@ohos.i18n'; 676 ``` 677 6782. Check the input character has a certain attribute. 679 680 Checks whether the input character is a digit. 681 682 ```ts 683 let isDigit = I18n.Unicode.isDigit("1"); // isDigit = true 684 isDigit = I18n.Unicode.isDigit("a"); // isDigit = false 685 ``` 686 687 Checks whether the input character is a space. 688 689 ```ts 690 let isSpaceChar = I18n.Unicode.isSpaceChar(" "); // isSpaceChar = true 691 isSpaceChar = I18n.Unicode.isSpaceChar("\n"); // isSpaceChar = false 692 ``` 693 694 Checks whether the input character is a white space. 695 696 ```ts 697 let isWhitespace = I18n.Unicode.isWhitespace(" "); // isWhitespace = true 698 isWhitespace = I18n.Unicode.isWhitespace("\n"); // isWhitespace = true 699 ``` 700 701 Check whether the input character is of the RTL language. 702 703 ```ts 704 let isRTL = I18n.Unicode.isRTL(""); // isRTL = true (Arabic characters are written from left to right.) 705 isRTL = I18n.Unicode.isRTL("a"); // isRTL = false 706 ``` 707 708 Checks whether the input character is an ideographic character. 709 710 ```ts 711 let isIdeograph = I18n.Unicode.isIdeograph("Hello"); // isIdeograph = true 712 isIdeograph = I18n.Unicode.isIdeograph("a"); // isIdeograph = false 713 ``` 714 715 Checks whether the input character is a letter. 716 717 ```ts 718 let isLetter = I18n.Unicode.isLetter("a"); // isLetter = true 719 isLetter = I18n.Unicode.isLetter("."); // isLetter = false 720 ``` 721 722 Checks whether the input character is a lowercase letter. 723 724 ```ts 725 let isLowerCase = I18n.Unicode.isLowerCase("a"); // isLetter = true 726 isLowerCase = I18n.Unicode.isLowerCase("A"); // isLetter = false 727 ``` 728 729 Checks whether the input character is an uppercase letter. 730 731 ```ts 732 let isUpperCase = I18n.Unicode.isUpperCase("a"); // isUpperCase = false 733 isUpperCase = I18n.Unicode.isUpperCase("A"); // isUpperCase = true 734 ``` 735 7363. Obtain the character type. 737 738 Call [getType](../reference/apis/js-apis-i18n.md#gettype9) to obtain the character type. 739 740 ```ts 741 let type = I18n.Unicode.getType("a"); // type = U_LOWER_CASE_LETTER 742 ``` 743 744## Obtaining the Sequence of Year, Month, and Day in a Date 745 746### Available APIs 747 748| Class | API | Description | 749| --------- | ---------------------------------------- | ------------------------------ | 750| I18NUtil | getDateOrder(locale: string): string<sup>9+</sup> | Checks the sequence of year, month, and day in a date. | 751 752### How to Develop 753 7541. Import the **i18n** module. 755 756 ```ts 757 import I18n from '@ohos.i18n'; 758 ``` 759 7602. Check the sequence of year, month, and day in a date. 761 762 Call [getDateOrder](../reference/apis/js-apis-i18n.md#getdateorder9) to obtain the sequence of year, month, and day in the date of the specified locale. 763 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**. 764 765 ```ts 766 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. 767 ``` 768 769