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

S
shawn_he 已提交
3
> **NOTE**<br>
S
shawn_he 已提交
4
> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
S
shawn_he 已提交
5
>
S
shawn_he 已提交
6
> - This module contains enhanced i18n APIs, which are not defined in ECMA 402.
Z
zengyawen 已提交
7

S
shawn_he 已提交
8 9

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

```
import i18n from '@ohos.i18n';
```


S
shawn_he 已提交
16
## i18n.getDisplayLanguage
Z
zengyawen 已提交
17

S
shawn_he 已提交
18
getDisplayLanguage(language: string, locale: string, sentenceCase?: boolean): string
Z
zengyawen 已提交
19 20 21

Obtains the localized script for the specified language.

S
shawn_he 已提交
22 23
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
24 25 26 27 28 29
**Parameters**
| Name         | Type     | Mandatory  | Description              |
| ------------ | ------- | ---- | ---------------- |
| language     | string  | Yes   | Specified language.           |
| locale       | string  | Yes   | Locale ID.    |
| sentenceCase | boolean | No   | Whether to use sentence case for the localized script.|
S
shawn_he 已提交
30

S
shawn_he 已提交
31
**Return value**
S
shawn_he 已提交
32 33 34
| Type    | Description           |
| ------ | ------------- |
| string | Localized script for the specified language.|
S
shawn_he 已提交
35

S
shawn_he 已提交
36
**Example**
S
shawn_he 已提交
37 38 39 40 41 42 43 44 45
  ```
  i18n.getDisplayLanguage("zh", "en-GB", true);
  i18n.getDisplayLanguage("zh", "en-GB");
  ```


## i18n.getDisplayCountry

getDisplayCountry(country: string, locale: string, sentenceCase?: boolean): string
Z
zengyawen 已提交
46 47 48

Obtains the localized script for the specified country.

S
shawn_he 已提交
49 50
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
51 52 53 54 55 56
**Parameters**
| Name         | Type     | Mandatory  | Description              |
| ------------ | ------- | ---- | ---------------- |
| country      | string  | Yes   | Specified country.           |
| locale       | string  | Yes   | Locale ID.    |
| sentenceCase | boolean | No   | Whether to use sentence case for the localized script.|
S
shawn_he 已提交
57

S
shawn_he 已提交
58
**Return value**
S
shawn_he 已提交
59 60 61
| Type    | Description           |
| ------ | ------------- |
| string | Localized script for the specified country.|
S
shawn_he 已提交
62

S
shawn_he 已提交
63
**Example**
S
shawn_he 已提交
64 65 66 67 68 69
  ```
  i18n.getDisplayCountry("zh-CN", "en-GB", true);
  i18n.getDisplayCountry("zh-CN", "en-GB");
  ```


S
shawn_he 已提交
70
## i18n.isRTL<sup>7+</sup>
S
shawn_he 已提交
71 72

isRTL(locale: string): boolean
Z
zengyawen 已提交
73 74 75

Checks whether the localized script for the specified language is displayed from right to left.

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

S
shawn_he 已提交
78 79 80 81
**Parameters**
| Name   | Type    | Description     |
| ------ | ------ | ------- |
| locale | string | Locale ID.|
Z
zengyawen 已提交
82

S
shawn_he 已提交
83
**Return value**
S
shawn_he 已提交
84 85 86
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the localized script is displayed from right to left; returns **false** otherwise.|
Z
zengyawen 已提交
87

S
shawn_he 已提交
88
**Example**
S
shawn_he 已提交
89 90 91 92
  ```
  i18n.isRTL("zh-CN");// Since Chinese is not written from right to left, false is returned.
  i18n.isRTL("ar-EG");// Since Arabic is written from right to left, true is returned.
  ```
Z
zengyawen 已提交
93 94


S
shawn_he 已提交
95
## i18n.getSystemLanguage
Z
zengyawen 已提交
96

S
shawn_he 已提交
97
getSystemLanguage(): string
Z
zengyawen 已提交
98

S
shawn_he 已提交
99
Obtains the system language.
Z
zengyawen 已提交
100

S
shawn_he 已提交
101 102
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
103
**Return value**
S
shawn_he 已提交
104 105 106
| Type    | Description     |
| ------ | ------- |
| string | System language ID.|
S
shawn_he 已提交
107

S
shawn_he 已提交
108
**Example**
S
shawn_he 已提交
109 110 111
  ```
  i18n.getSystemLanguage();
  ```
Z
zengyawen 已提交
112 113


S
shawn_he 已提交
114 115 116 117 118 119
## i18n.setSystemLanguage

setSystemLanguage(language: string): boolean

Sets the system language.

S
shawn_he 已提交
120
**Permission required**: ohos.permission.UPDATE_CONFIGURATION
S
shawn_he 已提交
121

S
shawn_he 已提交
122 123
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
124 125
**System API**: This is a system API and cannot be called by third-party applications.

S
shawn_he 已提交
126 127 128 129
**Parameters**
| Name     | Type    | Description   |
| -------- | ------ | ----- |
| language | string | Language ID.|
S
shawn_he 已提交
130

S
shawn_he 已提交
131
**Return value**
S
shawn_he 已提交
132 133 134
| Type     | Description                                   |
| ------- | ------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
S
shawn_he 已提交
135

S
shawn_he 已提交
136
**Example**
S
shawn_he 已提交
137 138 139 140 141 142 143
  ```
  i18n.setSystemLanguage('zh');
  ```


## i18n.getSystemLanguages

S
shawn_he 已提交
144
getSystemLanguages(): Array&lt;string&gt;
S
shawn_he 已提交
145 146 147 148 149

Obtains the list of system languages.

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

S
shawn_he 已提交
150 151 152
**System API**: This is a system API and cannot be called by third-party applications.

**Return value**
S
shawn_he 已提交
153 154 155
| Type                 | Description          |
| ------------------- | ------------ |
| Array&lt;string&gt; | List of the IDs of system languages.|
S
shawn_he 已提交
156

S
shawn_he 已提交
157
**Example**
S
shawn_he 已提交
158 159 160 161 162 163 164
  ```
  i18n.getSystemLanguages();
  ```


## i18n.getSystemCountries

S
shawn_he 已提交
165
getSystemCountries(language: string): Array&lt;string&gt;
S
shawn_he 已提交
166 167 168 169 170

Obtains the list of countries and regions supported for the specified language.

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

S
shawn_he 已提交
171 172
**System API**: This is a system API and cannot be called by third-party applications.

S
shawn_he 已提交
173 174 175 176
**Parameters**
| Name     | Type    | Description   |
| -------- | ------ | ----- |
| language | string | Language ID.|
S
shawn_he 已提交
177

S
shawn_he 已提交
178
**Return value**
S
shawn_he 已提交
179 180 181
| Type                 | Description          |
| ------------------- | ------------ |
| Array&lt;string&gt; | List of the IDs of the countries and regions supported for the specified language.|
S
shawn_he 已提交
182

S
shawn_he 已提交
183
**Example**
S
shawn_he 已提交
184 185 186 187 188
  ```
  i18n.getSystemCountries('zh');
  ```


S
shawn_he 已提交
189
## i18n.getSystemRegion
Z
zengyawen 已提交
190

S
shawn_he 已提交
191
getSystemRegion(): string
Z
zengyawen 已提交
192

S
shawn_he 已提交
193 194 195 196
Obtains the system region.

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

S
shawn_he 已提交
197
**Return value**
S
shawn_he 已提交
198 199 200
| Type    | Description     |
| ------ | ------- |
| string | System region ID.|
Z
zengyawen 已提交
201

S
shawn_he 已提交
202
**Example**
S
shawn_he 已提交
203 204 205
  ```
  i18n.getSystemRegion();
  ```
Z
zengyawen 已提交
206 207


S
shawn_he 已提交
208 209 210 211 212 213
## i18n.setSystemRegion

setSystemRegion(region: string): boolean

Sets the system region.

S
shawn_he 已提交
214
**Permission required**: ohos.permission.UPDATE_CONFIGURATION
S
shawn_he 已提交
215

S
shawn_he 已提交
216 217
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
218 219
**System API**: This is a system API and cannot be called by third-party applications.

