國(guó)際化-Intl

2024-01-23 15:51 更新

本模塊提供基礎(chǔ)的應(yīng)用國(guó)際化能力,包括時(shí)間日期格式化、數(shù)字格式化、排序等,相關(guān)接口在ECMA 402標(biāo)準(zhǔn)中定義。

I18N模塊提供其他非ECMA 402定義的國(guó)際化接口,與本模塊共同使用可提供完整地國(guó)際化支持能力。

說明
  • 本模塊首批接口從API version 6開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。

  • Intl模塊包含國(guó)際化能力基礎(chǔ)接口(在ECMA 402中定義),包括時(shí)間日期格式化、數(shù)字格式化、排序等,國(guó)際化增強(qiáng)能力請(qǐng)參考I18N模塊

導(dǎo)入模塊

  1. import Intl from '@ohos.intl';

未正確導(dǎo)入包可能產(chǎn)生不明確的接口行為。

Locale

屬性

系統(tǒng)能力:SystemCapability.Global.I18n

名稱

類型

可讀

可寫

說明

language

string

與區(qū)域設(shè)置關(guān)聯(lián)的語, 如:zh。

script

string

語言的書寫方式,如:Hans。

region

string

與區(qū)域設(shè)置相關(guān)的地區(qū),如:CN。

baseName

string

Locale的基本核心信息(由語言腳本與地區(qū)組成),如:zh-Hans-CN。

caseFirst

string

區(qū)域的整理規(guī)則是否考慮大小寫,取值包括:"upper", "lower", "false"。

calendar

string

區(qū)域的日歷信息,取值包括:"buddhist", "chinese", "coptic","dangi", "ethioaa", "ethiopic", "gregory", "hebrew", "indian", "islamic", "islamic-umalqura", "islamic-tbla", "islamic-civil", "islamic-rgsa", "iso8601", "japanese", "persian", "roc", "islamicc"。

collation

string

區(qū)域的排序規(guī)則,取值包括:"big5han", "compat", "dict", "direct", "ducet", "eor", "gb2312", "phonebk", "phonetic", "pinyin", "reformed", "searchjl", "stroke", "trad", "unihan", "zhuyin"。

hourCycle

string

區(qū)域的時(shí)制信息,取值包括:"h12", "h23", "h11", "h24"。

numberingSystem

string

區(qū)域使用的數(shù)字系統(tǒng),取值包括:"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"。

numeric

boolean

是否對(duì)數(shù)字字符具有特殊的排序規(guī)則處理。

constructor8+

constructor()

創(chuàng)建區(qū)域?qū)ο?/p>

系統(tǒng)能力:SystemCapability.Global.I18n

示例:

  1. // 默認(rèn)構(gòu)造函數(shù)使用系統(tǒng)當(dāng)前l(fā)ocale創(chuàng)建Locale對(duì)象
  2. let locale = new Intl.Locale();
  3. // 返回系統(tǒng)當(dāng)前l(fā)ocale
  4. let localeID = locale.toString();

constructor

constructor(locale: string, options?: LocaleOptions)

創(chuàng)建區(qū)域?qū)ο?/p>

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

locale

string

包含區(qū)域信息的字符串,包括語言以、腳本、國(guó)家或地區(qū)。語言、腳本、國(guó)家或地區(qū)的國(guó)際標(biāo)準(zhǔn)及組合方式請(qǐng)見Intl開發(fā)指導(dǎo)

options9+

LocaleOptions

用于創(chuàng)建區(qū)域?qū)ο蟮倪x項(xiàng)。

示例:

  1. // 創(chuàng)建 "zh-CN" Locale對(duì)象
  2. let locale = new Intl.Locale("zh-CN");
  3. let localeID = locale.toString(); // localeID = "zh-CN"

toString

toString(): string

獲取區(qū)域?qū)ο蟮淖址硎?/p>

系統(tǒng)能力:SystemCapability.Global.I18n

返回值:

類型

說明

string

區(qū)域?qū)ο蟮淖址硎尽?/p>

示例:

  1. // 創(chuàng)建 "en-GB" Locale對(duì)象
  2. let locale = new Intl.Locale("en-GB");
  3. let localeID = locale.toString(); // localeID = "en-GB"

