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