1# @ohos.i18n (国际化-I18n)(系统接口) 2 3<!--Kit: Localization Kit--> 4<!--Subsystem: Global--> 5<!--Owner: @yliupy--> 6<!--Designer: @sunyaozu--> 7<!--Tester: @lpw_work--> 8<!--Adviser: @Brilliantry_Rui--> 9 10 本模块提供系统相关的或者增强的国际化能力,包括区域管理、电话号码处理、日历等,相关接口为ECMA 402标准中未定义的补充接口。[Intl模块](js-apis-intl.md)提供了ECMA 402标准定义的基础国际化接口,与本模块共同使用可提供完整地国际化支持能力。 11 12> **说明:** 13> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> 15> - 从API version 11开始,本模块部分接口支持在ArkTS卡片中使用。 16> 17> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.i18n (国际化-I18n)](js-apis-intl.md)。 18 19 20## 导入模块 21 22```ts 23import { i18n } from '@kit.LocalizationKit'; 24``` 25 26## System<sup>9+</sup> 27 28提供系统属性获取或设置的能力。 29 30### setSystemLanguage<sup>9+</sup> 31 32static setSystemLanguage(language: string): void 33 34设置系统语言。 35 36**系统接口**:此接口为系统接口。 37 38**需要权限**:ohos.permission.UPDATE_CONFIGURATION 39 40**系统能力**:SystemCapability.Global.I18n 41 42**参数:** 43 44| 参数名 | 类型 | 必填 | 说明 | 45| -------- | ------ | ---- | ----- | 46| language | string | 是 | 合法的语言ID。 | 47 48**错误码:** 49 50以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 51 52| 错误码ID | 错误信息 | 53| ------ | ---------------------- | 54| 201 | Permission verification failed. The application does not have the permission required to call the API. | 55| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 56| 890001 | Invalid parameter. Possible causes: Parameter verification failed. | 57 58**示例:** 59 ```ts 60 import { BusinessError } from '@kit.BasicServicesKit'; 61 62 // 设置系统语言 63 try { 64 i18n.System.setSystemLanguage('zh'); // 设置系统当前语言为 "zh" 65 } catch(error) { 66 let err: BusinessError = error as BusinessError; 67 console.error(`call System.setSystemLanguage failed, error code: ${err.code}, message: ${err.message}.`); 68 } 69 ``` 70 71### setSystemRegion<sup>9+</sup> 72 73static setSystemRegion(region: string): void 74 75设置系统地区。 76 77若要监听系统地区变化,可以监听[事件](../apis-basic-services-kit/common_event/commonEventManager-definitions.md#common_event_locale_changed)OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_LOCALE_CHANGED。 78 79**系统接口**:此接口为系统接口。 80 81**需要权限**:ohos.permission.UPDATE_CONFIGURATION 82 83**系统能力**:SystemCapability.Global.I18n 84 85**参数:** 86 87| 参数名 | 类型 | 必填 | 说明 | 88| ------ | ------ | ---- | ----- | 89| region | string | 是 | 合法的地区ID。 | 90 91**错误码:** 92 93以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 94 95| 错误码ID | 错误信息 | 96| ------ | ---------------------- | 97| 201 | Permission verification failed. The application does not have the permission required to call the API. | 98| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 99| 890001 | Invalid parameter. Possible causes: Parameter verification failed. | 100 101**示例:** 102 ```ts 103 import { BusinessError } from '@kit.BasicServicesKit'; 104 105 try { 106 i18n.System.setSystemRegion('CN'); // 设置系统当前地区为 "CN" 107 } catch(error) { 108 let err: BusinessError = error as BusinessError; 109 console.error(`call System.setSystemRegion failed, error code: ${err.code}, message: ${err.message}.`); 110 } 111 ``` 112 113 114 115### setSystemLocale<sup>(deprecated)</sup> 116 117static setSystemLocale(locale: string): void 118 119> 从API version 9开始支持,从API version 20开始废弃。 120 121设置系统区域。 122 123若要监听系统区域变化,可以监听[事件](../apis-basic-services-kit/common_event/commonEventManager-definitions.md#common_event_locale_changed)OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_LOCALE_CHANGED。 124 125**系统接口**:此接口为系统接口。 126 127**需要权限**:ohos.permission.UPDATE_CONFIGURATION 128 129**系统能力**:SystemCapability.Global.I18n 130 131**参数:** 132 133| 参数名 | 类型 | 必填 | 说明 | 134| ------ | ------ | ---- | --------------- | 135| locale | string | 是 | [表示区域ID的字符串](../../internationalization/i18n-locale-culture.md#实现原理),由语言、脚本、国家或地区组成。 | 136 137**错误码:** 138 139以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 140 141| 错误码ID | 错误信息 | 142| ------ | ---------------------- | 143| 201 | Permission verification failed. The application does not have the permission required to call the API. | 144| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 145| 890001 | Invalid parameter. Possible causes: Parameter verification failed. | 146 147**示例:** 148 ```ts 149 import { BusinessError } from '@kit.BasicServicesKit'; 150 151 try { 152 i18n.System.setSystemLocale('zh-CN'); // 设置系统当前区域ID为 "zh-CN" 153 } catch(error) { 154 let err: BusinessError = error as BusinessError; 155 console.error(`call System.setSystemLocale failed, error code: ${err.code}, message: ${err.message}.`); 156 } 157 ``` 158 159 160### set24HourClock<sup>9+</sup> 161 162static set24HourClock(option: boolean): void 163 164设置系统时制是否为24小时制。 165 166**系统接口**:此接口为系统接口。 167 168**需要权限**:ohos.permission.UPDATE_CONFIGURATION 169 170**系统能力**:SystemCapability.Global.I18n 171 172**参数:** 173 174| 参数名 | 类型 | 必填 | 说明 | 175| ------ | ------- | ---- | ---------------------------------------- | 176| option | boolean | 是 | true表示设置系统时制为24小时制,false表示设置系统时制为12小时制。 | 177 178**错误码:** 179 180以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 181 182| 错误码ID | 错误信息 | 183| ------ | ---------------------- | 184| 201 | Permission verification failed. The application does not have the permission required to call the API. | 185| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 186| 890001 | Invalid parameter. Possible causes: Parameter verification failed. | 187 188**示例:** 189 ```ts 190 import { BusinessError } from '@kit.BasicServicesKit'; 191 192 // 将系统时制设置为24小时制 193 try { 194 i18n.System.set24HourClock(true); 195 } catch(error) { 196 let err: BusinessError = error as BusinessError; 197 console.error(`call System.set24HourClock failed, error code: ${err.code}, message: ${err.message}.`); 198 } 199 ``` 200 201### addPreferredLanguage<sup>9+</sup> 202 203static addPreferredLanguage(language: string, index?: number): void 204 205在系统偏好语言列表的指定位置添加偏好语言。 206 207**系统接口**:此接口为系统接口。 208 209**需要权限**:ohos.permission.UPDATE_CONFIGURATION 210 211**系统能力**:SystemCapability.Global.I18n 212 213**参数:** 214 215| 参数名 | 类型 | 必填 | 说明 | 216| -------- | ------ | ---- | ---------- | 217| language | string | 是 | 待添加的偏好语言,要求是合法的语言ID。 | 218| index | number | 否 | 偏好语言的添加位置。默认值:系统偏好语言列表长度。 | 219 220**错误码:** 221 222以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 223 224| 错误码ID | 错误信息 | 225| ------ | ---------------------- | 226| 201 | Permission verification failed. The application does not have the permission required to call the API. | 227| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 228| 890001 | Invalid parameter. Possible causes: Parameter verification failed. | 229 230**示例:** 231 ```ts 232 import { BusinessError } from '@kit.BasicServicesKit'; 233 234 // 将语言zh-CN添加到系统偏好语言列表中 235 let language = 'zh-CN'; 236 let index = 0; 237 try { 238 i18n.System.addPreferredLanguage(language, index); // 将zh-CN添加到系统偏好语言列表的第1位 239 } catch(error) { 240 let err: BusinessError = error as BusinessError; 241 console.error(`call System.addPreferredLanguage failed, error code: ${err.code}, message: ${err.message}.`); 242 } 243 ``` 244 245### removePreferredLanguage<sup>9+</sup> 246 247static removePreferredLanguage(index: number): void 248 249从系统偏好语言列表中移除指定位置的偏好语言。 250 251**系统接口**:此接口为系统接口。 252 253**需要权限**:ohos.permission.UPDATE_CONFIGURATION 254 255**系统能力**:SystemCapability.Global.I18n 256 257**参数:** 258 259| 参数名 | 类型 | 必填 | 说明 | 260| ----- | ------ | ---- | --------------------- | 261| index | number | 是 | 待删除偏好语言在系统偏好语言列表中的位置。 | 262 263**错误码:** 264 265以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 266 267| 错误码ID | 错误信息 | 268| ------ | ---------------------- | 269| 201 | Permission verification failed. The application does not have the permission required to call the API. | 270| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 271| 890001 | Invalid parameter. Possible causes: Parameter verification failed. | 272 273**示例:** 274 ```ts 275 import { BusinessError } from '@kit.BasicServicesKit'; 276 277 // 删除系统偏好语言列表中的第一个偏好语言 278 let index: number = 0; 279 try { 280 i18n.System.removePreferredLanguage(index); 281 } catch(error) { 282 let err: BusinessError = error as BusinessError; 283 console.error(`call System.removePreferredLanguage failed, error code: ${err.code}, message: ${err.message}.`); 284 } 285 ``` 286 287### setUsingLocalDigit<sup>9+</sup> 288 289static setUsingLocalDigit(flag: boolean): void 290 291设置系统是否使用本地数字。 292 293**系统接口**:此接口为系统接口。 294 295**需要权限**:ohos.permission.UPDATE_CONFIGURATION 296 297**系统能力**:SystemCapability.Global.I18n 298 299**参数:** 300 301| 参数名 | 类型 | 必填 | 说明 | 302| ---- | ------- | ---- | ------------------------------- | 303| flag | boolean | 是 | true表示打开本地数字开关,false表示关闭本地数字开关。 | 304 305**错误码:** 306 307以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 308 309| 错误码ID | 错误信息 | 310| ------ | ---------------------- | 311| 201 | Permission verification failed. The application does not have the permission required to call the API. | 312| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 313| 890001 | Invalid parameter. Possible causes: Parameter verification failed. | 314 315**示例:** 316 ```ts 317 import { BusinessError } from '@kit.BasicServicesKit'; 318 319 try { 320 i18n.System.setUsingLocalDigit(true); // 打开本地化数字开关 321 } catch(error) { 322 let err: BusinessError = error as BusinessError; 323 console.error(`call System.setUsingLocalDigit failed, error code: ${err.code}, message: ${err.message}.`); 324 } 325 ``` 326 327 328### setTemperatureType<sup>18+</sup> 329 330static setTemperatureType(type: TemperatureType): void 331 332设置系统的温度单位。 333 334**系统接口**:此接口为系统接口。 335 336**需要权限**:ohos.permission.UPDATE_CONFIGURATION 337 338**系统能力**:SystemCapability.Global.I18n 339 340**参数:** 341 342| 参数名 | 类型 | 必填 | 说明 | 343| ---- | ------- | ---- | ------------------------------- | 344| type | [TemperatureType](./js-apis-i18n.md#temperaturetype18) | 是 | 温度单位。 | 345 346**错误码:** 347 348以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 349 350| 错误码ID | 错误信息 | 351| ------ | ---------------------- | 352| 201 | Permission verification failed. The application does not have the permission required to call the API. | 353| 202 | Permission verification failed. A non-system application calls a system API. | 354| 890001 | Invalid parameter. Possible causes: Parameter verification failed. | 355 356> **说明** 357> 358> 890001的报错信息请以接口的实际报错为准。 359 360**示例:** 361 ```ts 362 import { BusinessError } from '@kit.BasicServicesKit'; 363 364 try { 365 i18n.System.setTemperatureType(i18n.TemperatureType.CELSIUS); // 设置温度单位为摄氏度 366 } catch(error) { 367 let err: BusinessError = error as BusinessError; 368 console.error(`call System.setTemperatureType failed, error code: ${err.code}, message: ${err.message}.`); 369 } 370 ``` 371 372### setFirstDayOfWeek<sup>18+</sup> 373 374static setFirstDayOfWeek(type: WeekDay): void 375 376设置系统的周起始日。 377 378**系统接口**:此接口为系统接口。 379 380**需要权限**:ohos.permission.UPDATE_CONFIGURATION 381 382**系统能力**:SystemCapability.Global.I18n 383 384**参数:** 385 386| 参数名 | 类型 | 必填 | 说明 | 387| ---- | ------- | ---- | ------------------------------- | 388| type | [WeekDay](./js-apis-i18n.md#weekday18) | 是 | 周期起始日。 | 389 390**错误码:** 391 392以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 393 394| 错误码ID | 错误信息 | 395| ------ | ---------------------- | 396| 201 | Permission verification failed. The application does not have the permission required to call the API. | 397| 202 | Permission verification failed. A non-system application calls a system API. | 398| 890001 | Invalid parameter. Possible causes: Parameter verification failed. | 399 400> **说明** 401> 402> 890001的报错信息请以接口的实际报错为准。 403 404**示例:** 405 ```ts 406 import { BusinessError } from '@kit.BasicServicesKit'; 407 408 try { 409 i18n.System.setFirstDayOfWeek(i18n.WeekDay.MON); // 设置用户偏好的周起始日为周一 410 } catch(error) { 411 let err: BusinessError = error as BusinessError; 412 console.error(`call System.setFirstDayOfWeek failed, error code: ${err.code}, message: ${err.message}.`); 413 } 414 ``` 415 416### getSystemCollations<sup>20+</sup> 417 418static getSystemCollations(): Map<string, string> 419 420获取系统支持的排序方式及名称。如系统语言为英文时,可以支持大写在前或小写在前的排序方式。 421 422**系统接口**:此接口为系统接口。 423 424**系统能力**:SystemCapability.Global.I18n 425 426**返回值:** 427 428| 类型 | 说明 | 429| ---------------------- | ----- | 430| Map<string, string> | 系统支持的排序方式及名称。其中Map的key为表示排序方式的字符串,value为表示排序方式对应名称的字符串。支持的范围和系统语言相关。 | 431 432**错误码:** 433 434以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 435 436| 错误码ID | 错误信息 | 437| ------ | ---------------------- | 438| 202 | Permission verification failed. A non-system application calls a system API. | 439 440**示例:** 441```ts 442import { BusinessError } from '@kit.BasicServicesKit'; 443import { i18n } from '@kit.LocalizationKit'; 444 445try { 446 let systemCollations: Map<string, string> = i18n.System.getSystemCollations(); 447} catch(error) { 448 let err: BusinessError = error as BusinessError; 449 console.error(`call System.getSystemCollations failed, error code: ${err.code}, message: ${err.message}.`); 450} 451``` 452 453### getUsingCollation<sup>20+</sup> 454 455static getUsingCollation(): string 456 457获取系统当前使用的排序方式。 458 459**系统接口**:此接口为系统接口。 460 461**系统能力**:SystemCapability.Global.I18n 462 463**返回值:** 464 465| 类型 | 说明 | 466| ---------------------- | ----- | 467| string | 系统当前使用的排序方式。 | 468 469**错误码:** 470 471以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 472 473| 错误码ID | 错误信息 | 474| ------ | ---------------------- | 475| 202 | Permission verification failed. A non-system application calls a system API. | 476 477**示例:** 478```ts 479import { BusinessError } from '@kit.BasicServicesKit'; 480import { i18n } from '@kit.LocalizationKit'; 481 482try { 483 let usingCollation: string = i18n.System.getUsingCollation(); 484} catch(error) { 485 let err: BusinessError = error as BusinessError; 486 console.error(`call System.getUsingCollation failed, error code: ${err.code}, message: ${err.message}.`); 487} 488``` 489 490### setSystemCollation<sup>20+</sup> 491 492static setSystemCollation(identifier: string): void 493 494设置系统的排序方式。 495 496**系统接口**:此接口为系统接口。 497 498**需要权限**:ohos.permission.UPDATE_CONFIGURATION 499 500**系统能力**:SystemCapability.Global.I18n 501 502**参数:** 503 504| 参数名 | 类型 | 必填 | 说明 | 505| ---- | ------- | ---- | ------------------------------- | 506| identifier | string | 是 | 系统支持的排序方式。支持的范围可以通过[getSystemCollations](#getsystemcollations20)获取。 | 507 508**错误码:** 509 510以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 511 512| 错误码ID | 错误信息 | 513| ------ | ---------------------- | 514| 201 | Permission verification failed. The application does not have the permission required to call the API. | 515| 202 | Permission verification failed. A non-system application calls a system API. | 516| 8900001 | Invalid parameter. Possible causes: Parameter verification failed. | 517 518**示例:** 519```ts 520import { BusinessError } from '@kit.BasicServicesKit'; 521import { i18n } from '@kit.LocalizationKit'; 522 523try { 524 i18n.System.setSystemCollation("zhuyin"); // 如果设置当前系统不支持的排序方式会报错 525} catch(error) { 526 let err: BusinessError = error as BusinessError; 527 console.error(`call System.setSystemCollation failed, error code: ${err.code}, message: ${err.message}.`); 528} 529``` 530 531### getSystemNumberingSystems<sup>20+</sup> 532 533static getSystemNumberingSystems(): Map<string, string> 534 535获取系统支持的数字系统及示例。示例为数字0~9在对应数字系统下的显示。 536 537**系统接口**:此接口为系统接口。 538 539**系统能力**:SystemCapability.Global.I18n 540 541**返回值:** 542 543| 类型 | 说明 | 544| ---------------------- | ----- | 545| Map<string, string> | 系统支持的数字系统及示例。其中Map的key为表示数字系统的字符串,value为表示数字系统对应的示例。支持的范围和系统语言相关。 | 546 547**错误码:** 548 549以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 550 551| 错误码ID | 错误信息 | 552| ------ | ---------------------- | 553| 202 | Permission verification failed. A non-system application calls a system API. | 554 555**示例:** 556```ts 557import { BusinessError } from '@kit.BasicServicesKit'; 558import { i18n } from '@kit.LocalizationKit'; 559 560try { 561 let systemNumberingSystems: Map<string, string> = i18n.System.getSystemNumberingSystems(); 562} catch(error) { 563 let err: BusinessError = error as BusinessError; 564 console.error(`call System.getSystemNumberingSystems failed, error code: ${err.code}, message: ${err.message}.`); 565} 566``` 567 568### setSystemNumberingSystem<sup>20+</sup> 569 570static setSystemNumberingSystem(identifier: string):void 571 572设置系统的数字系统。 573 574**系统接口**:此接口为系统接口。 575 576**需要权限**:ohos.permission.UPDATE_CONFIGURATION 577 578**系统能力**:SystemCapability.Global.I18n 579 580**参数:** 581 582| 参数名 | 类型 | 必填 | 说明 | 583| ---- | ------- | ---- | ------------------------------- | 584| identifier | string | 是 | 系统支持的数字系统。支持的范围可以通过[getSystemNumberingSystems](#getsystemnumberingsystems20)获取。 | 585 586**错误码:** 587 588以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 589 590| 错误码ID | 错误信息 | 591| ------ | ---------------------- | 592| 201 | Permission verification failed. The application does not have the permission required to call the API. | 593| 202 | Permission verification failed. A non-system application calls a system API. | 594| 8900001 | Invalid parameter. Possible causes: Parameter verification failed. | 595 596**示例:** 597```ts 598import { BusinessError } from '@kit.BasicServicesKit'; 599import { i18n } from '@kit.LocalizationKit'; 600 601try { 602 i18n.System.setSystemNumberingSystem("arab"); // 如果设置当前系统不支持的数字系统会报错 603} catch(error) { 604 let err: BusinessError = error as BusinessError; 605 console.error(`call System.setSystemNumberingSystem failed, error code: ${err.code}, message: ${err.message}.`); 606} 607``` 608 609### getSystemNumberPatterns<sup>20+</sup> 610 611static getSystemNumberPatterns(): Map<string, string> 612 613获取系统支持的数字格式及示例。数字格式指数字中的千分符和小数分隔符的格式。 614 615**系统接口**:此接口为系统接口。 616 617**系统能力**:SystemCapability.Global.I18n 618 619**返回值:** 620 621| 类型 | 说明 | 622| ---------------------- | ----- | 623| Map<string, string> | 系统支持的数字格式及示例。其中Map的key表示数字格式,是千分符和小数分隔符的unicode编码,value表示数字格式对应的示例。支持的范围和系统语言地区相关。 | 624 625**错误码:** 626 627以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 628 629| 错误码ID | 错误信息 | 630| ------ | ---------------------- | 631| 202 | Permission verification failed. A non-system application calls a system API. | 632 633**示例:** 634```ts 635import { BusinessError } from '@kit.BasicServicesKit'; 636import { i18n } from '@kit.LocalizationKit'; 637 638try { 639 let systemNumberPatterns: Map<string, string> = i18n.System.getSystemNumberPatterns(); 640} catch(error) { 641 let err: BusinessError = error as BusinessError; 642 console.error(`call System.getSystemNumberPatterns failed, error code: ${err.code}, message: ${err.message}.`); 643} 644``` 645 646### getUsingNumberPattern<sup>20+</sup> 647 648static getUsingNumberPattern(): string 649 650获取系统当前使用的数字格式。 651 652**系统接口**:此接口为系统接口。 653 654**系统能力**:SystemCapability.Global.I18n 655 656**返回值:** 657 658| 类型 | 说明 | 659| ---------------------- | ----- | 660| string | 系统当前使用的数字格式。 | 661 662**错误码:** 663 664以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 665 666| 错误码ID | 错误信息 | 667| ------ | ---------------------- | 668| 202 | Permission verification failed. A non-system application calls a system API. | 669 670**示例:** 671```ts 672import { BusinessError } from '@kit.BasicServicesKit'; 673import { i18n } from '@kit.LocalizationKit'; 674 675try { 676 let usingNumberPattern: string = i18n.System.getUsingNumberPattern(); 677} catch(error) { 678 let err: BusinessError = error as BusinessError; 679 console.error(`call System.getUsingNumberPattern failed, error code: ${err.code}, message: ${err.message}.`); 680} 681``` 682 683### setSystemNumberPattern<sup>20+</sup> 684 685static setSystemNumberPattern(pattern: string): void 686 687设置系统的数字格式。 688 689**系统接口**:此接口为系统接口。 690 691**需要权限**:ohos.permission.UPDATE_CONFIGURATION 692 693**系统能力**:SystemCapability.Global.I18n 694 695**参数:** 696 697| 参数名 | 类型 | 必填 | 说明 | 698| ---- | ------- | ---- | ------------------------------- | 699| pattern | string | 是 | 系统支持的数字格式。支持的范围可以通过[getSystemNumberPatterns](#getsystemnumberpatterns20)获取。 | 700 701**错误码:** 702 703以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 704 705| 错误码ID | 错误信息 | 706| ------ | ---------------------- | 707| 201 | Permission verification failed. The application does not have the permission required to call the API. | 708| 202 | Permission verification failed. A non-system application calls a system API. | 709| 8900001 | Invalid parameter. Possible causes: Parameter verification failed. | 710 711**示例:** 712```ts 713import { BusinessError } from '@kit.BasicServicesKit'; 714import { i18n } from '@kit.LocalizationKit'; 715 716try { 717 i18n.System.setSystemNumberPattern("002e002c"); // 如果设置当前系统不支持的数字格式会报错 718} catch(error) { 719 let err: BusinessError = error as BusinessError; 720 console.error(`call System.setSystemNumberPattern failed, error code: ${err.code}, message: ${err.message}.`); 721} 722``` 723 724### getSystemMeasurements<sup>20+</sup> 725 726static getSystemMeasurements(): Map<string, string> 727 728获取系统支持的度量衡及其名称。 729 730**系统接口**:此接口为系统接口。 731 732**系统能力**:SystemCapability.Global.I18n 733 734**返回值:** 735 736| 类型 | 说明 | 737| ---------------------- | ----- | 738| Map<string, string> | 系统支持的度量衡及其名称。其中Map的key表示度量衡的标识,value表示度量衡的名称。支持的度量衡如下:<br>- metric:公制。<br>- uksystem:英制。<br>- ussystem:美制。 | 739 740**错误码:** 741 742以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 743 744| 错误码ID | 错误信息 | 745| ------ | ---------------------- | 746| 202 | Permission verification failed. A non-system application calls a system API. | 747 748**示例:** 749```ts 750import { BusinessError } from '@kit.BasicServicesKit'; 751import { i18n } from '@kit.LocalizationKit'; 752 753try { 754 let systemMeasurements: Map<string, string> = i18n.System.getSystemMeasurements(); 755} catch(error) { 756 let err: BusinessError = error as BusinessError; 757 console.error(`call System.getSystemMeasurements failed, error code: ${err.code}, message: ${err.message}.`); 758} 759``` 760 761### getUsingMeasurement<sup>20+</sup> 762 763static getUsingMeasurement(): string 764 765获取系统当前使用的度量衡。 766 767**系统接口**:此接口为系统接口。 768 769**系统能力**:SystemCapability.Global.I18n 770 771**返回值:** 772 773| 类型 | 说明 | 774| ---------------------- | ----- | 775| string | 系统当前使用的度量衡,取值及对应含义如下:<br>- metric:公制。<br>- uksystem:英制。<br>- ussystem:美制。 | 776 777**错误码:** 778 779以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 780 781| 错误码ID | 错误信息 | 782| ------ | ---------------------- | 783| 202 | Permission verification failed. A non-system application calls a system API. | 784 785**示例:** 786```ts 787import { BusinessError } from '@kit.BasicServicesKit'; 788import { i18n } from '@kit.LocalizationKit'; 789 790try { 791 let usingMeasurement: string = i18n.System.getUsingMeasurement(); 792} catch(error) { 793 let err: BusinessError = error as BusinessError; 794 console.error(`call System.getUsingMeasurement failed, error code: ${err.code}, message: ${err.message}.`); 795} 796``` 797 798### setSystemMeasurement<sup>20+</sup> 799 800static setSystemMeasurement(identifier: string): void 801 802设置系统的度量衡。 803 804**系统接口**:此接口为系统接口。 805 806**需要权限**:ohos.permission.UPDATE_CONFIGURATION 807 808**系统能力**:SystemCapability.Global.I18n 809 810**参数:** 811 812| 参数名 | 类型 | 必填 | 说明 | 813| ---- | ------- | ---- | ------------------------------- | 814| identifier | string | 是 | 系统支持的度量衡。支持的范围可以通过[getSystemMeasurements](#getsystemmeasurements20)获取。 | 815 816**错误码:** 817 818以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 819 820| 错误码ID | 错误信息 | 821| ------ | ---------------------- | 822| 201 | Permission verification failed. The application does not have the permission required to call the API. | 823| 202 | Permission verification failed. A non-system application calls a system API. | 824| 8900001 | Invalid parameter. Possible causes: Parameter verification failed. | 825 826**示例:** 827```ts 828import { BusinessError } from '@kit.BasicServicesKit'; 829import { i18n } from '@kit.LocalizationKit'; 830 831try { 832 i18n.System.setSystemMeasurement("uksystem"); // 如果设置当前系统不支持的度量衡会抛8900001错误码 833} catch(error) { 834 let err: BusinessError = error as BusinessError; 835 console.error(`call System.setSystemMeasurement failed, error code: ${err.code}, message: ${err.message}.`); 836} 837``` 838 839### getSystemNumericalDatePatterns<sup>20+</sup> 840 841static getSystemNumericalDatePatterns(): Map<string, string> 842 843获取系统支持的数字日期格式及其示例。 844 845**系统接口**:此接口为系统接口。 846 847**系统能力**:SystemCapability.Global.I18n 848 849**返回值:** 850 851| 类型 | 说明 | 852| ---------------------- | ----- | 853| Map<string, string> | 获取系统支持的数字日期格式及其示例。其中Map的key表示数字日期格式,形如`dd/MM/y`;value表示数字日期示例,形如`18/07/2025`。 | 854 855**错误码:** 856 857以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 858 859| 错误码ID | 错误信息 | 860| ------ | ---------------------- | 861| 202 | Permission verification failed. A non-system application calls a system API. | 862 863**示例:** 864```ts 865import { BusinessError } from '@kit.BasicServicesKit'; 866import { i18n } from '@kit.LocalizationKit'; 867 868try { 869 let datePatterns: Map<string, string> = i18n.System.getSystemNumericalDatePatterns(); 870} catch(error) { 871 let err: BusinessError = error as BusinessError; 872 console.error(`call System.getSystemNumericalDatePatterns failed, error code: ${err.code}, message: ${err.message}.`); 873} 874``` 875 876### getUsingNumericalDatePattern<sup>20+</sup> 877 878static getUsingNumericalDatePattern(): string 879 880获取系统当前使用的数字日期格式。 881 882**系统接口**:此接口为系统接口。 883 884**系统能力**:SystemCapability.Global.I18n 885 886**返回值:** 887 888| 类型 | 说明 | 889| ---------------------- | ----- | 890| string | 系统当前使用的数字日期格式。 | 891 892**错误码:** 893 894以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 895 896| 错误码ID | 错误信息 | 897| ------ | ---------------------- | 898| 202 | Permission verification failed. A non-system application calls a system API. | 899 900**示例:** 901```ts 902import { BusinessError } from '@kit.BasicServicesKit'; 903import { i18n } from '@kit.LocalizationKit'; 904 905try { 906 let datePattern: string = i18n.System.getUsingNumericalDatePattern(); 907} catch(error) { 908 let err: BusinessError = error as BusinessError; 909 console.error(`call System.getUsingNumericalDatePattern failed, error code: ${err.code}, message: ${err.message}.`); 910} 911``` 912 913### setSystemNumericalDatePattern<sup>20+</sup> 914 915static setSystemNumericalDatePattern(identifier: string): void 916 917设置系统的数字日期格式。 918 919**系统接口**:此接口为系统接口。 920 921**需要权限**:ohos.permission.UPDATE_CONFIGURATION 922 923**系统能力**:SystemCapability.Global.I18n 924 925**参数:** 926 927| 参数名 | 类型 | 必填 | 说明 | 928| ---- | ------- | ---- | ------------------------------- | 929| identifier | string | 是 | 系统支持的数字日期格式。支持的范围可以通过[getSystemNumericalDatePatterns](#getsystemnumericaldatepatterns20)获取。 | 930 931**错误码:** 932 933以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 934 935| 错误码ID | 错误信息 | 936| ------ | ---------------------- | 937| 201 | Permission verification failed. The application does not have the permission required to call the API. | 938| 202 | Permission verification failed. A non-system application calls a system API. | 939| 8900001 | Invalid parameter. Possible causes: Parameter verification failed. | 940 941**示例:** 942```ts 943import { BusinessError } from '@kit.BasicServicesKit'; 944import { i18n } from '@kit.LocalizationKit'; 945 946try { 947 i18n.System.setSystemNumericalDatePattern("dd/MM/y"); // 如果设置当前系统不支持的数字日期格式,系统会抛出8900001错误码 948} catch(error) { 949 let err: BusinessError = error as BusinessError; 950 console.error(`call System.setSystemNumericalDatePattern failed, error code: ${err.code}, message: ${err.message}.`); 951} 952``` 953 954## SystemLocaleManager<sup>10+</sup> 955 956提供语言、地区和时区信息排序的能力。 957 958### constructor<sup>10+</sup> 959 960constructor() 961 962创建SystemLocaleManager对象。 963 964**系统接口**:此接口为系统接口。 965 966**系统能力**:SystemCapability.Global.I18n 967 968**示例:** 969 ```ts 970 let systemLocaleManager: i18n.SystemLocaleManager = new i18n.SystemLocaleManager(); 971 ``` 972 973 974### getLanguageInfoArray<sup>10+</sup> 975 976getLanguageInfoArray(languages: Array<string>, options?: SortOptions): Array<LocaleItem> 977 978获取排序后的语言信息列表。 979 980**系统接口**:此接口为系统接口。 981 982**系统能力**:SystemCapability.Global.I18n 983 984**参数:** 985 986| 参数名 | 类型 | 必填 | 说明 | 987| --------- | ------------- | ---- | ------------- | 988| languages | Array<string> | 是 | 待排序的语言列表,要求是合法的语言ID。| 989| options | [SortOptions](#sortoptions10) | 否 | 语言排序选项。 | 990 991**返回值:** 992 993| 类型 | 说明 | 994| ----------------- | -------------------- | 995| Array<[LocaleItem](#localeitem10)> | 排序后的语言信息列表。 | 996 997**错误码:** 998 999以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 1000 1001| 错误码ID | 错误信息 | 1002| ------ | ---------------------- | 1003| 202 | Permission verification failed. A non-system application calls a system API. | 1004| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 1005| 890001 | Invalid parameter. Possible causes: Parameter verification failed. | 1006 1007**示例:** 1008 ```ts 1009 import { BusinessError } from '@kit.BasicServicesKit'; 1010 1011 // 当系统语言为zh-Hans,系统地区为CN,系统区域为zh-Hans-CN时 1012 let systemLocaleManager: i18n.SystemLocaleManager = new i18n.SystemLocaleManager(); 1013 let languages: string[] = ["zh-Hans", "en-US", "pt", "ar"]; 1014 let sortOptions: i18n.SortOptions = {locale: "zh-Hans-CN", isUseLocalName: true, isSuggestedFirst: true}; 1015 try { 1016 // 排序后的语言顺序为: [zh-Hans, en-US, pt, ar] 1017 let sortedLanguages: Array<i18n.LocaleItem> = systemLocaleManager.getLanguageInfoArray(languages, sortOptions); 1018 } catch(error) { 1019 let err: BusinessError = error as BusinessError; 1020 console.error(`call systemLocaleManager.getLanguageInfoArray failed, error code: ${err.code}, message: ${err.message}.`); 1021 } 1022 ``` 1023 1024 1025### getRegionInfoArray<sup>10+</sup> 1026 1027getRegionInfoArray(regions: Array<string>, options?: SortOptions): Array<LocaleItem> 1028 1029获取排序后的国家或地区信息列表。 1030 1031**系统接口**:此接口为系统接口。 1032 1033**系统能力**:SystemCapability.Global.I18n 1034 1035**参数:** 1036 1037| 参数名 | 类型 | 必填 | 说明 | 1038| --------- | ------------- | ---- | ------------- | 1039| regions | Array<string> | 是 | 待排序的国家或地区列表,要求是合法的国家或地区ID。| 1040| options | [SortOptions](#sortoptions10) | 否 | 国家或地区排序选项。<br>区域ID的默认值为系统当前区域ID,isUseLocalName的默认值为false,isSuggestedFirst的默认值为true。 | 1041 1042**返回值:** 1043 1044| 类型 | 说明 | 1045| ----------------- | -------------------- | 1046| Array<[LocaleItem](#localeitem10)> | 排序后的国家或地区信息列表。 | 1047 1048**错误码:** 1049 1050以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 1051 1052| 错误码ID | 错误信息 | 1053| ------ | ---------------------- | 1054| 202 | Permission verification failed. A non-system application calls a system API. | 1055| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 1056| 890001 | Invalid parameter. Possible causes: Parameter verification failed. | 1057 1058**示例:** 1059 ```ts 1060 import { BusinessError } from '@kit.BasicServicesKit'; 1061 1062 // 当系统语言为zh-Hans,系统地区为CN,系统区域为zh-Hans-CN时 1063 let systemLocaleManager: i18n.SystemLocaleManager = new i18n.SystemLocaleManager(); 1064 let regions: string[] = ["CN", "US", "PT", "EG"]; 1065 let sortOptions: i18n.SortOptions = {locale: "zh-Hans-CN", isUseLocalName: false, isSuggestedFirst: true}; 1066 try { 1067 // 排序后的地区顺序为: [CN, EG, US, PT] 1068 let sortedRegions: Array<i18n.LocaleItem> = systemLocaleManager.getRegionInfoArray(regions, sortOptions); 1069 } catch(error) { 1070 let err: BusinessError = error as BusinessError; 1071 console.error(`call systemLocaleManager.getRegionInfoArray failed, error code: ${err.code}, message: ${err.message}.`); 1072 } 1073 ``` 1074 1075### getTimeZoneCityItemArray<sup>10+</sup> 1076 1077static getTimeZoneCityItemArray(): Array<TimeZoneCityItem> 1078 1079获取排序后的时区城市组合信息列表。 1080 1081**系统接口**:此接口为系统接口。 1082 1083**系统能力**:SystemCapability.Global.I18n 1084 1085**返回值:** 1086 1087| 类型 | 说明 | 1088| ----------------- | -------------------- | 1089| Array<[TimeZoneCityItem](#timezonecityitem10)> | 排序后的时区城市组合信息列表。 | 1090 1091**错误码:** 1092 1093以下错误码的详细介绍请参见[ohos.i18n错误码](errorcode-i18n.md)和[通用错误码](../errorcode-universal.md)。 1094 1095| 错误码ID | 错误信息 | 1096| ------ | ---------------------- | 1097| 202 | Permission verification failed. A non-system application calls a system API. | 1098 1099**示例:** 1100 ```ts 1101 import { BusinessError } from '@kit.BasicServicesKit'; 1102 1103 try { 1104 let timeZoneCityItemArray: Array<i18n.TimeZoneCityItem> = i18n.SystemLocaleManager.getTimeZoneCityItemArray(); 1105 for (let i = 0; i < timeZoneCityItemArray.length; i++) { 1106 console.info(timeZoneCityItemArray[i].zoneId + ", " + timeZoneCityItemArray[i].cityId + ", " + timeZoneCityItemArray[i].cityDisplayName + 1107 ", " + timeZoneCityItemArray[i].offset + "\r\n"); 1108 } 1109 } catch(error) { 1110 let err: BusinessError = error as BusinessError; 1111 console.error(`call SystemLocaleManager.getTimeZoneCityItemArray failed, error code: ${err.code}, message: ${err.message}.`); 1112 } 1113 ``` 1114 1115## LocaleItem<sup>10+</sup> 1116 1117语言或国家地区的组合信息。 1118 1119**系统接口**:此接口为系统接口。 1120 1121**系统能力**:SystemCapability.Global.I18n 1122 1123| 名称 | 类型 | 只读 | 可选 | 说明 | 1124| --------------- | --------------- | ------ | ------ | --------------------------------------- | 1125| id | string | 否 | 否 | 语言代码或国家地区代码,如"zh"、"CN"。 | 1126| suggestionType | [SuggestionType](#suggestiontype10) | 否 | 否 | 语言或国家地区推荐类型。 | 1127| displayName | string | 否 | 否 | id在SystemLocaleManager的指定区域下的表示。| 1128| localName | string | 否 | 是 | id的本地名称。只有在表示语言相关信息时才存在该选项。 | 1129 1130## TimeZoneCityItem<sup>10+</sup> 1131 1132时区城市的组合信息。 1133 1134**系统接口**:此接口为系统接口。 1135 1136**系统能力**:SystemCapability.Global.I18n 1137 1138| 名称 | 类型 | 只读 | 可选 | 说明 | 1139| --------------- | --------------- | ------ | ------ | --------------------------------------- | 1140| zoneId | string | 否 | 否 | 时区ID,例如Asia/Shanghai。 | 1141| cityId | string | 否 | 否 | 城市ID,例如Shanghai。 | 1142| cityDisplayName | string | 否 | 否 | 城市ID在系统区域下显示的名称。 | 1143| offset | number | 否 | 否 | 时区ID的偏移量。 | 1144| zoneDisplayName | string | 否 | 否 | 时区ID在系统区域下显示的名称。 | 1145| rawOffset | number | 否 | 是 | 时区ID的固定偏移量。 | 1146 1147 1148## SuggestionType<sup>10+</sup> 1149 1150语言或国家地区的推荐类型。 1151 1152**系统接口**:此接口为系统接口。 1153 1154**系统能力**:SystemCapability.Global.I18n 1155 1156| 名称 | 值 | 说明 | 1157| ---------------------- | ---- | ---- | 1158| SUGGESTION_TYPE_NONE | 0x00 | 非推荐语言或国家地区。 | 1159| SUGGESTION_TYPE_RELATED| 0x01 | 系统语言推荐的国家地区或系统国家地区推荐的语言。 | 1160| SUGGESTION_TYPE_SIM | 0x02 | Sim卡国家地区推荐的语言。 | 1161 1162 1163## SortOptions<sup>10+<sup> 1164 1165语言或国家地区排序选项。 1166 1167**系统接口**:此接口为系统接口。 1168 1169**系统能力**:SystemCapability.Global.I18n 1170 1171| 名称 | 类型 | 只读 | 可选 | 说明 | 1172| --------------- | --------------- | ---- | ---- | --------------------------------------- | 1173| locale | string | 否 | 是 | [表示区域ID的字符串](../../internationalization/i18n-locale-culture.md#实现原理),由语言、脚本、国家或地区组成,如"zh-Hans-CN"。<br>默认值:系统当前区域ID。 | 1174| isUseLocalName | boolean | 否 | 是 | true表示使用本地名称进行排序,false表示不使用本地名称进行排序。<br>若调用方法为getLanguageInfoArray,isUseLocalName属性默认值为true。<br>若调用方法为getRegionInfoArray,isUseLocalName属性默认值为false。 | 1175| isSuggestedFirst | boolean | 否 | 是 | true表示将推荐语言或国家地区在排序结果中置顶,false表示不将推荐语言或国家地区在排序结果中置顶。<br>默认值:true。 | 1176