maximize

maximize(): Locale

最大化區(qū)域信息,若缺少腳本與地區(qū)信息,則補(bǔ)齊。

系統(tǒng)能力:SystemCapability.Global.I18n

返回值:

類型

說明

Locale

最大化后的區(qū)域?qū)ο蟆?/p>

示例:

  1. // 創(chuàng)建 "zh" Locale對(duì)象
  2. let locale = new Intl.Locale("zh");
  3. // 補(bǔ)齊Locale對(duì)象的腳本和地區(qū)
  4. let maximizedLocale = locale.maximize();
  5. let localeID = maximizedLocale.toString(); // localeID = "zh-Hans-CN"
  6. // 創(chuàng)建 "en-US" Locale對(duì)象
  7. locale = new Intl.Locale("en-US");
  8. // 補(bǔ)齊Locale對(duì)象的腳本
  9. maximizedLocale = locale.maximize();
  10. localeID = maximizedLocale.toString(); // localeID = "en-Latn-US"

minimize

minimize(): Locale

最小化區(qū)域信息,若包含腳本與地區(qū)信息,則去除。

系統(tǒng)能力:SystemCapability.Global.I18n

返回值:

類型

說明

Locale

最小化后的區(qū)域?qū)ο蟆?/p>

示例:

  1. // 創(chuàng)建 "zh-Hans-CN" Locale對(duì)象
  2. let locale = new Intl.Locale("zh-Hans-CN");
  3. // 去除Locale對(duì)象的腳本和地區(qū)
  4. let minimizedLocale = locale.minimize();
  5. let localeID = minimizedLocale.toString(); // localeID = "zh"
  6. // 創(chuàng)建 "en-US" Locale對(duì)象
  7. locale = new Intl.Locale("en-US");
  8. // 去除Locale對(duì)象的地區(qū)
  9. minimizedLocale = locale.minimize();
  10. localeID = minimizedLocale.toString(); // localeID = "en"

LocaleOptions9+

表示區(qū)域初始化選項(xiàng)。

系統(tǒng)能力:SystemCapability.Global.I18n

名稱

類型

可讀

可寫

說明

calendar

string

日歷參數(shù),如:"buddhist", "chinese", "coptic", "dangi", "ethioaa", "ethiopic", "gregory", "hebrew", "indian", "islamic", "islamic-umalqura", "islamic-tbla", "islamic-civil", "islamic-rgsa", "iso8601", "japanese", "persian", "roc", "islamicc"。

collation

string

排序參數(shù),取值包括:"big5han", "compat", "dict", "direct", "ducet", "emoji", "eor", "gb2312", "phonebk", "phonetic", "pinyin", "reformed ", "search", "searchjl", "standard", "stroke", "trad", "unihan", "zhuyin"。

hourCycle

string

時(shí)制格式,取值包括:"h11", "h12", "h23", "h24"。

numberingSystem

string

數(shù)字系統(tǒng),取值包括:"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"。

numeric

boolean

是否使用12小時(shí)制。

caseFirst

string

表示大寫、小寫的排序順序,取值范圍:"upper", "lower", "false"。

DateTimeFormat

constructor8+

constructor()

創(chuàng)建時(shí)間日期格式化對(duì)象。

系統(tǒng)能力:SystemCapability.Global.I18n

示例:

  1. // 使用系統(tǒng)當(dāng)前l(fā)ocale創(chuàng)建DateTimeFormat對(duì)象
  2. let datefmt= new Intl.DateTimeFormat();

constructor

constructor(locale: string | Array<string>, options?: DateTimeOptions)

創(chuàng)建時(shí)間日期格式化對(duì)象。

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

locale

string | Array<string>

包含區(qū)域設(shè)置信息的字符串,包括語言以及可選的腳本和區(qū)域。

options9+

DateTimeOptions

用于創(chuàng)建時(shí)間日期格式化的選項(xiàng)。

示例:

  1. // 使用 "zh-CN" locale創(chuàng)建DateTimeFormat對(duì)象,日期風(fēng)格為full,時(shí)間風(fēng)格為medium
  2. let datefmt= new Intl.DateTimeFormat("zh-CN", { dateStyle: 'full', timeStyle: 'medium' });

示例:

  1. // 使用 ["ban", "zh"] locale列表創(chuàng)建DateTimeFormat對(duì)象,因?yàn)閎an為非法LocaleID,因此使用zh Locale創(chuàng)建DateTimeFormat對(duì)象
  2. let datefmt= new Intl.DateTimeFormat(["ban", "zh"], { dateStyle: 'full', timeStyle: 'medium' });

format

format(date: Date): string

格式化時(shí)間日期字符串。

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

date

Date

時(shí)間日期對(duì)象。

返回值:

類型

說明

string

格式化后的時(shí)間日期字符串

示例:

  1. let date = new Date(2021, 11, 17, 3, 24, 0);
  2. // 使用 en-GB locale創(chuàng)建DateTimeFormat對(duì)象
  3. let datefmt = new Intl.DateTimeFormat("en-GB");
  4. let formattedDate = datefmt.format(date); // formattedDate "17/12/2021"
  5. // 使用 en-GB locale創(chuàng)建DateTimeFormat對(duì)象,dateStyle設(shè)置為full,timeStyle設(shè)置為medium
  6. datefmt = new Intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' });
  7. formattedDate = datefmt.format(date); // formattedDate "Friday, 17 December 2021 at 03:24:00"

formatRange

formatRange(startDate: Date, endDate: Date): string

格式化時(shí)間日期段字符串。

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

startDate

Date

起始的時(shí)間日期。

endDate

Date

結(jié)束的時(shí)間日期。

返回值:

類型

說明

string

格式化后的時(shí)間日期段字符串。

示例:

  1. let startDate = new Date(2021, 11, 17, 3, 24, 0);
  2. let endDate = new Date(2021, 11, 18, 3, 24, 0);
  3. // 使用 en-GB locale創(chuàng)建DateTimeFormat對(duì)象
  4. let datefmt = new Intl.DateTimeFormat("en-GB");
  5. let formattedDateRange = datefmt.formatRange(startDate, endDate); // formattedDateRange = "17/12/2021-18/12/2021"

resolvedOptions

resolvedOptions(): DateTimeOptions

獲取DateTimeFormat對(duì)象的格式化選項(xiàng)。

系統(tǒng)能力:SystemCapability.Global.I18n

返回值:

類型

說明

DateTimeOptions

DateTimeFormat 對(duì)象的格式化選項(xiàng)。

示例:

  1. let datefmt = new Intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' });
  2. // 返回DateTimeFormat對(duì)象的配置項(xiàng)
  3. let options = datefmt.resolvedOptions();
  4. let dateStyle = options.dateStyle; // dateStyle = "full"
  5. let timeStyle = options.timeStyle; // timeStyle = "medium"

DateTimeOptions9+

表示時(shí)間日期格式化選項(xiàng)。

系統(tǒng)能力:SystemCapability.Global.I18n

名稱

類型

可讀

可寫

說明

locale

string

區(qū)域參數(shù), 如:zh-Hans-CN。

dateStyle

string

日期顯示格式,取值包括:"long", "short", "medium", "full"。

timeStyle

string

時(shí)間顯示格式,取值包括:"long", "short", "medium", "full"。

hourCycle

string

時(shí)制格式,取值包括:"h11", "h12", "h23", "h24"。

timeZone

string

使用的時(shí)區(qū)(合法的IANA時(shí)區(qū)ID)。

numberingSystem

string

數(shù)字系統(tǒng),取值包括:"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"。

hour12

boolean

是否使用12小時(shí)制。

weekday

string

工作日的顯示格式,取值包括:"long", "short", "narrow"。

era

string

時(shí)代的顯示格式,取值包括:"long", "short", "narrow"。

year

string

年份的顯示格式,取值包括:"numeric", "2-digit"。

month

string

月份的顯示格式,取值包括:"numeric", "2-digit", "long", "short", "narrow"。

day

string

日期的顯示格式,取值包括:"numeric", "2-digit"。

hour

string

小時(shí)的顯示格式,取值包括:"numeric", "2-digit"。

minute

string

分鐘的顯示格式,取值包括:"numeric", "2-digit"。

second

string

秒鐘的顯示格式,取值包括:"numeric", "2-digit"。

timeZoneName

string

時(shí)區(qū)名稱的本地化表示。

dayPeriod

string

時(shí)段的顯示格式,取值包括:"long", "short", "narrow"。

localeMatcher

string

要使用的區(qū)域匹配算法,取值包括:"lookup", "best fit"。

formatMatcher

string

要使用的格式匹配算法,取值包括:"basic", "best fit"。

NumberFormat

constructor8+

constructor()

創(chuàng)建數(shù)字格式化對(duì)象。

系統(tǒng)能力:SystemCapability.Global.I18n

示例:

  1. // 使用系統(tǒng)當(dāng)前l(fā)ocale創(chuàng)建NumberFormat對(duì)象
  2. let numfmt = new Intl.NumberFormat();

constructor

constructor(locale: string | Array<string>, options?: NumberOptions)

創(chuàng)建數(shù)字格式化對(duì)象。

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

locale

string | Array<string>

包含區(qū)域設(shè)置信息的字符串,包括語言以及可選的腳本和區(qū)域。

options9+

NumberOptions

用于創(chuàng)建數(shù)字格式化的選項(xiàng)。

示例:

  1. // 使用 en-GB locale創(chuàng)建NumberFormat對(duì)象,style設(shè)置為decimal,notation設(shè)置為scientific
  2. let numfmt = new Intl.NumberFormat("en-GB", {style:'decimal', notation:"scientific"});

format

format(number: number): string;

格式化數(shù)字字符串。

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

number

number

數(shù)字對(duì)象

返回值:

類型

說明

string

格式化后的數(shù)字字符串

示例:

  1. // 使用 ["en-GB", "zh"] locale列表創(chuàng)建NumberFormat對(duì)象,因?yàn)閑n-GB為合法LocaleID,因此使用en-GB創(chuàng)建NumberFormat對(duì)象
  2. let numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
  3. let formattedNumber = numfmt.format(1223); // formattedNumber = 1.223E3

resolvedOptions

resolvedOptions(): NumberOptions

獲取NumberFormat 對(duì)象的格式化選項(xiàng)。

系統(tǒng)能力:SystemCapability.Global.I18n

返回值:

類型

說明

NumberOptions

NumberFormat 對(duì)象的格式化選項(xiàng)。

示例:

  1. let numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
  2. // 獲取NumberFormat對(duì)象配置項(xiàng)
  3. let options = numfmt.resolvedOptions();
  4. let style = options.style; // style = decimal
  5. let notation = options.notation; // notation = scientific

NumberOptions9+

表示設(shè)備支持的能力。

系統(tǒng)能力:SystemCapability.Global.I18n

名稱

類型

可讀

可寫

說明

locale

string

區(qū)域參數(shù), 如:"zh-Hans-CN"。

currency

string

貨幣單位, 如:"EUR","CNY","USD"等。

currencySign

string

貨幣單位的符號(hào)顯示,取值包括: "symbol","narrowSymbol","code","name" 。

currencyDisplay

string

貨幣的顯示方式,取值包括:"symbol", "narrowSymbol", "code", "name"。

unit

string

單位名稱,如:"meter","inch",“hectare”等。

unitDisplay

string

單位的顯示格式,取值包括:"long", "short", "narrow"。

unitUsage

string

單位的使用場(chǎng)景,取值包括:"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"。

signDisplay

string

數(shù)字符號(hào)的顯示格式,取值包括:"auto", "never", "always", "expectZero"。

compactDisplay

string

緊湊型的顯示格式,取值包括:"long", "short"。

notation

string

數(shù)字的格式化規(guī)格,取值包括:"standard", "scientific", "engineering", "compact"。

localeMatcher

string

要使用的區(qū)域匹配算法,取值包括:"lookup", "best fit"。

style

string

數(shù)字的顯示格式,取值包括:"decimal", "currency", "percent", "unit"。

numberingSystem

string

數(shù)字系統(tǒng),取值包括:"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"。

useGrouping

boolean

是否分組顯示。

minimumIntegerDigits

number

表示要使用的最小整數(shù)位數(shù),取值范圍:1~21。

minimumFractionDigits

number

表示要使用的最小分?jǐn)?shù)位數(shù),取值范圍:0~20。

maximumFractionDigits

number

表示要使用的最大分?jǐn)?shù)位數(shù),取值范圍:1~21。

minimumSignificantDigits

number

表示要使用的最低有效位數(shù),取值范圍:1~21。

maximumSignificantDigits

number

表示要使用的最大有效位數(shù),取值范圍:1~21。

Collator8+

constructor8+

constructor()

創(chuàng)建排序?qū)ο蟆?/p>

系統(tǒng)能力:SystemCapability.Global.I18n

示例:

  1. // 使用系統(tǒng)locale創(chuàng)建Collator對(duì)象
  2. let collator = new Intl.Collator();

constructor8+

constructor(locale: string | Array<string>, options?: CollatorOptions)

創(chuàng)建排序?qū)ο蟆?/p>

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

locale

string | Array<string>

包含區(qū)域設(shè)置信息的字符串,包括語言以及可選的腳本和區(qū)域。

options9+

CollatorOptions

用于創(chuàng)建排序?qū)ο蟮倪x項(xiàng)。

示例:

  1. // 使用 zh-CN locale創(chuàng)建Collator對(duì)象,localeMatcher設(shè)置為lookup,usage設(shè)置為sort
  2. let collator = new Intl.Collator("zh-CN", {localeMatcher: "lookup", usage: "sort"});

compare8+

compare(first: string, second: string): number

依據(jù)Collator的排序策略對(duì)兩個(gè)字符串進(jìn)行比較。

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

first

string

進(jìn)行比較第一個(gè)字符串。

second

string

進(jìn)行比較的第二個(gè)字符串。

返回值:

類型

說明

number

比較結(jié)果。當(dāng)number為負(fù)數(shù),表示first排序在second之前;當(dāng)number為0,表示first與second排序相同;當(dāng)number為正數(shù),表示first排序在second之后。

示例:

  1. // 使用en-GB locale創(chuàng)建Collator對(duì)象
  2. let collator = new Intl.Collator("en-GB");
  3. // 比較 "first" 和 "second" 的先后順序
  4. let compareResult = collator.compare("first", "second"); // compareResult = -1

resolvedOptions8+

resolvedOptions(): CollatorOptions

返回Collator對(duì)象的屬性。

系統(tǒng)能力:SystemCapability.Global.I18n

返回值:

類型

說明

CollatorOptions

返回的Collator對(duì)象的屬性。

示例:

  1. let collator = new Intl.Collator("zh-Hans", { usage: 'sort', ignorePunctuation: true });
  2. // 獲取Collator對(duì)象的配置項(xiàng)
  3. let options = collator.resolvedOptions();
  4. let usage = options.usage; // usage = "sort"
  5. let ignorePunctuation = options.ignorePunctuation; // ignorePunctuation = true

CollatorOptions9+

表示Collator可設(shè)置的屬性。

系統(tǒng)能力:SystemCapability.Global.I18n

名稱

類型

可讀

可寫

說明

localeMatcher

string

locale匹配算法,取值范圍:"best fit", "lookup"。

usage

string

比較的用途,取值范圍:"sort", "search"。

sensitivity

string

表示字符串中的哪些差異會(huì)導(dǎo)致非零結(jié)果值,取值范圍:"base", "accent", "case", "letiant"。

ignorePunctuation

boolean

表示是否忽略標(biāo)點(diǎn)符號(hào),取值范圍:true, false。

collation

string

排序規(guī)則,取值范圍:"big5han", "compat", "dict", "direct", "ducet", "eor", "gb2312", "phonebk", "phonetic", "pinyin", "reformed", "searchjl", "stroke", "trad", "unihan", "zhuyin"。

numeric

boolean

是否使用數(shù)字排序,取值范圍:true, false。

caseFirst

string

表示大寫、小寫的排序順序,取值范圍:"upper", "lower", "false"。

PluralRules8+

constructor8+

constructor()

創(chuàng)建單復(fù)數(shù)對(duì)象來計(jì)算數(shù)字的單復(fù)數(shù)類別。

系統(tǒng)能力:SystemCapability.Global.I18n

示例:

  1. // 使用系統(tǒng)locale創(chuàng)建PluralRules對(duì)象
  2. let pluralRules = new Intl.PluralRules();

constructor8+

constructor(locale: string | Array<string>, options?: PluralRulesOptions)

創(chuàng)建單復(fù)數(shù)對(duì)象來計(jì)算數(shù)字的單復(fù)數(shù)類別。

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

locale

string | Array<string>

包含區(qū)域設(shè)置信息的字符串,包括語言以及可選的腳本和區(qū)域。

options9+

PluralRulesOptions

用于創(chuàng)建單復(fù)數(shù)對(duì)象的選項(xiàng)。

示例:

  1. // 使用 zh-CN locale創(chuàng)建PluralRules對(duì)象,localeMatcher設(shè)置為lookup,type設(shè)置為cardinal
  2. let pluralRules= new Intl.PluralRules("zh-CN", {"localeMatcher": "lookup", "type": "cardinal"});

select8+

select(n: number): string

返回一個(gè)字符串表示該數(shù)字的單復(fù)數(shù)類別。

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

n

number

待獲取單復(fù)數(shù)類別的數(shù)字。

返回值:

類型

說明

string

單復(fù)數(shù)類別,取值包括:"zero","one","two", "few", "many", "others"。

示例:

  1. // 使用 zh-Hans locale創(chuàng)建PluralRules對(duì)象
  2. let zhPluralRules = new Intl.PluralRules("zh-Hans");
  3. // 計(jì)算 zh-Hans locale中數(shù)字1對(duì)應(yīng)的單復(fù)數(shù)類別
  4. let plural = zhPluralRules.select(1); // plural = other
  5. // 使用 en-US locale創(chuàng)建PluralRules對(duì)象
  6. let enPluralRules = new Intl.PluralRules("en-US");
  7. // 計(jì)算 en-US locale中數(shù)字1對(duì)應(yīng)的單復(fù)數(shù)類別
  8. plural = enPluralRules.select(1); // plural = one

PluralRulesOptions9+

表示PluralRules對(duì)象可設(shè)置的屬性。

系統(tǒng)能力:SystemCapability.Global.I18n

名稱

類型

可讀

可寫

說明

localeMatcher

string

locale匹配算法,取值包括:"best fit", "lookup"。

type

string

排序的類型,取值包括:"cardinal", "ordinal"。

minimumIntegerDigits

number

表示要使用的最小整數(shù)位數(shù),取值范圍:1~21。

minimumFractionDigits

number

表示要使用的最小分?jǐn)?shù)位數(shù),取值范圍:0~20。

maximumFractionDigits

number

表示要使用的最大分?jǐn)?shù)位數(shù),取值范圍:1~21。

minimumSignificantDigits

number

表示要使用的最低有效位數(shù),取值范圍:1~21。

maximumSignificantDigits

number

表示要使用的最大有效位數(shù),取值范圍:1~21。

RelativeTimeFormat8+

constructor8+

constructor()

創(chuàng)建相對(duì)時(shí)間格式化對(duì)象。

系統(tǒng)能力:SystemCapability.Global.I18n

示例:

  1. // 使用系統(tǒng)locale創(chuàng)建RelativeTimeFormat對(duì)象
  2. let relativetimefmt = new Intl.RelativeTimeFormat();

constructor8+

constructor(locale: string | Array<string>, options?: RelativeTimeFormatInputOptions)

創(chuàng)建相對(duì)時(shí)間格式化對(duì)象。

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

locale

string | Array<string>

包含區(qū)域設(shè)置信息的字符串,包括語言以及可選的腳本和區(qū)域。

options9+

RelativeTimeFormatInputOptions

用于創(chuàng)建相對(duì)時(shí)間格式化對(duì)象的選項(xiàng)。

示例:

  1. // 使用 zh-CN locale創(chuàng)建RelativeTimeFormat對(duì)象,localeMatcher設(shè)置為lookup,numeric設(shè)置為always,style設(shè)置為long
  2. let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {"localeMatcher": "lookup", "numeric": "always", "style": "long"});

format8+

format(value: number, unit: string): string

依據(jù)locale和格式化選項(xiàng),對(duì)value和unit進(jìn)行格式化。

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

value

number

相對(duì)時(shí)間格式化的數(shù)值。

unit

string

相對(duì)時(shí)間格式化的單位,取值包括:"year", "quarter", "month", "week", "day", "hour", "minute", "second"。

返回值:

類型

說明

string

格式化后的相對(duì)時(shí)間。

示例:

  1. // 使用 zh-CN locale創(chuàng)建RelativeTimeFormat對(duì)象
  2. let relativetimefmt = new Intl.RelativeTimeFormat("zh-CN");
  3. // 計(jì)算 zh-CN locale中數(shù)字3,單位quarter的本地化表示
  4. let formatResult = relativetimefmt.format(3, "quarter"); // formatResult = "3個(gè)季度后"

formatToParts8+

formatToParts(value: number, unit: string): Array<object>

返回一個(gè)對(duì)象數(shù)組,表示可用于自定義區(qū)域設(shè)置格式的相對(duì)時(shí)間格式。

系統(tǒng)能力:SystemCapability.Global.I18n

參數(shù):

參數(shù)名

類型

必填

說明

value

number

相對(duì)時(shí)間格式化的數(shù)值。

unit

string

相對(duì)時(shí)間格式化的單位,取值包括:"year", "quarter", "month", "week", "day", "hour", "minute", "second"。

返回值:

類型

說明

Array<object>

返回可用于自定義區(qū)域設(shè)置格式的相對(duì)時(shí)間格式的對(duì)象數(shù)組。

示例:

  1. // 使用 en locale創(chuàng)建RelativeTimeFormat對(duì)象,numeric設(shè)置為auto
  2. let relativetimefmt = new Intl.RelativeTimeFormat("en", {"numeric": "auto"});
  3. let parts = relativetimefmt.formatToParts(10, "seconds"); // parts = [ {type: "literal", value: "in"}, {type: "integer", value: 10, unit: "second"}, {type: "literal", value: "seconds"} ]

resolvedOptions8+

resolvedOptions(): RelativeTimeFormatResolvedOptions

獲取RelativeTimeFormat對(duì)象的格式化選項(xiàng)。

系統(tǒng)能力:SystemCapability.Global.I18n

返回值:

類型

說明

RelativeTimeFormatResolvedOptions

RelativeTimeFormat 對(duì)象的格式化選項(xiàng)。

示例:

  1. // 使用 en-GB locale創(chuàng)建RelativeTimeFormat對(duì)象
  2. let relativetimefmt= new Intl.RelativeTimeFormat("en-GB", { style: "short" });
  3. // 獲取RelativeTimeFormat對(duì)象配置項(xiàng)
  4. let options = relativetimefmt.resolvedOptions();
  5. let style = options.style; // style = "short"

RelativeTimeFormatInputOptions9+

表示RelativeTimeFormat對(duì)象可設(shè)置的屬性。

系統(tǒng)能力:SystemCapability.Global.I18n

名稱

類型

可讀

可寫

說明

localeMatcher

string

locale匹配算法,取值包括:"best fit", "lookup"。

numeric

string

輸出消息的格式,取值包括:"always", "auto"。

style

string

國(guó)際化消息的長(zhǎng)度,取值包括:"long", "short", "narrow"。

RelativeTimeFormatResolvedOptions8+

表示RelativeTimeFormat對(duì)象可設(shè)置的屬性。

系統(tǒng)能力:SystemCapability.Global.I18n

名稱

類型

可讀

可寫

說明

locale

string

包含區(qū)域設(shè)置信息的字符串,包括語言以及可選的腳本和區(qū)域。

numeric

string

輸出消息的格式,取值包括:"always", "auto"。

style

string

國(guó)際化消息的長(zhǎng)度,取值包括:"long", "short", "narrow"。

numberingSystem

string

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)