S
shawn_he 已提交
220 221 222 223
**Parameters**
| Name   | Type    | Description   |
| ------ | ------ | ----- |
| region | string | Region ID.|
S
shawn_he 已提交
224

S
shawn_he 已提交
225
**Return value**
S
shawn_he 已提交
226 227 228
| Type     | Description                                   |
| ------- | ------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
S
shawn_he 已提交
229

S
shawn_he 已提交
230
**Example**
S
shawn_he 已提交
231
  ```
S
shawn_he 已提交
232
  i18n.setSystemRegion('CN');
S
shawn_he 已提交
233 234 235
  ```


S
shawn_he 已提交
236
## i18n.getSystemLocale
Z
zengyawen 已提交
237

S
shawn_he 已提交
238
getSystemLocale(): string
Z
zengyawen 已提交
239 240 241

Obtains the system locale.

S
shawn_he 已提交
242
**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
243

S
shawn_he 已提交
244
**Return value**
S
shawn_he 已提交
245 246 247
| Type    | Description     |
| ------ | ------- |
| string | System locale ID.|
Z
zengyawen 已提交
248

S
shawn_he 已提交
249
**Example**
S
shawn_he 已提交
250 251 252
  ```
  i18n.getSystemLocale();
  ```
Z
zengyawen 已提交
253 254


S
shawn_he 已提交
255 256 257 258 259 260
## i18n.setSystemLocale

setSystemLocale(locale: string): boolean

Sets the system locale.

S
shawn_he 已提交
261
**Permission required**: ohos.permission.UPDATE_CONFIGURATION
S
shawn_he 已提交
262

S
shawn_he 已提交
263 264
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
265 266
**System API**: This is a system API and cannot be called by third-party applications.

S
shawn_he 已提交
267 268 269 270
**Parameters**
| Name   | Type    | Description             |
| ------ | ------ | --------------- |
| locale | string | System locale ID, for example, **zh-CN**.|
S
shawn_he 已提交
271

S
shawn_he 已提交
272
**Return value**
S
shawn_he 已提交
273 274 275
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
S
shawn_he 已提交
276

S
shawn_he 已提交
277
**Example**
S
shawn_he 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290
  ```
  i18n.setSystemLocale('zh-CN');
  ```


## i18n.isSuggested

isSuggested(language: string, region?: string): boolean

Checks whether the system language matches the specified region.

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

S
shawn_he 已提交
291 292
**System API**: This is a system API and cannot be called by third-party applications.

S
shawn_he 已提交
293 294 295 296 297
**Parameters**
| Name     | Type    | Mandatory  | Description           |
| -------- | ------ | ---- | ------------- |
| language | string | Yes   | Valid language ID, for example, **zh**.|
| region   | string | No   | Valid region ID, for example, **CN**. |
S
shawn_he 已提交
298

S
shawn_he 已提交
299
**Return value**
S
shawn_he 已提交
300 301 302
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the system language matches the specified region; returns **false** otherwise.|
S
shawn_he 已提交
303

S
shawn_he 已提交
304
**Example**
S
shawn_he 已提交
305 306 307 308 309
  ```
  i18n.isSuggested('zh', 'CN');
  ```


S
shawn_he 已提交
310
## i18n.getCalendar<sup>8+</sup>
Z
zengyawen 已提交
311

S
shawn_he 已提交
312
getCalendar(locale: string, type? : string): Calendar
Z
zengyawen 已提交
313

S
shawn_he 已提交
314
Obtains a **Calendar** object.
Z
zengyawen 已提交
315

S
shawn_he 已提交
316
**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
317

S
shawn_he 已提交
318 319 320 321 322
**Parameters**
| Name   | Type    | Mandatory  | Description                                      |
| ------ | ------ | ---- | ---------------------------------------- |
| locale | string | Yes   | Valid locale value, for example, **zh-Hans-CN**.                |
| type   | string | No   | Valid calendar type. Currently, the valid types are as follows: **buddhist**, **chinese**, **coptic**, **ethiopic**, **hebrew**, **gregory**, **indian**, **islamic\_civil**, **islamic\_tbla**, **islamic\_umalqura**, **japanese**, and **persian**. If this parameter is left unspecified, the default calendar type of the specified locale is used.|
S
shawn_he 已提交
323

S
shawn_he 已提交
324
**Return value**
S
shawn_he 已提交
325 326 327
| Type                    | Description   |
| ---------------------- | ----- |
| [Calendar](#calendar8) | **Calendar** object.|
S
shawn_he 已提交
328

S
shawn_he 已提交
329
**Example**
S
shawn_he 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
  ```
  i18n.getCalendar("zh-Hans", "gregory");
  ```


## Calendar<sup>8+</sup>


### setTime<sup>8+</sup>

setTime(date: Date): void

Sets the date for this **Calendar** object.

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

S
shawn_he 已提交
346 347 348 349
**Parameters**
| Name | Type  | Mandatory  | Description               |
| ---- | ---- | ---- | ----------------- |
| date | Date | Yes   | Date to be set for the **Calendar** object.|
S
shawn_he 已提交
350

S
shawn_he 已提交
351
**Example**
S
shawn_he 已提交
352
  ```
S
shawn_he 已提交
353
  var calendar = i18n.getCalendar("en-US", "gregory");
S
shawn_he 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366
  var date = new Date(2021, 10, 7, 8, 0, 0, 0);
  calendar.setTime(date);
  ```


### setTime<sup>8+</sup>

setTime(time: number): void

Sets the date and time for this **Calendar** object. The value is represented by the number of milliseconds that have elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970).

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

S
shawn_he 已提交
367 368 369 370
**Parameters**
| Name | Type    | Mandatory  | Description                                      |
| ---- | ------ | ---- | ---------------------------------------- |
| time | number | Yes   | Number of milliseconds that have elapsed since the Unix epoch.|
S
shawn_he 已提交
371

S
shawn_he 已提交
372
**Example**
S
shawn_he 已提交
373
  ```
S
shawn_he 已提交
374
  var calendar = i18n.getCalendar("en-US", "gregory");
S
shawn_he 已提交
375 376 377 378 379 380 381 382 383 384 385 386
  calendar.setTime(10540800000);
  ```


### set<sup>8+</sup>

set(year: number, month: number, date:number, hour?: number, minute?: number, second?: number): void

Sets the year, month, day, hour, minute, and second for this **Calendar** object.

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

S
shawn_he 已提交
387 388 389 390 391 392 393 394 395
**Parameters**
| Name   | Type    | Mandatory  | Description    |
| ------ | ------ | ---- | ------ |
| year   | number | Yes   | Year to set. |
| month  | number | Yes   | Month to set. |
| date   | number | Yes   | Day to set. |
| hour   | number | No   | Hour to set.|
| minute | number | No   | Minute to set.|
| second | number | No   | Second to set. |
S
shawn_he 已提交
396

S
shawn_he 已提交
397
**Example**
S
shawn_he 已提交
398 399
  ```
  var calendar = i18n.getCalendar("zh-Hans");
S
shawn_he 已提交
400
  calendar.set(2021, 10, 1, 8, 0, 0); // set time to 2021.10.1 08:00:00
S
shawn_he 已提交
401 402 403 404 405 406 407 408 409 410 411
  ```


### setTimeZone<sup>8+</sup>

setTimeZone(timezone: string): void

Sets the time zone of this **Calendar** object.

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

S
shawn_he 已提交
412 413 414 415
**Parameters**
| Name     | Type    | Mandatory  | Description                       |
| -------- | ------ | ---- | ------------------------- |
| timezone | string | Yes   | Time zone, for example, **Asia/Shanghai**.|
S
shawn_he 已提交
416

S
shawn_he 已提交
417
**Example**
S
shawn_he 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431
  ```
  var calendar = i18n.getCalendar("zh-Hans");
  calendar.setTimeZone("Asia/Shanghai");
  ```


### getTimeZone<sup>8+</sup>

getTimeZone(): string

Obtains the time zone of this **Calendar** object.

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

S
shawn_he 已提交
432
**Return value**
S
shawn_he 已提交
433 434 435
| Type    | Description        |
| ------ | ---------- |
| string | Time zone of the **Calendar** object.|
S
shawn_he 已提交
436

S
shawn_he 已提交
437
**Example**
S
shawn_he 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
  ```
  var calendar = i18n.getCalendar("zh-Hans");
  calendar.setTimeZone("Asia/Shanghai");
  calendar.getTimeZone(); // Asia/Shanghai"
  ```


### getFirstDayOfWeek<sup>8+</sup>

getFirstDayOfWeek(): number

Obtains the start day of a week for this **Calendar** object.

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

S
shawn_he 已提交
453
**Return value**
S
shawn_he 已提交
454 455 456
| Type    | Description                   |
| ------ | --------------------- |
| number | Start day of a week. The value **1** indicates Sunday, and the value **7** indicates Saturday.|
S
shawn_he 已提交
457

S
shawn_he 已提交
458
**Example**
S
shawn_he 已提交
459
  ```
S
shawn_he 已提交
460
  var calendar = i18n.getCalendar("en-US", "gregory");
S
shawn_he 已提交
461 462 463 464 465 466 467 468 469 470 471 472
  calendar.getFirstDayOfWeek();
  ```


### setFirstDayOfWeek<sup>8+</sup>

setFirstDayOfWeek(value: number): void

Sets the start day of a week for this **Calendar** object.

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

S
shawn_he 已提交
473 474 475 476
**Parameters**
| Name  | Type    | Mandatory  | Description                   |
| ----- | ------ | ---- | --------------------- |
| value | number | No   | Start day of a week. The value **1** indicates Sunday, and the value **7** indicates Saturday.|
Z
zengyawen 已提交
477

S
shawn_he 已提交
478
**Example**
S
shawn_he 已提交
479 480 481 482
  ```
  var calendar = i18n.getCalendar("zh-Hans");
  calendar.setFirstDayOfWeek(0);
  ```
Z
zengyawen 已提交
483 484


S
shawn_he 已提交
485
### getMinimalDaysInFirstWeek<sup>8+</sup>
Z
zengyawen 已提交
486

S
shawn_he 已提交
487 488 489 490 491 492
getMinimalDaysInFirstWeek(): number

Obtains the minimum number of days in the first week of a year.

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

S
shawn_he 已提交
493
**Return value**
S
shawn_he 已提交
494 495 496
| Type    | Description          |
| ------ | ------------ |
| number | Minimum number of days in the first week of a year.|
S
shawn_he 已提交
497

S
shawn_he 已提交
498
**Example**
S
shawn_he 已提交
499 500 501 502
  ```
  var calendar = i18n.getCalendar("zh-Hans");
  calendar.getMinimalDaysInFirstWeek();
  ```
Z
zengyawen 已提交
503 504


S
shawn_he 已提交
505
### setMinimalDaysInFirstWeek<sup>8+</sup>
Z
zengyawen 已提交
506

S
shawn_he 已提交
507
setMinimalDaysInFirstWeek(value: number): void
Z
zengyawen 已提交
508 509 510

Sets the minimum number of days in the first week of a year.

S
shawn_he 已提交
511 512
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
513 514 515 516
**Parameters**
| Name  | Type    | Mandatory  | Description          |
| ----- | ------ | ---- | ------------ |
| value | number | No   | Minimum number of days in the first week of a year.|
S
shawn_he 已提交
517

S
shawn_he 已提交
518
**Example**
S
shawn_he 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532
  ```
  var calendar = i18n.getCalendar("zh-Hans");
  calendar.setMinimalDaysInFirstWeek(3);
  ```


### get<sup>8+</sup>

get(field: string): number

Obtains the value of the specified field in the **Calendar** object.

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

S
shawn_he 已提交
533 534 535 536
**Parameters**
| Name  | Type    | Mandatory  | Description                                      |
| ----- | ------ | ---- | ---------------------------------------- |
| field | string | Yes   | Value of the specified field in the **Calendar** object. Currently, a valid field can be any of the following: **era**, **year**, **month**, **week\_of\_year**, **week\_of\_month**, **date**, **day\_of\_year**, **day\_of\_week**, **day\_of\_week\_in\_month**, **hour**, **hour\_of\_day**, **minute**, **second**, **millisecond**, **zone\_offset**, **dst\_offset**, **year\_woy**, **dow\_local**, **extended\_year**, **julian\_day**, **milliseconds\_in\_day**, **is\_leap\_month**.|
S
shawn_he 已提交
537

S
shawn_he 已提交
538
**Return value**
S
shawn_he 已提交
539 540 541
| Type    | Description                                      |
| ------ | ---------------------------------------- |
| number | Value of the specified field. For example, if the year in the internal date of this **Calendar** object is **1990**, the **get("year")** function will return **1990**.|
S
shawn_he 已提交
542

S
shawn_he 已提交
543
**Example**
S
shawn_he 已提交
544 545
  ```
  var calendar = i18n.getCalendar("zh-Hans");
S
shawn_he 已提交
546
  calendar.set(2021, 10, 1, 8, 0, 0); // set time to 2021.10.1 08:00:00
S
shawn_he 已提交
547 548 549 550 551 552 553 554 555 556 557 558
  calendar.get("hour_of_day"); // 8
  ```


### getDisplayName<sup>8+</sup>

getDisplayName(locale: string): string

Obtains the name of the **Calendar** object displayed for the specified locale.

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

S
shawn_he 已提交
559 560 561 562
**Parameters**
| Name   | Type    | Mandatory  | Description                                      |
| ------ | ------ | ---- | ---------------------------------------- |
| locale | string | Yes   | Locale for which the name of the **Calendar** object is displayed. For example, if **locale** is **en-US**, the name of the Buddhist calendar will be **Buddhist Calendar**.|
S
shawn_he 已提交
563

S
shawn_he 已提交
564
**Return value**
S
shawn_he 已提交
565 566 567
| Type    | Description                 |
| ------ | ------------------- |
| string | Name of the **Calendar** object displayed for the specified locale.|
S
shawn_he 已提交
568

S
shawn_he 已提交
569
**Example**
S
shawn_he 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583
  ```
  var calendar = i18n.getCalendar("en-US", "buddhist");
  calendar.getDisplayName("zh"); // Obtain the name of the Buddhist calendar in zh.
  ```


### isWeekend<sup>8+</sup>

isWeekend(date?: Date): boolean

Checks whether the specified date in this **Calendar** object is a weekend.

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

S
shawn_he 已提交
584 585 586 587
**Parameters**
| Name | Type  | Mandatory  | Description                                      |
| ---- | ---- | ---- | ---------------------------------------- |
| date | Date | No   | Specified date in this **Calendar** object. If this parameter is left unspecified, the system checks whether the current date in the **Calendar** object is a weekend.|
S
shawn_he 已提交
588

S
shawn_he 已提交
589
**Return value**
S
shawn_he 已提交
590 591 592
| Type     | Description                                 |
| ------- | ----------------------------------- |
| boolean | Returns **true** if the date is a weekend; returns **false** if the date is a weekday.|
S
shawn_he 已提交
593

S
shawn_he 已提交
594
**Example**
S
shawn_he 已提交
595 596
  ```
  var calendar = i18n.getCalendar("zh-Hans");
S
shawn_he 已提交
597
  calendar.set(2021, 11, 11, 8, 0, 0); // set time to 2021.11.11 08:00:00
S
shawn_he 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
  calendar.isWeekend(); // false
  var date = new Date(2011, 11, 6, 9, 0, 0);
  calendar.isWeekend(date); // true
  ```


## PhoneNumberFormat<sup>8+</sup>


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

constructor(country: string, options?: PhoneNumberFormatOptions)

Creates a **PhoneNumberFormat** object.

**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
614 615

Parameters
S
shawn_he 已提交
616 617 618 619
| Name    | Type                                      | Mandatory  | Description              |
| ------- | ---------------------------------------- | ---- | ---------------- |
| country | string                                   | Yes   | Country or region to which the phone number to be formatted belongs.|
| options | [PhoneNumberFormatOptions](#phonenumberformatoptions8) | No   | Options of the **PhoneNumberFormat** object. |
Z
zengyawen 已提交
620

S
shawn_he 已提交
621
**Example**
S
shawn_he 已提交
622 623 624 625 626 627 628 629
  ```
  var phoneNumberFormat= new i18n.PhoneNumberFormat("CN", {"type": "E164"});
  ```


### isValidNumber<sup>8+</sup>

isValidNumber(number: string): boolean
Z
zengyawen 已提交
630 631 632

Checks whether the format of the specified phone number is valid.

S
shawn_he 已提交
633 634
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
635 636 637 638
**Parameters**
| Name   | Type    | Mandatory  | Description       |
| ------ | ------ | ---- | --------- |
| number | string | Yes   | Phone number to be checked.|
S
shawn_he 已提交
639

S
shawn_he 已提交
640
**Return value**
S
shawn_he 已提交
641 642 643
| Type     | Description                                   |
| ------- | ------------------------------------- |
| boolean | Returns **true** if the phone number format is valid; returns **false** otherwise.|
S
shawn_he 已提交
644

S
shawn_he 已提交
645
**Example**
S
shawn_he 已提交
646 647 648 649 650 651 652 653 654
  ```
  var phonenumberfmt = new i18n.PhoneNumberFormat("CN");
  phonenumberfmt.isValidNumber("15812312312");
  ```


### format<sup>8+</sup>

format(number: string): string
Z
zengyawen 已提交
655 656 657

Formats a phone number.

S
shawn_he 已提交
658 659
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
660 661 662 663
**Parameters**
| Name   | Type    | Mandatory  | Description        |
| ------ | ------ | ---- | ---------- |
| number | string | Yes   | Phone number to be formatted.|
S
shawn_he 已提交
664

S
shawn_he 已提交
665
**Return value**
S
shawn_he 已提交
666 667 668
| Type    | Description        |
| ------ | ---------- |
| string | Formatted phone number.|
S
shawn_he 已提交
669

S
shawn_he 已提交
670
**Example**
S
shawn_he 已提交
671 672 673 674 675 676 677 678 679 680
  ```
  var phonenumberfmt = new i18n.PhoneNumberFormat("CN");
  phonenumberfmt.format("15812312312");
  ```


## PhoneNumberFormatOptions<sup>8+</sup>

Defines the options for this PhoneNumberFormat object.

S
shawn_he 已提交
681
**System capability**: SystemCapability.Global.I18n
S
shawn_he 已提交
682

S
shawn_he 已提交
683 684
| Name  | Type  | Readable  | Writable  | Description                                      |
| ---- | ------ | ---- | ---- | ---------------------------------------- |
S
shawn_he 已提交
685
| type | string | Yes   | Yes   | Format type of a phone number. The available options are as follows: E164,&nbsp;INTERNATIONAL,&nbsp;NATIONAL, and&nbsp;RFC3966.|
S
shawn_he 已提交
686 687 688


## UnitInfo<sup>8+</sup>
Z
zengyawen 已提交
689 690 691

Defines the measurement unit information.

S
shawn_he 已提交
692
**System capability**: SystemCapability.Global.I18n
S
shawn_he 已提交
693

S
shawn_he 已提交
694 695 696
| Name           | Type  | Readable  | Writable  | Description                                      |
| ------------- | ------ | ---- | ---- | ---------------------------------------- |
| unit          | string | Yes   | Yes   | Name of the measurement unit, for example, **meter**, **inch**, or **cup**.|
S
shawn_he 已提交
697
| measureSystem | string | Yes   | Yes   | Measurement system. The value can be **SI**,&nbsp;**US**, or&nbsp;**UK**.|
S
shawn_he 已提交
698 699 700 701 702 703 704


## Util<sup>8+</sup>


### unitConvert<sup>8+</sup>

S
shawn_he 已提交
705
static unitConvert(fromUnit: UnitInfo, toUnit: UnitInfo, value: number, locale: string, style?: string): string
Z
zengyawen 已提交
706 707 708

Converts one measurement unit into another and formats the unit based on the specified locale and style.

S
shawn_he 已提交
709 710
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
711 712 713 714 715 716 717
**Parameters**
| Name     | Type                    | Mandatory  | Description                                      |
| -------- | ---------------------- | ---- | ---------------------------------------- |
| fromUnit | [UnitInfo](#unitinfo8) | Yes   | Measurement unit to be converted.                                |
| toUnit   | [UnitInfo](#unitinfo8) | Yes   | Measurement unit to be converted to.                                |
| value    | number                 | Yes   | Value of the measurement unit to be converted.                            |
| locale   | string                 | Yes   | Locale used for formatting, for example, **zh-Hans-CN**.               |
S
shawn_he 已提交
718
| style    | string                 | No   | Style used for formatting. The value can be **long**, **short**, or **narrow**.|
S
shawn_he 已提交
719

S
shawn_he 已提交
720
**Return value**
S
shawn_he 已提交
721 722 723
| Type    | Description                     |
| ------ | ----------------------- |
| string | Character string obtained after formatting based on the measurement unit specified by **toUnit**.|
S
shawn_he 已提交
724

S
shawn_he 已提交
725
**Example**
S
shawn_he 已提交
726
  ```
S
shawn_he 已提交
727
  i18n.Util.unitConvert({unit: "cup", measureSystem: "US"}, {unit: "liter", measureSystem: "SI"}, 1000, "en-US", "long");
S
shawn_he 已提交
728 729 730
  ```


S
shawn_he 已提交
731
## getInstance<sup>8+</sup>
S
shawn_he 已提交
732

S
shawn_he 已提交
733
getInstance(locale?:string): IndexUtil
S
shawn_he 已提交
734 735 736 737 738

Creates an **IndexUtil** object.

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

S
shawn_he 已提交
739 740 741 742
**Parameters**
| Name   | Type    | Mandatory  | Description                          |
| ------ | ------ | ---- | ---------------------------- |
| locale | string | No   | A string containing locale information, including the language, optional script, and region.|
S
shawn_he 已提交
743

S
shawn_he 已提交
744
**Return value**
S
shawn_he 已提交
745 746 747
| Type                      | Description                   |
| ------------------------ | --------------------- |
| [IndexUtil](#indexutil8) | **IndexUtil** object mapping to the specified locale.|
S
shawn_he 已提交
748

S
shawn_he 已提交
749
**Example**
S
shawn_he 已提交
750
  ```
S
shawn_he 已提交
751
  var indexUtil= i18n.getInstance("zh-CN");
S
shawn_he 已提交
752 753 754
  ```


S
shawn_he 已提交
755 756 757
## IndexUtil<sup>8+</sup>


S
shawn_he 已提交
758 759 760 761 762 763 764 765
### getIndexList<sup>8+</sup>

getIndexList(): Array&lt;string&gt;

Obtains the index list for this **locale** object.

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

S
shawn_he 已提交
766
**Return value**
S
shawn_he 已提交
767 768 769
| Type                 | Description                |
| ------------------- | ------------------ |
| Array&lt;string&gt; | Index list for this **locale** object.|
S
shawn_he 已提交
770

S
shawn_he 已提交
771
**Example**
S
shawn_he 已提交
772 773 774 775 776 777 778 779
  ```
  var indexUtil = i18n.getInstance("zh-CN");
  var indexList = indexUtil.getIndexList();
  ```


### addLocale<sup>8+</sup>

S
shawn_he 已提交
780
addLocale(locale: string): void
S
shawn_he 已提交
781 782 783 784 785

Adds the index of the new **locale** object to the index list.

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

S
shawn_he 已提交
786 787 788 789
**Parameters**
| Name   | Type    | Mandatory  | Description                          |
| ------ | ------ | ---- | ---------------------------- |
| locale | string | Yes   | A string containing locale information, including the language, optional script, and region.|
S
shawn_he 已提交
790

S
shawn_he 已提交
791
**Example**
S
shawn_he 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805
  ```
  var indexUtil = i18n.getInstance("zh-CN");
  indexUtil.addLocale("en-US");
  ```


### getIndex<sup>8+</sup>

getIndex(text: string): string

Obtains the index of a text object.

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

S
shawn_he 已提交
806 807 808 809
**Parameters**
| Name | Type    | Mandatory  | Description          |
| ---- | ------ | ---- | ------------ |
| text | string | Yes   | **text** object whose index is to be obtained.|
S
shawn_he 已提交
810

S
shawn_he 已提交
811
**Return value**
S
shawn_he 已提交
812 813 814
| Type    | Description         |
| ------ | ----------- |
| string | Index of the **text** object.|
S
shawn_he 已提交
815

S
shawn_he 已提交
816
**Example**
S
shawn_he 已提交
817 818 819 820 821 822 823 824 825 826 827
  ```
  var indexUtil= i18n.getInstance("zh-CN");
  indexUtil.getIndex("hi"); // Return h.
  ```


## Character<sup>8+</sup>


### isDigit<sup>8+</sup>

S
shawn_he 已提交
828
static isDigit(char: string): boolean
S
shawn_he 已提交
829 830 831 832 833

Checks whether the input character string is composed of digits.

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

S
shawn_he 已提交
834 835 836 837
**Parameters**
| Name | Type    | Mandatory  | Description   |
| ---- | ------ | ---- | ----- |
| char | string | Yes   | Input character.|
S
shawn_he 已提交
838

S
shawn_he 已提交
839
**Return value**
S
shawn_he 已提交
840 841 842
| Type     | Description                                  |
| ------- | ------------------------------------ |
| boolean | Returns **true** if the input character is a digit; returns **false** otherwise.|
S
shawn_he 已提交
843

S
shawn_he 已提交
844
**Example**
S
shawn_he 已提交
845
  ```
S
shawn_he 已提交
846
  var isdigit = i18n.Character.isDigit("1"); // Return true.
S
shawn_he 已提交
847 848 849 850 851
  ```


### isSpaceChar<sup>8+</sup>

S
shawn_he 已提交
852
static isSpaceChar(char: string): boolean
Z
zengyawen 已提交
853

Z
zengyawen 已提交
854
Checks whether the input character is a space.
Z
zengyawen 已提交
855

S
shawn_he 已提交
856 857
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
858 859 860 861
**Parameters**
| Name | Type    | Mandatory  | Description   |
| ---- | ------ | ---- | ----- |
| char | string | Yes   | Input character.|
S
shawn_he 已提交
862

S
shawn_he 已提交
863
**Return value**
S
shawn_he 已提交
864 865 866
| Type     | Description                                    |
| ------- | -------------------------------------- |
| boolean | Returns **true** if the input character is a space; returns **false** otherwise.|
S
shawn_he 已提交
867

S
shawn_he 已提交
868
**Example**
S
shawn_he 已提交
869
  ```
S
shawn_he 已提交
870
  var isspacechar = i18n.Character.isSpaceChar("a"); // Return false.
S
shawn_he 已提交
871 872 873 874 875
  ```


### isWhitespace<sup>8+</sup>

S
shawn_he 已提交
876
static isWhitespace(char: string): boolean
Z
zengyawen 已提交
877

Z
zengyawen 已提交
878
Checks whether the input character is a white space.
Z
zengyawen 已提交
879

S
shawn_he 已提交
880 881
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
882 883 884 885
**Parameters**
| Name | Type    | Mandatory  | Description   |
| ---- | ------ | ---- | ----- |
| char | string | Yes   | Input character.|
S
shawn_he 已提交
886

S
shawn_he 已提交
887
**Return value**
S
shawn_he 已提交
888 889 890
| Type     | Description                                    |
| ------- | -------------------------------------- |
| boolean | Returns **true** if the input character is a white space; returns **false** otherwise.|
S
shawn_he 已提交
891

S
shawn_he 已提交
892
**Example**
S
shawn_he 已提交
893
  ```
S
shawn_he 已提交
894
  var isspacechar = i18n.Character.isSpaceChar("a"); // Return false.
S
shawn_he 已提交
895 896 897 898 899
  ```


### isRTL<sup>8+</sup>

S
shawn_he 已提交
900
static isRTL(char: string): boolean
S
shawn_he 已提交
901 902 903 904 905

Checks whether the input character is of the right to left (RTL) language.

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

S
shawn_he 已提交
906 907 908 909
**Parameters**
| Name | Type    | Mandatory  | Description   |
| ---- | ------ | ---- | ----- |
| char | string | Yes   | Input character.|
S
shawn_he 已提交
910

S
shawn_he 已提交
911
**Return value**
S
shawn_he 已提交
912 913 914
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the input character is of the RTL language; returns **false** otherwise.|
S
shawn_he 已提交
915

S
shawn_he 已提交
916
**Example**
S
shawn_he 已提交
917
  ```
S
shawn_he 已提交
918
  var isrtl = i18n.Character.isRTL("a"); // Return false.
S
shawn_he 已提交
919 920 921 922 923
  ```


### isIdeograph<sup>8+</sup>

S
shawn_he 已提交
924
static isIdeograph(char: string): boolean
Z
zengyawen 已提交
925

Z
zengyawen 已提交
926
Checks whether the input character is an ideographic character.
Z
zengyawen 已提交
927

S
shawn_he 已提交
928 929
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
930 931 932 933
**Parameters**
| Name | Type    | Mandatory  | Description   |
| ---- | ------ | ---- | ----- |
| char | string | Yes   | Input character.|
S
shawn_he 已提交
934

S
shawn_he 已提交
935
**Return value**
S
shawn_he 已提交
936 937 938
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the input character is an ideographic character; returns **false** otherwise.|
S
shawn_he 已提交
939

S
shawn_he 已提交
940
**Example**
S
shawn_he 已提交
941
  ```
S
shawn_he 已提交
942
  var isideograph = i18n.Character.isIdeograph("a"); // Return false.
S
shawn_he 已提交
943 944 945 946 947
  ```


### isLetter<sup>8+</sup>

S
shawn_he 已提交
948
static isLetter(char: string): boolean
Z
zengyawen 已提交
949

Z
zengyawen 已提交
950
Checks whether the input character is a letter.
Z
zengyawen 已提交
951

S
shawn_he 已提交
952 953
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
954 955 956 957
**Parameters**
| Name | Type    | Mandatory  | Description   |
| ---- | ------ | ---- | ----- |
| char | string | Yes   | Input character.|
S
shawn_he 已提交
958

S
shawn_he 已提交
959
**Return value**
S
shawn_he 已提交
960 961 962
| Type     | Description                                  |
| ------- | ------------------------------------ |
| boolean | Returns **true** if the input character is a letter; returns **false** otherwise.|
S
shawn_he 已提交
963

S
shawn_he 已提交
964
**Example**
S
shawn_he 已提交
965
  ```
S
shawn_he 已提交
966
  var isletter = i18n.Character.isLetter("a"); // Return true.
S
shawn_he 已提交
967 968 969 970 971
  ```


### isLowerCase<sup>8+</sup>

S
shawn_he 已提交
972
static isLowerCase(char: string): boolean
Z
zengyawen 已提交
973

Z
zengyawen 已提交
974
Checks whether the input character is a lowercase letter.
Z
zengyawen 已提交
975

S
shawn_he 已提交
976 977
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
978 979 980 981
**Parameters**
| Name | Type    | Mandatory  | Description   |
| ---- | ------ | ---- | ----- |
| char | string | Yes   | Input character.|
S
shawn_he 已提交
982

S
shawn_he 已提交
983
**Return value**
S
shawn_he 已提交
984 985 986
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the input character is a lowercase letter; returns **false** otherwise.|
S
shawn_he 已提交
987

S
shawn_he 已提交
988
**Example**
S
shawn_he 已提交
989
  ```
S
shawn_he 已提交
990
  var islowercase = i18n.Character.isLowerCase("a"); // Return true.
S
shawn_he 已提交
991 992 993 994 995
  ```


### isUpperCase<sup>8+</sup>

S
shawn_he 已提交
996
static isUpperCase(char: string): boolean
Z
zengyawen 已提交
997

Z
zengyawen 已提交
998
Checks whether the input character is an uppercase letter.
Z
zengyawen 已提交
999

S
shawn_he 已提交
1000 1001
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
1002 1003 1004 1005
**Parameters**
| Name | Type    | Mandatory  | Description   |
| ---- | ------ | ---- | ----- |
| char | string | Yes   | Input character.|
S
shawn_he 已提交
1006

S
shawn_he 已提交
1007
**Return value**
S
shawn_he 已提交
1008 1009 1010
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the input character is an uppercase letter; returns **false** otherwise.|
S
shawn_he 已提交
1011

S
shawn_he 已提交
1012
**Example**
S
shawn_he 已提交
1013
  ```
S
shawn_he 已提交
1014
  var isuppercase = i18n.Character.isUpperCase("a"); // Return false.
S
shawn_he 已提交
1015 1016 1017 1018 1019
  ```


### getType<sup>8+</sup>

S
shawn_he 已提交
1020
static getType(char: string): string
Z
zengyawen 已提交
1021 1022 1023

Obtains the type of the input character string.

S
shawn_he 已提交
1024
**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
1025

S
shawn_he 已提交
1026 1027 1028 1029
**Parameters**
| Name | Type    | Mandatory  | Description   |
| ---- | ------ | ---- | ----- |
| char | string | Yes   | Input character.|
Z
zengyawen 已提交
1030

S
shawn_he 已提交
1031
**Return value**
S
shawn_he 已提交
1032 1033 1034
| Type    | Description         |
| ------ | ----------- |
| string | Type of the input character.|
Z
zengyawen 已提交
1035

S
shawn_he 已提交
1036
**Example**
S
shawn_he 已提交
1037
  ```
S
shawn_he 已提交
1038
  var type = i18n.Character.getType("a");
S
shawn_he 已提交
1039
  ```
Z
zengyawen 已提交
1040 1041


S
shawn_he 已提交
1042
## i18n.getLineInstance<sup>8+</sup>
Z
zengyawen 已提交
1043

S
shawn_he 已提交
1044
getLineInstance(locale: string): BreakIterator
Z
zengyawen 已提交
1045

S
shawn_he 已提交
1046
Obtains a [BreakIterator](#breakiterator8) object for text segmentation.
Z
zengyawen 已提交
1047

S
shawn_he 已提交
1048
**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
1049

S
shawn_he 已提交
1050 1051 1052 1053
**Parameters**
| Name   | Type    | Mandatory  | Description                                      |
| ------ | ------ | ---- | ---------------------------------------- |
| locale | string | Yes   | Valid locale value, for example, **zh-Hans-CN**. The [BreakIterator](#breakiterator8) object segments text according to the rules of the specified locale.|
Z
zengyawen 已提交
1054

S
shawn_he 已提交
1055
**Return value**
S
shawn_he 已提交
1056 1057 1058
| Type                              | Description         |
| -------------------------------- | ----------- |
| [BreakIterator](#breakiterator8) | [BreakIterator](#breakiterator8) object used for text segmentation.|
Z
zengyawen 已提交
1059

S
shawn_he 已提交
1060
**Example**
S
shawn_he 已提交
1061
  ```
S
shawn_he 已提交
1062
  var iterator = i18n.getLineInstance("en");
S
shawn_he 已提交
1063
  ```
Z
zengyawen 已提交
1064 1065


S
shawn_he 已提交
1066
## BreakIterator<sup>8+</sup>
Z
zengyawen 已提交
1067 1068


S
shawn_he 已提交
1069
### setLineBreakText<sup>8+</sup>
Z
zengyawen 已提交
1070

S
shawn_he 已提交
1071
setLineBreakText(text: string): void
Z
zengyawen 已提交
1072

S
shawn_he 已提交
1073
Sets the text to be processed by the [BreakIterator](#breakiterator8) object.
Z
zengyawen 已提交
1074

S
shawn_he 已提交
1075
**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
1076

S
shawn_he 已提交
1077 1078 1079 1080
**Parameters**
| Name | Type    | Mandatory  | Description                     |
| ---- | ------ | ---- | ----------------------- |
| text | string | Yes   | Text to be processed by the **BreakIterator** object.|
Z
zengyawen 已提交
1081

S
shawn_he 已提交
1082
**Example**
S
shawn_he 已提交
1083
  ```
S
shawn_he 已提交
1084
  var iterator = i18n.getLineInstance("en");
S
shawn_he 已提交
1085 1086
  iterator.setLineBreakText("Apple is my favorite fruit.");
  ```
Z
zengyawen 已提交
1087 1088


S
shawn_he 已提交
1089
### getLineBreakText<sup>8+</sup>
Z
zengyawen 已提交
1090

S
shawn_he 已提交
1091
getLineBreakText(): string
Z
zengyawen 已提交
1092

S
shawn_he 已提交
1093
Obtains the text being processed by the [BreakIterator](#breakiterator8) object.
Z
zengyawen 已提交
1094

S
shawn_he 已提交
1095
**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
1096

S
shawn_he 已提交
1097
**Return value**
S
shawn_he 已提交
1098 1099 1100
| Type    | Description                    |
| ------ | ---------------------- |
| string | Text being processed by the **BreakIterator** object.|
Z
zengyawen 已提交
1101

S
shawn_he 已提交
1102
**Example**
S
shawn_he 已提交
1103
  ```
S
shawn_he 已提交
1104
  var iterator = i18n.getLineInstance("en");
S
shawn_he 已提交
1105 1106 1107
  iterator.setLineBreakText("Apple is my favorite fruit.");
  iterator.getLineBreakText(); // Apple is my favorite fruit.
  ```
Z
zengyawen 已提交
1108 1109


S
shawn_he 已提交
1110
### current<sup>8+</sup>
Z
zengyawen 已提交
1111

S
shawn_he 已提交
1112 1113 1114 1115 1116 1117
current(): number

Obtains the position of the [BreakIterator](#breakiterator8) object in the text being processed.

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

S
shawn_he 已提交
1118
**Return value**
S
shawn_he 已提交
1119 1120 1121
| Type    | Description                         |
| ------ | --------------------------- |
| number | Position of the **BreakIterator** object in the text being processed.|
S
shawn_he 已提交
1122

S
shawn_he 已提交
1123
**Example**
S
shawn_he 已提交
1124
  ```
S
shawn_he 已提交
1125
  var iterator = i18n.getLineInstance("en");
S
shawn_he 已提交
1126
  iterator.setLineBreakText("Apple is my favorite fruit.");
S
shawn_he 已提交
1127
  iterator.current(); // 0
S
shawn_he 已提交
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
  ```


### first<sup>8+</sup>

first(): number

Puts the [BreakIterator](#breakiterator8) object to the first text boundary, which is always at the beginning of the processed text.

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

S
shawn_he 已提交
1139
**Return value**
S
shawn_he 已提交
1140 1141 1142
| Type    | Description               |
| ------ | ----------------- |
| number | Offset to the first text boundary of the processed text.|
S
shawn_he 已提交
1143

S
shawn_he 已提交
1144
**Example**
S
shawn_he 已提交
1145
  ```
S
shawn_he 已提交
1146
  var iterator = i18n.getLineInstance("en");
S
shawn_he 已提交
1147
  iterator.setLineBreakText("Apple is my favorite fruit.");
S
shawn_he 已提交
1148
  iterator.first(); // 0
S
shawn_he 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
  ```


### last<sup>8+</sup>

last(): number

Puts the [BreakIterator](#breakiterator8) object to the last text boundary, which is always the next position after the end of the processed text.

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

S
shawn_he 已提交
1160
**Return value**
S
shawn_he 已提交
1161 1162 1163
| Type    | Description                |
| ------ | ------------------ |
| number | Offset of the last text boundary of the processed text.|
S
shawn_he 已提交
1164

S
shawn_he 已提交
1165
**Example**
S
shawn_he 已提交
1166
  ```
S
shawn_he 已提交
1167
  var iterator = i18n.getLineInstance("en");
S
shawn_he 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
  iterator.setLineBreakText("Apple is my favorite fruit.");
  iterator.last(); // 27
  ```


### next<sup>8+</sup>

next(index?: number): number

Moves the [BreakIterator](#breakiterator8) object backward by the specified number of text boundaries if the specified index is a positive number. If the index is a negative number, the [BreakIterator](#breakiterator8) object will be moved forward by the corresponding number of text boundaries. If no index is specified, the index will be treated as **1**.

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

S
shawn_he 已提交
1181 1182 1183 1184
**Parameters**
| Name  | Type    | Mandatory  | Description                                      |
| ----- | ------ | ---- | ---------------------------------------- |
| index | number | No   | Number of text boundaries by which the [BreakIterator](#breakiterator8) object is moved. A positive value indicates that the text boundary is moved backward, and a negative value indicates the opposite. If no index is specified, the index will be treated as **1**.|
S
shawn_he 已提交
1185

S
shawn_he 已提交
1186
**Return value**
S
shawn_he 已提交
1187 1188 1189
| Type    | Description                                      |
| ------ | ---------------------------------------- |
| number | Position of the [BreakIterator](#breakiterator8) object in the text after it is moved by the specified number of text boundaries. The value **-1** is returned if the position of the [BreakIterator](#breakiterator8) object is outside of the processed text after it is moved by the specified number of text boundaries.|
S
shawn_he 已提交
1190

S
shawn_he 已提交
1191
**Example**
S
shawn_he 已提交
1192
  ```
S
shawn_he 已提交
1193
  var iterator = i18n.getLineInstance("en");
S
shawn_he 已提交
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
  iterator.setLineBreakText("Apple is my favorite fruit.");
  iterator.first(); // 0
  iterator.next(); // 6
  iterator.next(10); // -1
  ```


### previous<sup>8+</sup>

previous(): number

Moves the [BreakIterator](#breakiterator8) object to the previous text boundary.

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

S
shawn_he 已提交
1209
**Return value**
S
shawn_he 已提交
1210 1211 1212
| Type    | Description                                      |
| ------ | ---------------------------------------- |
| number | Position of the [BreakIterator](#breakiterator8) object in the text after it is moved to the previous text boundary. The value **-1** is returned if the position of the [BreakIterator](#breakiterator8) object is outside of the processed text after it is moved by the specified number of text boundaries.|
Z
zengyawen 已提交
1213

S
shawn_he 已提交
1214
**Example**
S
shawn_he 已提交
1215
  ```
S
shawn_he 已提交
1216
  var iterator = i18n.getLineInstance("en");
S
shawn_he 已提交
1217 1218 1219 1220 1221
  iterator.setLineBreakText("Apple is my favorite fruit.");
  iterator.first(); // 0
  iterator.next(3); // 12
  iterator.previous(); // 9
  ```
Z
zengyawen 已提交
1222 1223


S
shawn_he 已提交
1224
### following<sup>8+</sup>
Z
zengyawen 已提交
1225

S
shawn_he 已提交
1226
following(offset: number): number
Z
zengyawen 已提交
1227

S
shawn_he 已提交
1228
Moves the [BreakIterator](#breakiterator8) object to the text boundary after the position specified by the offset. Position of the [BreakIterator](#breakiterator8) object after it is moved to the text boundary after the position specified by the offset.
Z
zengyawen 已提交
1229

S
shawn_he 已提交
1230
**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
1231

S
shawn_he 已提交
1232 1233 1234 1235
**Parameters**
| Name   | Type    | Mandatory  | Description                                      |
| ------ | ------ | ---- | ---------------------------------------- |
| offset | number | Yes   | Offset to the position before the text boundary to which the [BreakIterator](#breakiterator8) object is moved.|
Z
zengyawen 已提交
1236

S
shawn_he 已提交
1237
**Return value**
S
shawn_he 已提交
1238 1239 1240
| Type    | Description                                      |
| ------ | ---------------------------------------- |
| number | The value **-1** is returned if the text boundary to which the [BreakIterator](#breakiterator8) object is moved is outside of the processed text.|
S
shawn_he 已提交
1241

S
shawn_he 已提交
1242
**Example**
S
shawn_he 已提交
1243
  ```
S
shawn_he 已提交
1244
  var iterator = i18n.getLineInstance("en");
S
shawn_he 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
  iterator.setLineBreakText("Apple is my favorite fruit.");
  iterator.following(0); // 6
  iterator.following(100); // -1
  iterator.current(); // 27
  ```


### isBoundary<sup>8+</sup>

isBoundary(offset: number): boolean

Checks whether the position specified by the offset is a text boundary. If **true** is returned, the [BreakIterator](#breakiterator8) object is moved to the position specified by the offset. If **false** is returned, the [BreakIterator](#breakiterator8) object is moved to the text boundary after the position specified by the offset, which is equivalent to calling [following](#following8)(offset).

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

S
shawn_he 已提交
1260 1261 1262 1263
**Parameters**
| Name   | Type    | Mandatory  | Description         |
| ------ | ------ | ---- | ----------- |
| offset | number | Yes   | Position to check.|
S
shawn_he 已提交
1264

S
shawn_he 已提交
1265
**Return value**
S
shawn_he 已提交
1266 1267 1268
| Type     | Description                             |
| ------- | ------------------------------- |
| boolean | Returns **true** if the position specified by the offset is a text boundary; returns **false** otherwise.|
S
shawn_he 已提交
1269

S
shawn_he 已提交
1270
**Example**
S
shawn_he 已提交
1271
  ```
S
shawn_he 已提交
1272
  var iterator = i18n.getLineInstance("en");
S
shawn_he 已提交
1273 1274 1275 1276 1277 1278
  iterator.setLineBreakText("Apple is my favorite fruit.");
  iterator.isBoundary(0); // true;
  iterator.isBoundary(5); // false;
  ```


S
shawn_he 已提交
1279
## i18n.is24HourClock<sup>7+</sup>
S
shawn_he 已提交
1280 1281 1282 1283 1284 1285 1286

is24HourClock(): boolean

Checks whether the 24-hour clock is used.

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

S
shawn_he 已提交
1287
**Return value**
S
shawn_he 已提交
1288 1289 1290
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the 24-hour clock is used; returns **false** otherwise.|
S
shawn_he 已提交
1291

S
shawn_he 已提交
1292
**Example**
S
shawn_he 已提交
1293 1294 1295 1296 1297
  ```
  var is24HourClock = i18n.is24HourClock();
  ```


S
shawn_he 已提交
1298
## i18n.set24HourClock<sup>7+</sup>
S
shawn_he 已提交
1299 1300

set24HourClock(option: boolean): boolean
Z
zengyawen 已提交
1301 1302 1303

Sets the 24-hour clock.

S
shawn_he 已提交
1304
**Permission required**: ohos.permission.UPDATE_CONFIGURATION
S
shawn_he 已提交
1305

S
shawn_he 已提交
1306 1307
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
1308 1309 1310 1311
**Parameters**
| Name   | Type     | Mandatory  | Description                                      |
| ------ | ------- | ---- | ---------------------------------------- |
| option | boolean | Yes   | Whether to enable the 24-hour clock. The value **true** means to enable the 24-hour clock, and the value **false** means the opposite.|
S
shawn_he 已提交
1312

S
shawn_he 已提交
1313
**Return value**
S
shawn_he 已提交
1314 1315 1316
| Type     | Description                           |
| ------- | ----------------------------- |
| boolean | Returns **true** if the 24-hour clock is enabled; returns **false** otherwise.|
S
shawn_he 已提交
1317

S
shawn_he 已提交
1318
**Example**
S
shawn_he 已提交
1319 1320
  ```
  // Set the system time to the 24-hour clock.
S
shawn_he 已提交
1321
  var success = i18n.set24HourClock(true);
S
shawn_he 已提交
1322 1323 1324 1325 1326 1327
  ```


## i18n.addPreferredLanguage<sup>8+</sup>

addPreferredLanguage(language: string, index?: number): boolean
Z
zengyawen 已提交
1328 1329 1330

Adds a preferred language to the specified position on the preferred language list.

S
shawn_he 已提交
1331
**Permission required**: ohos.permission.UPDATE_CONFIGURATION
S
shawn_he 已提交
1332

S
shawn_he 已提交
1333 1334
**System capability**: SystemCapability.Global.I18n

S
shawn_he 已提交
1335 1336 1337 1338 1339
**Parameters**
| Name     | Type    | Mandatory  | Description        |
| -------- | ------ | ---- | ---------- |
| language | string | Yes   | Preferred language to add. |
| index    | number | No   | Position to which the preferred language is added.|
S
shawn_he 已提交
1340

S
shawn_he 已提交
1341
**Return value**
S
shawn_he 已提交
1342 1343 1344
| Type     | Description                           |
| ------- | ----------------------------- |
| boolean | Returns **true** if the preferred language is successfully added; returns **false** otherwise.|
S
shawn_he 已提交
1345

S
shawn_he 已提交
1346
**Example**
S
shawn_he 已提交
1347 1348 1349 1350 1351 1352 1353 1354
  ```
  // Add zh-CN to the preferred language list.
  var language = 'zh-CN';
  var index = 0;
  var success = i18n.addPreferredLanguage(language, index);
  ```


S
shawn_he 已提交
1355
## i18n.removePreferredLanguage<sup>8+</sup>
S
shawn_he 已提交
1356

S
shawn_he 已提交
1357
removePreferredLanguage(index: number): boolean
Z
zengyawen 已提交
1358 1359 1360

Deletes a preferred language from the specified position on the preferred language list.

S
shawn_he 已提交
1361
**Permission required**: ohos.permission.UPDATE_CONFIGURATION
S
shawn_he 已提交
1362

S
shawn_he 已提交
1363
**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
1364

S
shawn_he 已提交
1365 1366 1367 1368
**Parameters**
| Name  | Type    | Mandatory  | Description                   |
| ----- | ------ | ---- | --------------------- |
| index | number | Yes   | Position of the preferred language to delete.|
Z
zengyawen 已提交
1369

S
shawn_he 已提交
1370
**Return value**
S
shawn_he 已提交
1371 1372 1373
| Type     | Description                           |
| ------- | ----------------------------- |
| boolean | Returns **true** if the preferred language is deleted; returns **false** otherwise.|
Z
zengyawen 已提交
1374

S
shawn_he 已提交
1375
**Example**
S
shawn_he 已提交
1376 1377 1378 1379 1380
  ```
  // Delete the first preferred language from the preferred language list.
  var index = 0;
  var success = i18n.removePreferredLanguage(index);
  ```
Z
zengyawen 已提交
1381 1382


S
shawn_he 已提交
1383
## i18n.getPreferredLanguageList<sup>8+</sup>
Z
zengyawen 已提交
1384

S
shawn_he 已提交
1385
getPreferredLanguageList(): Array&lt;string&gt;
Z
zengyawen 已提交
1386

S
shawn_he 已提交
1387
Obtains the list of preferred languages.
Z
zengyawen 已提交
1388

S
shawn_he 已提交
1389
**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
1390

S
shawn_he 已提交
1391
**Return value**
S
shawn_he 已提交
1392 1393 1394
| Type                 | Description       |
| ------------------- | --------- |
| Array&lt;string&gt; | List of preferred languages.|
Z
zengyawen 已提交
1395

S
shawn_he 已提交
1396
**Example**
S
shawn_he 已提交
1397 1398 1399
  ```
  var preferredLanguageList = i18n.getPreferredLanguageList();
  ```
Z
zengyawen 已提交
1400 1401


S
shawn_he 已提交
1402
## i18n.getFirstPreferredLanguage<sup>8+</sup>
Z
zengyawen 已提交
1403

S
shawn_he 已提交
1404 1405
getFirstPreferredLanguage(): string

S
shawn_he 已提交
1406
Obtains the first language in the preferred language list.
Z
zengyawen 已提交
1407

S
shawn_he 已提交
1408
**System capability**: SystemCapability.Global.I18n
Z
zengyawen 已提交
1409

S
shawn_he 已提交
1410
**Return value**
S
shawn_he 已提交
1411 1412 1413
| Type    | Description            |
| ------ | -------------- |
| string | First language in the preferred language list.|
Z
zengyawen 已提交
1414

S
shawn_he 已提交
1415
**Example**
S
shawn_he 已提交
1416 1417 1418
  ```
  var firstPreferredLanguage = i18n.getFirstPreferredLanguage();
  ```
S
shawn_he 已提交
1419 1420


S
shawn_he 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
## i18n.getAppPreferredLanguage<sup>9+</sup>

getAppPreferredLanguage(): string

Obtains the preferred language of an application.

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

**Return value**
| Type    | Description            |
| ------ | -------------- |
| string | Preferred language of the application.|

**Example**
  ```
  var appPreferredLanguage = i18n.getAppPreferredLanguage();
  ```


S
shawn_he 已提交
1440
## i18n.getTimeZone<sup>7+</sup>
S
shawn_he 已提交
1441 1442 1443 1444 1445 1446 1447

getTimeZone(zoneID?: string): TimeZone

Obtains the **TimeZone** object corresponding to the specified time zone ID.

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

S
shawn_he 已提交
1448 1449 1450 1451
**Parameters**
| Name   | Type    | Mandatory  | Description   |
| ------ | ------ | ---- | ----- |
| zondID | string | No   | Time zone ID.|
S
shawn_he 已提交
1452

S
shawn_he 已提交
1453
**Return value**
S
shawn_he 已提交
1454 1455 1456
| Type      | Description          |
| -------- | ------------ |
| TimeZone | **TimeZone** object corresponding to the time zone ID.|
S
shawn_he 已提交
1457

S
shawn_he 已提交
1458
**Example**
S
shawn_he 已提交
1459 1460 1461 1462 1463
  ```
  var timezone = i18n.getTimeZone();
  ```


S
shawn_he 已提交
1464
## TimeZone<sup>8+</sup>
S
shawn_he 已提交
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474


### getID<sup>8+</sup>

getID(): string

Obtains the ID of the specified **TimeZone** object.

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

S
shawn_he 已提交
1475
**Return value**
S
shawn_he 已提交
1476 1477 1478
| Type    | Description          |
| ------ | ------------ |
| string | Time zone ID corresponding to the **TimeZone** object.|
S
shawn_he 已提交
1479

S
shawn_he 已提交
1480
**Example**
S
shawn_he 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
  ```
  var timezone = i18n.getTimeZone();
  timezone.getID();
  ```


### getDisplayName<sup>8+</sup>

getDisplayName(locale?: string, isDST?: boolean): string

Obtains the representation of a **TimeZone** object in the specified locale.

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

S
shawn_he 已提交
1495 1496 1497 1498 1499
**Parameters**
| Name   | Type     | Mandatory  | Description                  |
| ------ | ------- | ---- | -------------------- |
| locale | string  | No   | System locale ID.               |
| isDST  | boolean | No   | Whether to consider DST when obtaining the representation of the **TimeZone** object.|
S
shawn_he 已提交
1500

S
shawn_he 已提交
1501
**Return value**
S
shawn_he 已提交
1502 1503 1504
| Type    | Description           |
| ------ | ------------- |
| string | Representation of the **TimeZone** object in the specified locale.|
S
shawn_he 已提交
1505

S
shawn_he 已提交
1506
**Example**
S
shawn_he 已提交
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
  ```
  var timezone = i18n.getTimeZone();
  timezone.getDisplayName("zh-CN", false);
  ```


### getRawOffset<sup>8+</sup>

getRawOffset(): number

Obtains the offset between the time zone represented by a **TimeZone** object and the UTC time zone.

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

S
shawn_he 已提交
1521
**Return value**
S
shawn_he 已提交
1522 1523 1524
| Type    | Description                 |
| ------ | ------------------- |
| number | Offset between the time zone represented by the **TimeZone** object and the UTC time zone.|
S
shawn_he 已提交
1525

S
shawn_he 已提交
1526
**Example**
S
shawn_he 已提交
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
  ```
  var timezone = i18n.getTimeZone();
  timezone.getRawOffset();
  ```


### getOffset<sup>8+</sup>

getOffset(date?: number): number

Obtains the offset between the time zone represented by a **TimeZone** object and the UTC time zone at a certain time point.

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

S
shawn_he 已提交
1541
**Return value**
S
shawn_he 已提交
1542 1543 1544
| Type    | Description                     |
| ------ | ----------------------- |
| number | Offset between the time zone represented by the **TimeZone** object and the UTC time zone at a certain time point.|
S
shawn_he 已提交
1545

S
shawn_he 已提交
1546
**Example**
S
shawn_he 已提交
1547 1548 1549 1550
  ```
  var timezone = i18n.getTimeZone();
  timezone.getOffset(1234567890);
  ```