1# @ohos.intl (国际化-Intl) 2 3本模块提供基础的应用国际化能力,包括时间日期格式化、数字格式化、排序等,相关接口在ECMA 402标准中定义。 4[I18N模块](js-apis-i18n.md)提供其他非ECMA 402定义的国际化接口,与本模块共同使用可提供完整地国际化支持能力。 5 6> **说明:** 7> 8> - 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 9> 10> - 本模块接口依据[CLDR](https://cldr.unicode.org) 国际化数据库进行处理,随着CLDR演进,本模块接口处理结果可能发生变化。其中,API version 12对应[CLDR 42](https://cldr.unicode.org/index/downloads/cldr-42),详细数据变化请参考官方链接。 11> 12> - 从API version 11开始,本模块部分接口支持在ArkTS卡片中使用。 13> 14> - 从API version 12开始,本模块全接口支持在原子化服务中使用。 15 16 17## 导入模块 18 19```ts 20import { intl } from '@kit.LocalizationKit'; 21``` 22 23## Locale 24 25### 属性 26 27**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。 28 29**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 30 31**系统能力**:SystemCapability.Global.I18n 32 33| 名称 | 类型 | 必填 | 说明 | 34| --------------- | ------- | -------- | ---------------------------------------- | 35| language | string | 是 | 与区域设置相关的语言,如:zh。取值遵循ISO 639标准。 | 36| script | string | 是 | 区域语言的书写方式(脚本),如:Hans。取值遵循Unicode ISO 15924标准。 | 37| region | string | 是 | 与区域设置相关的国家或地区,如:CN。取值遵循ISO 3166标准。 | 38| baseName | string | 是 | Locale的基本信息,由语言、脚本、国家或地区组成,如:zh-Hans-CN。 | 39| caseFirst | string | 是 | 区域的排序规则是否考虑大小写,取值包括:<br>"upper", "lower", "false"。<br>不同取值表示的含义请参考[本地习惯排序表1](../../internationalization/i18n-sorting-local.md)。 | 40| calendar | string | 是 | 区域的日历信息,取值包括:<br>"buddhist", "chinese", "coptic","dangi", "ethioaa", "ethiopic", "gregory", "hebrew", "indian", "islamic", "islamic-umalqura", "islamic-tbla", "islamic-civil", "islamic-rgsa", "iso8601", "japanese", "persian", "roc", "islamicc"。<br>不同取值表示的含义请参考[设置日历和历法表1](../../internationalization/i18n-calendar.md)。 | 41| collation | string | 是 | 区域的排序规则,取值包括:<br>"big5han", "compat", "dict", "direct", "ducet", "eor", "gb2312", "phonebk", "phonetic", "pinyin", "reformed", "searchjl", "stroke", "trad", "unihan", "zhuyin"。<br>不同取值表示的含义请参考[本地习惯排序表1](../../internationalization/i18n-sorting-local.md)。 | 42| hourCycle | string | 是 | 区域的时制信息,取值包括:<br>"h11"、"h12"、"h23"、"h24"。<br>不同取值的显示效果可参考[时间日期国际化表5](../../internationalization/i18n-time-date.md)。 | 43| numberingSystem | string | 是 | 区域使用的数字系统,取值包括:<br>"adlm", "ahom", "arab", "arabext", "bali", "beng", "bhks", "brah", "cakm", "cham", "deva", "diak", "fullwide", "gong", "gonm", "gujr", "guru", "hanidec", "hmng", "hmnp", "java", "kali", "khmr", "knda", "lana", "lanatham", "laoo", "latn", "lepc", "limb", "mathbold", "mathdbl", "mathmono", "mathsanb", "mathsans", "mlym", "modi", "mong", "mroo", "mtei", "mymr", "mymrshan", "mymrtlng", "newa", "nkoo", "olck", "orya", "osma", "rohg", "saur", "segment", "shrd", "sind", "sinh", "sora", "sund", "takr", "talu", "tamldec", "telu", "thai", "tibt", "tirh", "vaii", "wara", "wcho"。 | 44| numeric | boolean | 是 | 是否对数字字符进行特殊的排序规则处理。<br>默认值:false。 | 45 46### constructor<sup>8+</sup> 47 48constructor() 49 50创建区域对象。 51 52**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。 53 54**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 55 56**系统能力**:SystemCapability.Global.I18n 57 58**示例:** 59 ```ts 60 // 默认构造函数使用系统当前locale创建 61 let locale = new intl.Locale(); 62 // 返回系统当前locale 63 let localeID = locale.toString(); 64 ``` 65 66### constructor 67 68constructor(locale: string, options?: LocaleOptions) 69 70创建区域对象。 71 72**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。 73 74**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 75 76**系统能力**:SystemCapability.Global.I18n 77 78**参数:** 79 80| 参数名 | 类型 | 必填 | 说明 | 81| -------------------- | -------------------------------- | ---- | ---------------------------- | 82| locale | string | 是 | 表示区域信息的字符串,由语言、脚本、国家或地区组成。<br>locale可填写组成部分中的一个或多个。| 83| options | [LocaleOptions](#localeoptions) | 否 | 创建区域对象的选项。 | 84 85**示例:** 86 ```ts 87 // 创建 "zh-CN" Locale对象 88 let locale = new intl.Locale("zh-CN"); 89 let localeID = locale.toString(); // localeID = "zh-CN" 90 ``` 91 92 93### toString 94 95toString(): string 96 97获取区域对象的字符串。 98 99**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。 100 101**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 102 103**系统能力**:SystemCapability.Global.I18n 104 105**返回值:** 106 107| 类型 | 说明 | 108| ------ | ----------- | 109| string | 区域对象的字符串。 | 110 111**示例:** 112 ```ts 113 // 创建 "en-GB" Locale对象 114 let locale = new intl.Locale("en-GB"); 115 let localeID = locale.toString(); // localeID = "en-GB" 116 ``` 117 118### maximize 119 120maximize(): Locale 121 122最大化区域信息,可补齐Locale中缺少脚本、国家或地区信息。 123 124**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。 125 126**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 127 128**系统能力**:SystemCapability.Global.I18n 129 130**返回值:** 131 132| 类型 | 说明 | 133| ----------------- | ---------- | 134| [Locale](#locale) | 补充完脚本、国家或地区信息后的区域对象。 | 135 136**示例:** 137 ```ts 138 // 创建 "zh" Locale对象 139 let locale = new intl.Locale("zh"); 140 // 补齐Locale对象的脚本和地区 141 let maximizedLocale = locale.maximize(); 142 let localeID = maximizedLocale.toString(); // localeID = "zh-Hans-CN" 143 144 // 创建 "en-US" Locale对象 145 locale = new intl.Locale("en-US"); 146 // 补齐Locale对象的脚本 147 maximizedLocale = locale.maximize(); 148 localeID = maximizedLocale.toString(); // localeID = "en-Latn-US" 149 ``` 150 151 152### minimize 153 154minimize(): Locale 155 156最小化区域信息,可删除Locale中的脚本、国家或地区信息。 157 158**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。 159 160**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 161 162**系统能力**:SystemCapability.Global.I18n 163 164**返回值:** 165 166| 类型 | 说明 | 167| ----------------- | ---------- | 168| [Locale](#locale) | 删除完脚本、国家或地区信息后的区域对象。 | 169 170**示例:** 171 ```ts 172 // 创建 "zh-Hans-CN" Locale对象 173 let locale = new intl.Locale("zh-Hans-CN"); 174 // 去除Locale对象的脚本和地区 175 let minimizedLocale = locale.minimize(); 176 let localeID = minimizedLocale.toString(); // localeID = "zh" 177 178 // 创建 "en-US" Locale对象 179 locale = new intl.Locale("en-US"); 180 // 去除Locale对象的地区 181 minimizedLocale = locale.minimize(); 182 localeID = minimizedLocale.toString(); // localeID = "en" 183 ``` 184 185## LocaleOptions 186 187区域初始化选项。从API9开始,LocaleOptions属性由必填改为可选。 188 189**卡片能力**:从API version 11开始,该类型支持在ArkTS卡片中使用。 190 191**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 192 193**系统能力**:SystemCapability.Global.I18n 194 195| 名称 | 类型 | 必填 | 说明 | 196| --------------- | ------- | ---- |---------------------------------------- | 197| calendar | string | 否 |日历参数,取值包括:<br>"buddhist", "chinese", "coptic", "dangi", "ethioaa", "ethiopic", "gregory", "hebrew", "indian", "islamic", "islamic-umalqura", "islamic-tbla", "islamic-civil", "islamic-rgsa", "iso8601", "japanese", "persian", "roc", "islamicc"。 | 198| collation | string | 否 |排序参数,取值包括:<br>"big5han", "compat", "dict", "direct", "ducet", "emoji", "eor", "gb2312", "phonebk", "phonetic", "pinyin", "reformed ", "search", "searchjl", "standard", "stroke", "trad", "unihan", "zhuyin"。 | 199| hourCycle | string | 否 |时制格式,取值包括:<br>"h11", "h12", "h23", "h24"。 | 200| numberingSystem | string | 否 |数字系统,取值包括:<br>"adlm", "ahom", "arab", "arabext", "bali", "beng", "bhks", "brah", "cakm", "cham", "deva", "diak", "fullwide", "gong", "gonm", "gujr", "guru", "hanidec", "hmng", "hmnp", "java", "kali", "khmr", "knda", "lana", "lanatham", "laoo", "latn", "lepc", "limb", "mathbold", "mathdbl", "mathmono", "mathsanb", "mathsans", "mlym", "modi", "mong", "mroo", "mtei", "mymr", "mymrshan", "mymrtlng", "newa", "nkoo", "olck", "orya", "osma", "rohg", "saur", "segment", "shrd", "sind", "sinh", "sora", "sund", "takr", "talu", "tamldec", "telu", "thai", "tibt", "tirh", "vaii", "wara", "wcho"。 | 201| numeric | boolean | 否 | 是否对数字字符进行特殊的排序规则处理。默认值:false。 | 202| caseFirst | string | 否 | 表示大写、小写的排序顺序,取值范围:<br>"upper", "lower", "false"。 | 203 204> **说明:** 205> 206> - calendar:不同取值表示的含义请参考[设置日历和历法表1](../../internationalization/i18n-calendar.md)。 207> 208> - hourCycle:不同取值的显示效果可参考[时间日期国际化表5](../../internationalization/i18n-time-date.md)。 209> 210> - collation、caseFirst:不同取值表示的含义请参考[本地习惯排序表1](../../internationalization/i18n-sorting-local.md)。 211 212## DateTimeFormat 213 214### constructor<sup>8+</sup> 215 216constructor() 217 218创建时间、日期格式化对象。 219 220**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。 221 222**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 223 224**系统能力**:SystemCapability.Global.I18n 225 226**示例:** 227 ```ts 228 // 使用系统当前locale创建DateTimeFormat对象 229 let datefmt= new intl.DateTimeFormat(); 230 ``` 231 232### constructor 233 234constructor(locale: string | Array<string>, options?: DateTimeOptions) 235 236创建时间、日期格式化对象。 237 238**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。 239 240**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 241 242**系统能力**:SystemCapability.Global.I18n 243 244**参数:** 245 246| 参数名 | 类型 | 必填 | 说明 | 247| -------------------- | ------------------------------------ | ---- | ---------------------------- | 248| locale | string \| Array<string> | 是 | 表示区域信息的字符串,由语言、脚本、国家或地区组成。<br>locale可填写组成部分中的一个或多个。 | 249| options | [DateTimeOptions](#datetimeoptions) | 否 | 创建时间、日期格式化对象时可设置的配置项。<br>若所有选项均未设置时,year、month、day三个属性的默认值为numeric。 | 250 251**示例:** 252 ```ts 253 // 使用 "zh-CN" locale创建DateTimeFormat对象,日期风格为full,时间风格为medium 254 let datefmt= new intl.DateTimeFormat("zh-CN", { dateStyle: 'full', timeStyle: 'medium' }); 255 256 // 使用 ["ban", "zh"] locale列表创建DateTimeFormat对象,因为ban为非法LocaleID,因此使用zh Locale创建DateTimeFormat对象 257 let datefmt= new intl.DateTimeFormat(["ban", "zh"], { dateStyle: 'full', timeStyle: 'medium' }); 258 ``` 259 260### format 261 262format(date: Date): string 263 264对时间、日期进行格式化。 265 266**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。 267 268**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 269 270**系统能力**:SystemCapability.Global.I18n 271 272**参数:** 273 274| 参数名 | 类型 | 必填 | 说明 | 275| ---- | ---- | ---- | ------- | 276| date | Date | 是 | 时间、日期。说明:月份从0开始计数,如0表示一月。 | 277 278**返回值:** 279 280| 类型 | 说明 | 281| ------ | ------------ | 282| string | 格式化后的时间、日期字符串。 | 283 284**示例:** 285 ```ts 286 let date = new Date(2021, 11, 17, 3, 24, 0); // 时间日期为2021.12.17 03:24:00 287 // 使用 en-GB locale创建DateTimeFormat对象 288 let datefmt = new intl.DateTimeFormat("en-GB"); 289 let formattedDate = datefmt.format(date); // formattedDate "17/12/2021" 290 291 // 使用 en-GB locale创建DateTimeFormat对象,dateStyle设置为full,timeStyle设置为medium 292 datefmt = new intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' }); 293 formattedDate = datefmt.format(date); // formattedDate "Friday, 17 December 2021 at 03:24:00" 294 ``` 295 296### formatRange 297 298formatRange(startDate: Date, endDate: Date): string 299 300对时间段、日期段进行格式化。 301 302**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。 303 304**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 305 306**系统能力**:SystemCapability.Global.I18n 307 308**参数:** 309 310| 参数名 | 类型 | 必填 | 说明 | 311| --------- | ---- | ---- | -------- | 312| startDate | Date | 是 | 时间、日期的开始。说明:月份从0开始计数,如0表示一月。| 313| endDate | Date | 是 | 时间、日期的结束。说明:月份从0开始计数,如0表示一月。| 314 315**返回值:** 316 317| 类型 | 说明 | 318| ------ | -------------- | 319| string | 格式化后的时间段、日期段字符串。 | 320 321**示例:** 322 ```ts 323 let startDate = new Date(2021, 11, 17, 3, 24, 0); // 时间日期为2021.12.17 03:24:00 324 let endDate = new Date(2021, 11, 18, 3, 24, 0); 325 // 使用 en-GB locale创建DateTimeFormat对象 326 let datefmt = new intl.DateTimeFormat("en-GB"); 327 let formattedDateRange = datefmt.formatRange(startDate, endDate); // formattedDateRange = "17/12/2021 - 18/12/2021" 328 ``` 329 330### resolvedOptions 331 332resolvedOptions(): DateTimeOptions 333 334获取创建时间、日期格式化对象时设置的配置项。 335 336**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。 337 338**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 339 340**系统能力**:SystemCapability.Global.I18n 341 342**返回值:** 343 344| 类型 | 说明 | 345| ------------------------------------ | ----------------------------- | 346| [DateTimeOptions](#datetimeoptions) | 时间、日期格式化对象设置的配置项。 | 347 348**示例:** 349 ```ts 350 let datefmt = new intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' }); 351 // 返回DateTimeFormat对象的配置项 352 let options = datefmt.resolvedOptions(); 353 let dateStyle = options.dateStyle; // dateStyle = "full" 354 let timeStyle = options.timeStyle; // timeStyle = "medium" 355 ``` 356 357 358## DateTimeOptions 359 360时间、日期格式化时可设置的配置项。从API9开始,DateTimeOptions的属性由必填改为可选。 361 362**卡片能力**:从API version 11开始,该类型支持在ArkTS卡片中使用。 363 364**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 365 366**系统能力**:SystemCapability.Global.I18n 367 368| 名称 | 类型 | 必填 | 说明 | 369| --------------- | ------- | ---- | ---------------------------------------- | 370| locale | string | 否 |区域参数, 如:zh-Hans-CN。 | 371| dateStyle | string | 否 |日期显示格式,取值包括:<br>"long", "short", "medium", "full", "auto"。<br>不同取值的显示效果请参考[时间日期国际化表1](../../internationalization/i18n-time-date.md)。 | 372| timeStyle | string | 否 |时间显示格式,取值包括:<br>"long", "short", "medium", "full", "auto"。<br>不同取值的显示效果请参考[时间日期国际化表2](../../internationalization/i18n-time-date.md)。 | 373| hourCycle | string | 否 |时制格式,取值包括:<br>"h11", "h12", "h23", "h24"。<br>不设置dateStyle或timeStyle参数时的显示效果请参考[时间日期国际化表5](../../internationalization/i18n-time-date.md)。<br>设置dateStyle或timeStyle参数时的显示效果请参考[时间日期国际化表6](../../internationalization/i18n-time-date.md)。 | 374| timeZone | string | 否 |使用的时区,取值为合法的IANA时区ID。 | 375| numberingSystem | string | 否 |数字系统,取值包括:<br>"adlm", "ahom", "arab", "arabext", "bali", "beng", "bhks", "brah", "cakm", "cham", "deva", "diak", "fullwide", "gong", "gonm", "gujr", "guru", "hanidec", "hmng", "hmnp", "java", "kali", "khmr", "knda", "lana", "lanatham", "laoo", "latn", "lepc", "limb", "mathbold", "mathdbl", "mathmono", "mathsanb", "mathsans", "mlym", "modi", "mong", "mroo", "mtei", "mymr", "mymrshan", "mymrtlng", "newa", "nkoo", "olck", "orya", "osma", "rohg", "saur", "segment", "shrd", "sind", "sinh", "sora", "sund", "takr", "talu", "tamldec", "telu", "thai", "tibt", "tirh", "vaii", "wara", "wcho"。 | 376| hour12 | boolean | 否 | 是否使用12小时制,<br>同时设置hour12和hourCycle时,hourCycle不生效。<br>若hour12和hourCycle未设置且系统24小时开关打开时,hour12属性的默认值为false。| 377| weekday | string | 否 | 星期的显示格式,取值包括:<br>"long", "short", "narrow", "auto"。<br>不同取值的显示效果请参考[时间日期国际化表4](../../internationalization/i18n-time-date.md)。 | 378| era | string | 否 | 纪元的显示格式,取值包括:<br>"long", "short", "narrow", "auto"。<br>不同取值的显示效果请参考[时间日期国际化表9](../../internationalization/i18n-time-date.md)。 | 379| year | string | 否 | 年份的显示格式,取值包括:<br>"numeric", "2-digit"。<br>不同取值的显示效果请参考[时间日期国际化表3](../../internationalization/i18n-time-date.md)。 | 380| month | string | 否 | 月份的显示格式,取值包括:<br>"numeric", "2-digit", "long", "short", "narrow", "auto"。<br>不同取值的显示效果请参考[时间日期国际化表6](../../internationalization/i18n-time-date.md)。 | 381| day | string | 否 | 日期的显示格式,取值包括:<br>"numeric", "2-digit"。 | 382| hour | string | 否 | 小时的显示格式,取值包括:<br>"numeric", "2-digit"。 | 383| minute | string | 否 | 分钟的显示格式,取值包括:<br>"numeric", "2-digit"。 | 384| second | string | 否 | 秒钟的显示格式,取值包括:<br>"numeric", "2-digit"。 | 385| timeZoneName | string | 否 | 时区名称的本地化表示,取值包括:<br>"long", "short", "auto"。<br>不同取值的显示效果请参考[时间日期国际化表8](../../internationalization/i18n-time-date.md)。 | 386| dayPeriod | string | 否 | 时段的显示格式,取值包括:<br>"long", "short", "narrow", "auto"。<br>不同取值的显示效果请参考[时间日期国际化表10](../../internationalization/i18n-time-date.md)。 | 387| localeMatcher | string | 否 | 要使用的区域匹配算法,取值包括:<br>"lookup":精确匹配;<br>"best fit":最佳匹配。 | 388| formatMatcher | string | 否 | 要使用的格式匹配算法,取值包括:<br>"basic":精确匹配;<br>"best fit":最佳匹配。 | 389 390## NumberFormat 391 392### constructor<sup>8+</sup> 393 394constructor() 395 396创建数字格式化对象。 397 398**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 399 400**系统能力**:SystemCapability.Global.I18n 401 402**示例:** 403 ```ts 404 // 使用系统当前locale创建NumberFormat对象 405 let numfmt = new intl.NumberFormat(); 406 ``` 407 408 409### constructor 410 411constructor(locale: string | Array<string>, options?: NumberOptions) 412 413创建数字格式化对象。 414 415**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 416 417**系统能力**:SystemCapability.Global.I18n 418 419**参数:** 420 421| 参数名 | 类型 | 必填 | 说明 | 422| -------------------- | -------------------------------- | ---- | ---------------------------- | 423| locale | string \| Array<string> | 是 | 表示区域信息的字符串,由语言、脚本、国家或地区组成。 | 424| options | [NumberOptions](#numberoptions) | 否 | 创建数字格式化对象时可设置的配置项。 | 425 426**示例:** 427 ```ts 428 // 使用 en-GB locale创建NumberFormat对象,style设置为decimal,notation设置为scientific 429 let numfmt = new intl.NumberFormat("en-GB", {style:'decimal', notation:"scientific"}); 430 ``` 431 432### format 433 434format(number: number): string 435 436格式化数字字符串。 437 438**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 439 440**系统能力**:SystemCapability.Global.I18n 441 442**参数:** 443 444| 参数名 | 类型 | 必填 | 说明 | 445| ------ | ------ | ---- | ---- | 446| number | number | 是 | 数字对象。 | 447 448**返回值:** 449 450| 类型 | 说明 | 451| ------ | ---------- | 452| string | 格式化后的数字字符串。 | 453 454 455**示例:** 456 ```ts 457 // 使用 ["en-GB", "zh"] locale列表创建NumberFormat对象,因为en-GB为合法LocaleID,因此使用en-GB创建NumberFormat对象 458 let numfmt = new intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"}); 459 let formattedNumber = numfmt.format(1223); // formattedNumber = 1.223E3 460 ``` 461 462 463### resolvedOptions 464 465resolvedOptions(): NumberOptions 466 467获取创建数字格式化对象时设置的配置项。 468 469**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 470 471**系统能力**:SystemCapability.Global.I18n 472 473**返回值:** 474 475| 类型 | 说明 | 476| -------------------------------- | --------------------------- | 477| [NumberOptions](#numberoptions) | 创建数字格式化对象时设置的配置项。 | 478 479 480**示例:** 481 ```ts 482 let numfmt = new intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"}); 483 // 获取NumberFormat对象配置项 484 let options = numfmt.resolvedOptions(); 485 let style = options.style; // style = decimal 486 let notation = options.notation; // notation = scientific 487 ``` 488 489## NumberOptions 490 491创建数字格式化对象时可设置的配置项。从API9开始,NumberOptions的属性由必填改为可选。 492 493**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 494 495**系统能力**:SystemCapability.Global.I18n 496 497| 名称 | 类型 | 必填 | 说明 | 498| ------------------------ | ------- | ---- | ---------------------------------------- | 499| locale | string | 否 | 区域参数, 如:"zh-Hans-CN"。<br>locale属性默认值为系统当前Locale。 | 500| currency | string | 否 | 货币单位, 取值符合[ISO-4217标准](https://www.iso.org/iso-4217-currency-codes.html),如:"EUR","CNY","USD"等。<br>从API version 12开始支持三位数字代码,如:"978","156","840"等。 | 501| currencySign | string | 否 | 货币单位的符号显示,取值包括: "standard","accounting"。<br>默认值为standard。 | 502| currencyDisplay | string | 否 | 货币的显示方式,取值包括:"symbol", "narrowSymbol", "code", "name"。<br>默认值为symbol。 | 503| unit | string | 否 | 单位名称,如:"meter","inch",“hectare”等。 | 504| unitDisplay | string | 否 | 单位的显示格式,取值包括:"long", "short", "narrow"。<br>默认值为short。 | 505| unitUsage<sup>8+</sup> | string | 否 | 单位的使用场景,取值包括:"default", "area-land-agricult", "area-land-commercl", "area-land-residntl", "length-person", "length-person-small", "length-rainfall", "length-road", "length-road-small", "length-snowfall", "length-vehicle", "length-visiblty", "length-visiblty-small", "length-person-informal", "length-person-small-informal", "length-road-informal", "speed-road-travel", "speed-wind", "temperature-person", "temperature-weather", "volume-vehicle-fuel", "elapsed-time-second", "size-file-byte", "size-shortfile-byte"。<br>默认值为default。 | 506| signDisplay | string | 否 | 数字符号的显示格式,取值包括:<br>"auto":自动判断是否显示正负符号;<br>"never":不显示正负号;<br>"always":总是显示正负号;<br>"exceptZero":除了0都显示正负号。<br>默认值为auto。 | 507| compactDisplay | string | 否 | 紧凑型的显示格式,取值包括:"long", "short"。<br>默认值为short。 | 508| notation | string | 否 | 数字的格式化规格,取值包括:"standard", "scientific", "engineering", "compact"。<br>默认值为standard。 | 509| localeMatcher | string | 否 | 要使用的区域匹配算法,取值包括:"lookup", "best fit"。<br>默认值为best fit。 | 510| style | string | 否 | 数字的显示格式,取值包括:"decimal", "currency", "percent", "unit"。<br>默认值为decimal。 | 511| numberingSystem | string | 否 | 数字系统,取值包括:<br>"adlm", "ahom", "arab", "arabext", "bali", "beng", "bhks", "brah", "cakm", "cham", "deva", "diak", "fullwide", "gong", "gonm", "gujr", "guru", "hanidec", "hmng", "hmnp", "java", "kali", "khmr", "knda", "lana", "lanatham", "laoo", "latn", "lepc", "limb", "mathbold", "mathdbl", "mathmono", "mathsanb", "mathsans", "mlym", "modi", "mong", "mroo", "mtei", "mymr", "mymrshan", "mymrtlng", "newa", "nkoo", "olck", "orya", "osma", "rohg", "saur", "segment", "shrd", "sind", "sinh", "sora", "sund", "takr", "talu", "tamldec", "telu", "thai", "tibt", "tirh", "vaii", "wara", "wcho"。numberingSystem属性默认值为locale的默认数字系统。 | 512| useGrouping | boolean | 否 | 是否分组显示。useGrouping属性默认值为auto。 | 513| minimumIntegerDigits | number | 否 | 表示要使用的最小整数位数,取值范围:1~21。<br>minimumIntegerDigits属性默认值为1。 | 514| minimumFractionDigits | number | 否 | 表示要使用的最小分数位数,取值范围:0~20。<br>minimumFractionDigits属性默认值为0。 | 515| maximumFractionDigits | number | 否 | 表示要使用的最大分数位数,取值范围:1~21。<br>maximumFractionDigits属性默认值为3。 | 516| minimumSignificantDigits | number | 否 | 表示要使用的最低有效位数,取值范围:1~21。<br>minimumSignificantDigits属性默认值为1。 | 517| maximumSignificantDigits | number | 否 | 表示要使用的最大有效位数,取值范围:1~21。<br>maximumSignificantDigits属性默认值为21。 | 518 519> **说明:** 520> 521> - 各属性不同取值代表的含义或呈现效果,请参考[数字与度量衡国际化](../../internationalization/i18n-numbers-weights-measures.md)。 522 523## Collator<sup>8+</sup> 524 525### constructor<sup>8+</sup> 526 527constructor() 528 529创建排序对象。 530 531**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 532 533**系统能力**:SystemCapability.Global.I18n 534 535**示例:** 536 ```ts 537 // 使用系统locale创建Collator对象 538 let collator = new intl.Collator(); 539 ``` 540 541 542### constructor<sup>8+</sup> 543 544constructor(locale: string | Array<string>, options?: CollatorOptions) 545 546创建排序对象。 547 548**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 549 550**系统能力**:SystemCapability.Global.I18n 551 552**参数:** 553 554| 参数名 | 类型 | 必填 | 说明 | 555| -------------------- | ------------------------------------ | ---- | ---------------------------- | 556| locale | string \| Array<string> | 是 | 表示区域信息的字符串,由语言、脚本、国家或地区组成。 | 557| options | [CollatorOptions](#collatoroptions8) | 否 | 创建排序对象时可设置的配置项。 | 558 559**示例:** 560 ```ts 561 // 使用 zh-CN locale创建Collator对象,localeMatcher设置为lookup,usage设置为sort 562 let collator = new intl.Collator("zh-CN", {localeMatcher: "lookup", usage: "sort"}); 563 ``` 564 565 566### compare<sup>8+</sup> 567 568compare(first: string, second: string): number 569 570依据配置项设置的排序规则,比较两个字符串。 571 572**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 573 574**系统能力**:SystemCapability.Global.I18n 575 576**参数:** 577 578| 参数名 | 类型 | 必填 | 说明 | 579| ------ | ------ | ---- | ------------ | 580| first | string | 是 | 进行比较第一个字符串。 | 581| second | string | 是 | 进行比较的第二个字符串。 | 582 583**返回值:** 584 585| 类型 | 说明 | 586| ------ | ---------------------------------------- | 587| number | 比较结果。<br>- number为负数时,表示first排序在second之前;<br>- number为0时,表示first与second排序相同;<br>- number为正数,表示first排序在second之后。 | 588 589**示例:** 590 ```ts 591 // 使用en-GB locale创建Collator对象 592 let collator = new intl.Collator("en-GB"); 593 // 比较 "first" 和 "second" 的先后顺序 594 let compareResult = collator.compare("first", "second"); // compareResult = -1 595 ``` 596 597 598### resolvedOptions<sup>8+</sup> 599 600resolvedOptions(): CollatorOptions 601 602获取创建排序对象时设置的配置项。 603 604**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 605 606**系统能力**:SystemCapability.Global.I18n 607 608**返回值:** 609 610| 类型 | 说明 | 611| ------------------------------------ | ----------------- | 612| [CollatorOptions](#collatoroptions8) | 返回排序对象的属性。 | 613 614**示例:** 615 ```ts 616 let collator = new intl.Collator("zh-Hans", { usage: 'sort', ignorePunctuation: true }); 617 // 获取Collator对象的配置项 618 let options = collator.resolvedOptions(); 619 let usage = options.usage; // usage = "sort" 620 let ignorePunctuation = options.ignorePunctuation; // ignorePunctuation = true 621 ``` 622 623 624## CollatorOptions<sup>8+</sup> 625 626创建排序对象时可设置的配置项。 627 628从API9中,CollatorOptions中的属性改为可选。 629 630**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 631 632**系统能力**:SystemCapability.Global.I18n 633 634| 名称 | 类型 | 必填 | 说明 | 635| ----------------- | ------- | ---- | ---------------------------------------- | 636| localeMatcher | string | 否 | locale匹配算法,取值范围:"best fit", "lookup"。<br>默认值为best fit。 | 637| usage | string | 否 | 比较的用途,取值范围:"sort", "search"。<br>默认值为sort。 | 638| sensitivity | string | 否 | 表示字符串中的哪些差异会导致非零结果值,取值范围:"base", "accent", "case", "letiant"。<br>默认值为variant。 | 639| ignorePunctuation | boolean | 否 | 表示是否忽略标点符号,取值范围:true, false。<br>默认值为false。 | 640| collation | string | 否 | 排序规则,<br>取值范围:"big5han", "compat", "dict", "direct", "ducet", "eor", "gb2312", "phonebk", "phonetic", "pinyin", "reformed", "searchjl", "stroke", "trad", "unihan", "zhuyin"。<br>默认值为default。 | 641| numeric | boolean | 否 | 是否使用数字排序,取值范围:true, false。<br>默认值为false。 | 642| caseFirst | string | 否 | 表示大写、小写的排序顺序,取值范围:"upper", "lower", "false"。<br>默认值为false。 | 643 644 645> **说明:** 646> 647> - CollatorOptions中属性的不同取值代表的含义请参考[本地习惯排序](../../internationalization/i18n-sorting-local.md)。 648 649 650## PluralRules<sup>8+</sup> 651 652### constructor<sup>8+</sup> 653 654constructor() 655 656创建单复数对象来计算数字的单复数类别。 657 658**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 659 660**系统能力**:SystemCapability.Global.I18n 661 662**示例:** 663 ```ts 664 // 使用系统locale创建PluralRules对象 665 let pluralRules = new intl.PluralRules(); 666 ``` 667 668 669### constructor<sup>8+</sup> 670 671constructor(locale: string | Array<string>, options?: PluralRulesOptions) 672 673创建单复数对象来计算数字的单复数类别。 674 675**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 676 677**系统能力**:SystemCapability.Global.I18n 678 679**参数:** 680 681| 参数名 | 类型 | 必填 | 说明 | 682| -------------------- | ---------------------------------------- | ---- | ---------------------------- | 683| locale | string \| Array<string> | 是 | 表示区域信息的字符串,由语言、脚本、国家或地区组成。 | 684| options | [PluralRulesOptions](#pluralrulesoptions8) | 否 | 创建单复数对象时设置的配置项。 | 685 686**示例:** 687 ```ts 688 // 使用 zh-CN locale创建PluralRules对象,localeMatcher设置为lookup,type设置为cardinal 689 let pluralRules= new intl.PluralRules("zh-CN", {"localeMatcher": "lookup", "type": "cardinal"}); 690 ``` 691 692### select<sup>8+</sup> 693 694select(n: number): string 695 696返回一个字符串表示该数字的单复数类别。 697 698**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 699 700**系统能力**:SystemCapability.Global.I18n 701 702**参数:** 703 704| 参数名 | 类型 | 必填 | 说明 | 705| ---- | ------ | ---- | ------------ | 706| n | number | 是 | 待获取单复数类别的数字。 | 707 708**返回值:** 709 710| 类型 | 说明 | 711| ------ | ---------------------------------------- | 712| string | 单复数类别,取值包括:"zero","one","two", "few", "many", "others"。 <br>不同取值代表的含义请参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。| 713 714**示例:** 715 ```ts 716 // 使用 zh-Hans locale创建PluralRules对象 717 let zhPluralRules = new intl.PluralRules("zh-Hans"); 718 // 计算 zh-Hans locale中数字1对应的单复数类别 719 let plural = zhPluralRules.select(1); // plural = other 720 721 // 使用 en-US locale创建PluralRules对象 722 let enPluralRules = new intl.PluralRules("en-US"); 723 // 计算 en-US locale中数字1对应的单复数类别 724 plural = enPluralRules.select(1); // plural = one 725 ``` 726 727 728## PluralRulesOptions<sup>8+</sup> 729 730创建单复数对象时可设置的配置项。从API9开始,PluralRulesOptions的属性由必填改为可选。 731 732**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 733 734**系统能力**:SystemCapability.Global.I18n 735 736| 名称 | 类型 | 可读 | 可写 | 说明 | 737| ------------------------ | ------ | ---- | ---- | ---------------------------------------- | 738| localeMatcher | string | 是 | 是 | locale匹配算法,取值包括:"best fit", "lookup"。<br>默认值为best fit。 | 739| type | string | 是 | 是 | 排序的类型,取值包括:"cardinal", "ordinal",<br>默认值为cardinal。<br>- cardinal:基数词,ordinal:序数词。 | 740| minimumIntegerDigits | number | 是 | 是 | 表示要使用的最小整数位数,取值范围:1~21。<br>默认值为1。 | 741| minimumFractionDigits | number | 是 | 是 | 表示要使用的最小分数位数,取值范围:0~20。<br>默认值为0。 | 742| maximumFractionDigits | number | 是 | 是 | 表示要使用的最大分数位数,取值范围:1~21。<br>默认值为3。 | 743| minimumSignificantDigits | number | 是 | 是 | 表示要使用的最低有效位数,取值范围:1~21。<br>默认值为1。 | 744| maximumSignificantDigits | number | 是 | 是 | 表示要使用的最大有效位数,取值范围:1~21。<br>默认值为21。 | 745 746 747## RelativeTimeFormat<sup>8+</sup> 748 749### constructor<sup>8+</sup> 750 751constructor() 752 753创建相对时间格式化对象。 754 755**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 756 757**系统能力**:SystemCapability.Global.I18n 758 759**示例:** 760 ```ts 761 // 使用系统locale创建RelativeTimeFormat对象 762 let relativetimefmt = new intl.RelativeTimeFormat(); 763 ``` 764 765 766### constructor<sup>8+</sup> 767 768constructor(locale: string | Array<string>, options?: RelativeTimeFormatInputOptions) 769 770创建相对时间格式化对象。 771 772**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 773 774**系统能力**:SystemCapability.Global.I18n 775 776**参数:** 777 778| 参数名 | 类型 | 必填 | 说明 | 779| -------------------- | ---------------------------------------- | ---- | ---------------------------- | 780| locale | string \| Array<string> | 是 | 表示区域信息的字符串,由语言、脚本、国家或地区组成。 | 781| options | [RelativeTimeFormatInputOptions](#relativetimeformatinputoptions8) | 否 | 创建相对时间格式化对象时可配置的选项。 | 782 783**示例:** 784 ```ts 785 // 使用 zh-CN locale创建RelativeTimeFormat对象,localeMatcher设置为lookup,numeric设置为always,style设置为long 786 let relativeTimeFormat = new intl.RelativeTimeFormat("zh-CN", {"localeMatcher": "lookup", "numeric": "always", "style": "long"}); 787 ``` 788 789 790### format<sup>8+</sup> 791 792format(value: number, unit: string): string 793 794依据locale和格式化选项,对value和unit进行格式化。 795 796**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 797 798**系统能力**:SystemCapability.Global.I18n 799 800**参数:** 801 802| 参数名 | 类型 | 必填 | 说明 | 803| ----- | ------ | ---- | ---------------------------------------- | 804| value | number | 是 | 相对时间格式化的数值。 | 805| unit | string | 是 | 相对时间格式化的单位,<br>取值包括:"year", "quarter", "month", "week", "day", "hour", "minute", "second"。 | 806 807**返回值:** 808 809| 类型 | 说明 | 810| ------ | ---------- | 811| string | 格式化后的相对时间。 | 812 813**示例:** 814 ```ts 815 // 使用 zh-CN locale创建RelativeTimeFormat对象 816 let relativetimefmt = new intl.RelativeTimeFormat("zh-CN"); 817 // 计算 zh-CN locale中数字3,单位quarter的本地化表示 818 let formatResult = relativetimefmt.format(3, "quarter"); // formatResult = "3个季度后" 819 ``` 820 821 822### formatToParts<sup>8+</sup> 823 824formatToParts(value: number, unit: string): Array<object> 825 826自定义区域的相对时间格式。 827 828**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 829 830**系统能力**:SystemCapability.Global.I18n 831 832**参数:** 833 834| 参数名 | 类型 | 必填 | 说明 | 835| ----- | ------ | ---- | ---------------------------------------- | 836| value | number | 是 | 相对时间格式化的数值。 | 837| unit | string | 是 | 相对时间格式化的单位,<br>取值包括:"year", "quarter", "month", "week", "day", "hour", "minute", "second"。 | 838 839**返回值:** 840 841| 类型 | 说明 | 842| ------------------- | --------------------------- | 843| Array<object> | 相对时间格式的对象数组。 | 844 845**示例:** 846 ```ts 847 // 使用 en locale创建RelativeTimeFormat对象,numeric设置为auto 848 let relativetimefmt = new intl.RelativeTimeFormat("en", {"numeric": "auto"}); 849 let parts = relativetimefmt.formatToParts(10, "seconds"); // parts = [ {type: "literal", value: "in"}, {type: "integer", value: 10, unit: "second"}, {type: "literal", value: "seconds"} ] 850 ``` 851 852 853### resolvedOptions<sup>8+</sup> 854 855resolvedOptions(): RelativeTimeFormatResolvedOptions 856 857获取RelativeTimeFormat对象的格式化选项。 858 859**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 860 861**系统能力**:SystemCapability.Global.I18n 862 863**返回值:** 864 865| 类型 | 说明 | 866| ---------------------------------------- | --------------------------------- | 867| [RelativeTimeFormatResolvedOptions](#relativetimeformatresolvedoptions8) | RelativeTimeFormat 对象的格式化选项。 | 868 869**示例:** 870 ```ts 871 // 使用 en-GB locale创建RelativeTimeFormat对象 872 let relativetimefmt= new intl.RelativeTimeFormat("en-GB", { style: "short" }); 873 // 获取RelativeTimeFormat对象配置项 874 let options = relativetimefmt.resolvedOptions(); 875 let style = options.style; // style = "short" 876 ``` 877 878 879## RelativeTimeFormatInputOptions<sup>8+</sup> 880 881创建相对时间格式化对象时可设置的属性选项。 882 883从API9开始,RelativeTimeFormatInputOptions中的属性改为可选。 884 885**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 886 887**系统能力**:SystemCapability.Global.I18n 888 889| 名称 | 类型 | 必填 |说明 | 890| ------------- | ------ | ---- | ---------------------------------------- | 891| localeMatcher | string | 否 | locale匹配算法,取值包括:"best fit", "lookup"。<br>默认值为best fit。 | 892| numeric | string | 否 | 输出消息的格式,取值包括:"always", "auto"。<br>默认值为always。 | 893| style | string | 否 | 国际化消息的长度,取值包括:"long", "short", "narrow"。<br>默认值为long。 | 894 895> **说明** 896> 897> numeric、style不同参数取值显示的效果,请参考[相对时间格式化选项](../../internationalization/i18n-time-date.md#相对时间格式化)。 898 899## RelativeTimeFormatResolvedOptions<sup>8+</sup> 900 901表示RelativeTimeFormat对象可设置的属性。 902 903**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 904 905**系统能力**:SystemCapability.Global.I18n 906 907| 名称 | 类型 | 必填 |说明 | 908| --------------- | ------ | ---- | ---------------------------------------- | 909| locale | string | 是 | 包含区域设置信息的字符串,包括语言以及可选的脚本和区域。 | 910| numeric | string | 是 | 输出消息的格式,取值包括:"always", "auto"。 | 911| style | string | 是 | 国际化消息的长度,取值包括:"long", "short", "narrow"。 | 912| numberingSystem | string | 是 | 使用的数字系统,取值包括:<br>"adlm", "ahom", "arab", "arabext", "bali", "beng", "bhks", "brah", "cakm", "cham", "deva", "diak", "fullwide", "gong", "gonm", "gujr", "guru", "hanidec", "hmng", "hmnp", "java", "kali", "khmr", "knda", "lana", "lanatham", "laoo", "latn", "lepc", "limb", "mathbold", "mathdbl", "mathmono", "mathsanb", "mathsans", "mlym", "modi", "mong", "mroo", "mtei", "mymr", "mymrshan", "mymrtlng", "newa", "nkoo", "olck", "orya", "osma", "rohg", "saur", "segment", "shrd", "sind", "sinh", "sora", "sund", "takr", "talu", "tamldec", "telu", "thai", "tibt", "tirh", "vaii", "wara", "wcho"。 | 913 914> **说明** 915> 916> numeric、style不同参数取值显示的效果,请参考[相对时间格式化选项](../../internationalization/i18n-time-date.md#相对时间格式化)。