W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
本模塊提供基礎(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模塊。
系統(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ī)則處理。 |
constructor()
創(chuàng)建區(qū)域?qū)ο?/p>
系統(tǒng)能力:SystemCapability.Global.I18n
示例:
- // 默認(rèn)構(gòu)造函數(shù)使用系統(tǒng)當(dāng)前l(fā)ocale創(chuàng)建Locale對(duì)象
- let locale = new Intl.Locale();
- // 返回系統(tǒng)當(dāng)前l(fā)ocale
- let localeID = locale.toString();
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+ | 否 | 用于創(chuàng)建區(qū)域?qū)ο蟮倪x項(xiàng)。 |
示例:
- // 創(chuàng)建 "zh-CN" Locale對(duì)象
- let locale = new Intl.Locale("zh-CN");
- let localeID = locale.toString(); // localeID = "zh-CN"
toString(): string
獲取區(qū)域?qū)ο蟮淖址硎?/p>
系統(tǒng)能力:SystemCapability.Global.I18n
返回值:
類型 | 說明 |
---|---|
string | 區(qū)域?qū)ο蟮淖址硎尽?/p> |
示例:
- // 創(chuàng)建 "en-GB" Locale對(duì)象
- let locale = new Intl.Locale("en-GB");
- let localeID = locale.toString(); // localeID = "en-GB"
maximize(): Locale
最大化區(qū)域信息,若缺少腳本與地區(qū)信息,則補(bǔ)齊。
系統(tǒng)能力:SystemCapability.Global.I18n
返回值:
類型 | 說明 |
---|---|
最大化后的區(qū)域?qū)ο蟆?/p> |
示例:
- // 創(chuàng)建 "zh" Locale對(duì)象
- let locale = new Intl.Locale("zh");
- // 補(bǔ)齊Locale對(duì)象的腳本和地區(qū)
- let maximizedLocale = locale.maximize();
- let localeID = maximizedLocale.toString(); // localeID = "zh-Hans-CN"
- // 創(chuàng)建 "en-US" Locale對(duì)象
- locale = new Intl.Locale("en-US");
- // 補(bǔ)齊Locale對(duì)象的腳本
- maximizedLocale = locale.maximize();
- localeID = maximizedLocale.toString(); // localeID = "en-Latn-US"
minimize(): Locale
最小化區(qū)域信息,若包含腳本與地區(qū)信息,則去除。
系統(tǒng)能力:SystemCapability.Global.I18n
返回值:
類型 | 說明 |
---|---|
最小化后的區(qū)域?qū)ο蟆?/p> |
示例:
- // 創(chuàng)建 "zh-Hans-CN" Locale對(duì)象
- let locale = new Intl.Locale("zh-Hans-CN");
- // 去除Locale對(duì)象的腳本和地區(qū)
- let minimizedLocale = locale.minimize();
- let localeID = minimizedLocale.toString(); // localeID = "zh"
- // 創(chuàng)建 "en-US" Locale對(duì)象
- locale = new Intl.Locale("en-US");
- // 去除Locale對(duì)象的地區(qū)
- minimizedLocale = locale.minimize();
- localeID = minimizedLocale.toString(); // localeID = "en"
表示區(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"。 |
constructor()
創(chuàng)建時(shí)間日期格式化對(duì)象。
系統(tǒng)能力:SystemCapability.Global.I18n
示例:
- // 使用系統(tǒng)當(dāng)前l(fā)ocale創(chuàng)建DateTimeFormat對(duì)象
- let datefmt= new Intl.DateTimeFormat();
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+ | 否 | 用于創(chuàng)建時(shí)間日期格式化的選項(xiàng)。 |
示例:
- // 使用 "zh-CN" locale創(chuàng)建DateTimeFormat對(duì)象,日期風(fēng)格為full,時(shí)間風(fēng)格為medium
- let datefmt= new Intl.DateTimeFormat("zh-CN", { dateStyle: 'full', timeStyle: 'medium' });
示例:
- // 使用 ["ban", "zh"] locale列表創(chuàng)建DateTimeFormat對(duì)象,因?yàn)閎an為非法LocaleID,因此使用zh Locale創(chuàng)建DateTimeFormat對(duì)象
- let datefmt= new Intl.DateTimeFormat(["ban", "zh"], { dateStyle: 'full', timeStyle: 'medium' });
format(date: Date): string
格式化時(shí)間日期字符串。
系統(tǒng)能力:SystemCapability.Global.I18n
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
date | Date | 是 | 時(shí)間日期對(duì)象。 |
返回值:
類型 | 說明 |
---|---|
string | 格式化后的時(shí)間日期字符串 |
示例:
- let date = new Date(2021, 11, 17, 3, 24, 0);
- // 使用 en-GB locale創(chuàng)建DateTimeFormat對(duì)象
- let datefmt = new Intl.DateTimeFormat("en-GB");
- let formattedDate = datefmt.format(date); // formattedDate "17/12/2021"
- // 使用 en-GB locale創(chuàng)建DateTimeFormat對(duì)象,dateStyle設(shè)置為full,timeStyle設(shè)置為medium
- datefmt = new Intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' });
- formattedDate = datefmt.format(date); // formattedDate "Friday, 17 December 2021 at 03:24:00"
formatRange(startDate: Date, endDate: Date): string
格式化時(shí)間日期段字符串。
系統(tǒng)能力:SystemCapability.Global.I18n
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
startDate | Date | 是 | 起始的時(shí)間日期。 |
endDate | Date | 是 | 結(jié)束的時(shí)間日期。 |
返回值:
類型 | 說明 |
---|---|
string | 格式化后的時(shí)間日期段字符串。 |
示例:
- let startDate = new Date(2021, 11, 17, 3, 24, 0);
- let endDate = new Date(2021, 11, 18, 3, 24, 0);
- // 使用 en-GB locale創(chuàng)建DateTimeFormat對(duì)象
- let datefmt = new Intl.DateTimeFormat("en-GB");
- let formattedDateRange = datefmt.formatRange(startDate, endDate); // formattedDateRange = "17/12/2021-18/12/2021"
resolvedOptions(): DateTimeOptions
獲取DateTimeFormat對(duì)象的格式化選項(xiàng)。
系統(tǒng)能力:SystemCapability.Global.I18n
返回值:
類型 | 說明 |
---|---|
DateTimeFormat 對(duì)象的格式化選項(xiàng)。 |
示例:
- let datefmt = new Intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' });
- // 返回DateTimeFormat對(duì)象的配置項(xiàng)
- let options = datefmt.resolvedOptions();
- let dateStyle = options.dateStyle; // dateStyle = "full"
- let timeStyle = options.timeStyle; // timeStyle = "medium"
表示時(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"。 |
constructor()
創(chuàng)建數(shù)字格式化對(duì)象。
系統(tǒng)能力:SystemCapability.Global.I18n
示例:
- // 使用系統(tǒng)當(dāng)前l(fā)ocale創(chuàng)建NumberFormat對(duì)象
- let numfmt = new Intl.NumberFormat();
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+ | 否 | 用于創(chuàng)建數(shù)字格式化的選項(xiàng)。 |
示例:
- // 使用 en-GB locale創(chuàng)建NumberFormat對(duì)象,style設(shè)置為decimal,notation設(shè)置為scientific
- let numfmt = new Intl.NumberFormat("en-GB", {style:'decimal', notation:"scientific"});
format(number: number): string;
格式化數(shù)字字符串。
系統(tǒng)能力:SystemCapability.Global.I18n
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
number | number | 是 | 數(shù)字對(duì)象 |
返回值:
類型 | 說明 |
---|---|
string | 格式化后的數(shù)字字符串 |
示例:
- // 使用 ["en-GB", "zh"] locale列表創(chuàng)建NumberFormat對(duì)象,因?yàn)閑n-GB為合法LocaleID,因此使用en-GB創(chuàng)建NumberFormat對(duì)象
- let numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
- let formattedNumber = numfmt.format(1223); // formattedNumber = 1.223E3
resolvedOptions(): NumberOptions
獲取NumberFormat 對(duì)象的格式化選項(xiàng)。
系統(tǒng)能力:SystemCapability.Global.I18n
返回值:
類型 | 說明 |
---|---|
NumberFormat 對(duì)象的格式化選項(xiàng)。 |
示例:
- let numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
- // 獲取NumberFormat對(duì)象配置項(xiàng)
- let options = numfmt.resolvedOptions();
- let style = options.style; // style = decimal
- let notation = options.notation; // notation = scientific
表示設(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。 |
constructor()
創(chuàng)建排序?qū)ο蟆?/p>
系統(tǒng)能力:SystemCapability.Global.I18n
示例:
- // 使用系統(tǒng)locale創(chuàng)建Collator對(duì)象
- let collator = new Intl.Collator();
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+ | 否 | 用于創(chuàng)建排序?qū)ο蟮倪x項(xiàng)。 |
示例:
- // 使用 zh-CN locale創(chuàng)建Collator對(duì)象,localeMatcher設(shè)置為lookup,usage設(shè)置為sort
- let collator = new Intl.Collator("zh-CN", {localeMatcher: "lookup", usage: "sort"});
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之后。 |
示例:
- // 使用en-GB locale創(chuàng)建Collator對(duì)象
- let collator = new Intl.Collator("en-GB");
- // 比較 "first" 和 "second" 的先后順序
- let compareResult = collator.compare("first", "second"); // compareResult = -1
resolvedOptions(): CollatorOptions
返回Collator對(duì)象的屬性。
系統(tǒng)能力:SystemCapability.Global.I18n
返回值:
類型 | 說明 |
---|---|
返回的Collator對(duì)象的屬性。 |
示例:
- let collator = new Intl.Collator("zh-Hans", { usage: 'sort', ignorePunctuation: true });
- // 獲取Collator對(duì)象的配置項(xiàng)
- let options = collator.resolvedOptions();
- let usage = options.usage; // usage = "sort"
- let ignorePunctuation = options.ignorePunctuation; // ignorePunctuation = true
表示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"。 |
constructor()
創(chuàng)建單復(fù)數(shù)對(duì)象來計(jì)算數(shù)字的單復(fù)數(shù)類別。
系統(tǒng)能力:SystemCapability.Global.I18n
示例:
- // 使用系統(tǒng)locale創(chuàng)建PluralRules對(duì)象
- let pluralRules = new Intl.PluralRules();
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+ | 否 | 用于創(chuàng)建單復(fù)數(shù)對(duì)象的選項(xiàng)。 |
示例:
- // 使用 zh-CN locale創(chuàng)建PluralRules對(duì)象,localeMatcher設(shè)置為lookup,type設(shè)置為cardinal
- let pluralRules= new Intl.PluralRules("zh-CN", {"localeMatcher": "lookup", "type": "cardinal"});
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"。 |
示例:
- // 使用 zh-Hans locale創(chuàng)建PluralRules對(duì)象
- let zhPluralRules = new Intl.PluralRules("zh-Hans");
- // 計(jì)算 zh-Hans locale中數(shù)字1對(duì)應(yīng)的單復(fù)數(shù)類別
- let plural = zhPluralRules.select(1); // plural = other
- // 使用 en-US locale創(chuàng)建PluralRules對(duì)象
- let enPluralRules = new Intl.PluralRules("en-US");
- // 計(jì)算 en-US locale中數(shù)字1對(duì)應(yīng)的單復(fù)數(shù)類別
- plural = enPluralRules.select(1); // plural = one
表示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。 |
constructor()
創(chuàng)建相對(duì)時(shí)間格式化對(duì)象。
系統(tǒng)能力:SystemCapability.Global.I18n
示例:
- // 使用系統(tǒng)locale創(chuàng)建RelativeTimeFormat對(duì)象
- let relativetimefmt = new Intl.RelativeTimeFormat();
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+ | 否 | 用于創(chuàng)建相對(duì)時(shí)間格式化對(duì)象的選項(xiàng)。 |
示例:
- // 使用 zh-CN locale創(chuàng)建RelativeTimeFormat對(duì)象,localeMatcher設(shè)置為lookup,numeric設(shè)置為always,style設(shè)置為long
- let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {"localeMatcher": "lookup", "numeric": "always", "style": "long"});
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í)間。 |
示例:
- // 使用 zh-CN locale創(chuàng)建RelativeTimeFormat對(duì)象
- let relativetimefmt = new Intl.RelativeTimeFormat("zh-CN");
- // 計(jì)算 zh-CN locale中數(shù)字3,單位quarter的本地化表示
- let formatResult = relativetimefmt.format(3, "quarter"); // formatResult = "3個(gè)季度后"
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ù)組。 |
示例:
- // 使用 en locale創(chuàng)建RelativeTimeFormat對(duì)象,numeric設(shè)置為auto
- let relativetimefmt = new Intl.RelativeTimeFormat("en", {"numeric": "auto"});
- let parts = relativetimefmt.formatToParts(10, "seconds"); // parts = [ {type: "literal", value: "in"}, {type: "integer", value: 10, unit: "second"}, {type: "literal", value: "seconds"} ]
resolvedOptions(): RelativeTimeFormatResolvedOptions
獲取RelativeTimeFormat對(duì)象的格式化選項(xiàng)。
系統(tǒng)能力:SystemCapability.Global.I18n
返回值:
類型 | 說明 |
---|---|
RelativeTimeFormat 對(duì)象的格式化選項(xiàng)。 |
示例:
- // 使用 en-GB locale創(chuàng)建RelativeTimeFormat對(duì)象
- let relativetimefmt= new Intl.RelativeTimeFormat("en-GB", { style: "short" });
- // 獲取RelativeTimeFormat對(duì)象配置項(xiàng)
- let options = relativetimefmt.resolvedOptions();
- let style = options.style; // style = "short"
表示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"。 |
表示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 | 是 | 是 |
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: