diff --git a/zh-cn/application-dev/internationalization/i18n-guidelines.md b/zh-cn/application-dev/internationalization/i18n-guidelines.md index c4f4b9c1d28ae74b866f19f80d05a3cc4039f120..04381e286c69a7889a78b3177f611767bd2af36c 100644 --- a/zh-cn/application-dev/internationalization/i18n-guidelines.md +++ b/zh-cn/application-dev/internationalization/i18n-guidelines.md @@ -37,7 +37,7 @@ ### 开发步骤 1. 导入I18n模块。 - ```js + ```ts import I18n from '@ohos.i18n'; ``` @@ -46,12 +46,15 @@ 调用setSystemLanguage接口设置系统语言(该接口为系统接口,只有具有UPDATE_CONFIGURATION权限的系统应用可以调用)。 调用getSystemLanguage接口获取系统语言。 - ```js + ```ts + import { BusinessError } from '@ohos.base'; + try { I18n.System.setSystemLanguage("en"); // 将系统语言设置为 "en" let language = I18n.System.getSystemLanguage(); // language = "en" } catch(error) { - console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); + let err: BusinessError = error as BusinessError; + console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); } ``` @@ -60,12 +63,15 @@ 调用setSystemRegion接口设置系统国家(该接口为系统接口,只有具有UPDATE_CONFIGURATION权限的系统应用可以调用)。 调用getSystemRegion接口获取系统国家。 - ```js + ```ts + import { BusinessError } from '@ohos.base'; + try { I18n.System.setSystemRegion("CN"); // 将系统国家设置为 "CN" let region = I18n.System.getSystemRegion(); // region = "CN" } catch(error) { - console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); + let err: BusinessError = error as BusinessError; + console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); } ``` @@ -74,12 +80,15 @@ 调用setSystemLocale接口设置系统Locale(该接口为系统接口,只有具有UPDATE_CONFIGURATION权限的系统应用可以调用)。Locale的定义请见[Locale](../internationalization/intl-guidelines.md#设置区域信息) 调用getSystemLocale接口获取系统Locale。 - ```js + ```ts + import { BusinessError } from '@ohos.base'; + try { I18n.System.setSystemLocale("zh-Hans-CN"); // 将系统Locale设置为 "zh-Hans-CN" let locale = I18n.System.getSystemLocale(); // locale = "zh-Hans-CN" } catch(error) { - console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); + let err: BusinessError = error as BusinessError; + console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); } ``` @@ -87,12 +96,15 @@ 调用isRTL接口获取Locale的语言是否为从右到左语言。 - ```js + ```ts + import { BusinessError } from '@ohos.base'; + try { let rtl = I18n.isRTL("zh-CN"); // rtl = false rtl = I18n.isRTL("ar"); // rtl = true } catch(error) { - console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); + let err: BusinessError = error as BusinessError; + console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); } ``` @@ -101,12 +113,15 @@ 调用set24HourClock接口打开系统24小时制设置。 调用is24HourClock接口来判断当前是否打开系统24小时制设置。 - ```js + ```ts + import { BusinessError } from '@ohos.base'; + try { I18n.System.set24HourClock(true); let hourClock = I18n.System.is24HourClock(); // hourClock = true } catch(error) { - console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); + let err: BusinessError = error as BusinessError; + console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); } ``` @@ -114,14 +129,17 @@ 调用getDisplayLanguage接口获取某一语言的本地化表示。其中,language表示待本地化显示的语言,locale表示本地化的Locale,sentenceCase结果是否需要首字母大写。 - ```js + ```ts + import { BusinessError } from '@ohos.base'; + try { let language = "en"; let locale = "zh-CN"; let sentenceCase = false; let localizedLanguage = I18n.System.getDisplayLanguage(language, locale, sentenceCase); // localizedLanguage = "英语" } catch(error) { - console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); + let err: BusinessError = error as BusinessError; + console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); } ``` @@ -129,14 +147,17 @@ 调用getDisplayCountry接口获取某一国家的本地化表示。其中,country表示待本地化显示的国家,locale表示本地化的Locale,sentenceCase结果是否需要首字母大写。 - ```js + ```ts + import { BusinessError } from '@ohos.base'; + try { let country = "US"; let locale = "zh-CN"; let sentenceCase = false; let localizedCountry = I18n.System.getDisplayCountry(country, locale, sentenceCase); // localizedCountry = "美国" } catch(error) { - console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); + let err: BusinessError = error as BusinessError; + console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); } ``` @@ -144,13 +165,15 @@ 调用getSystemLanguages接口获取系统支持的语言列表。 调用getSystemCountries接口获取某一语言系统支持的地区列表。 - ```js + ```ts + import { BusinessError } from '@ohos.base'; try { let languageList = I18n.System.getSystemLanguages(); // languageList = ["en-Latn-US", "zh-Hans"] let countryList = I18n.System.getSystemCountries("zh"); // countryList = ["ZW", "YT", ..., "CN", "DE"], 共240个国家和地区 } catch(error) { - console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); + let err: BusinessError = error as BusinessError; + console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); } ``` @@ -158,11 +181,14 @@ 调用isSuggested接口判断语言和地区是否匹配。 - ```js + ```ts + import { BusinessError } from '@ohos.base'; + try { let isSuggest = I18n.System.isSuggested("zh", "CN"); // isSuggest = true } catch(error) { - console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); + let err: BusinessError = error as BusinessError; + console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); } ``` @@ -174,7 +200,9 @@ 调用getFirstPreferredLanguage接口获取系统偏好语言列表中的第一个偏好语言。 调用getAppPreferredLanguageList接口获取应用偏好语言,应用偏好语言为系统偏好语言列表中第一个与应用的资源匹配的语言。 - ```js + ```ts + import { BusinessError } from '@ohos.base'; + try { I18n.System.addPreferredLanguage("en-GB", 0); // 将"en-GB"设置为系统偏好语言列表的第一个语言 let list = I18n.System.getPreferredLanguageList(); // 获取当前系统偏好语言列表 list = ["en-GB", ...] @@ -182,7 +210,8 @@ let firstPreferredLanguage = I18n.System.getFirstPreferredLanguage(); // firstPreferredLanguage = "en-GB" let appPreferredLanguage = I18n.System.getAppPreferredLanguage(); // 当应用中包含 "en-GB"资源时,应用偏好语言为"en-GB" } catch(error) { - console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); + let err: BusinessError = error as BusinessError; + console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); } ``` @@ -192,12 +221,15 @@ 调用getUsingLocalDigit接口访问本地化数字开关状态。 当前只有 "ar", "as", "bn", "fa", "mr", "my", "ne", "ur" 8个语言支持使用本地数字。 -```js +```ts +import { BusinessError } from '@ohos.base'; + try { I18n.System.setUsingLocalDigit(true); // 打开本地化数字开关 let status = I18n.System.getUsingLocalDigit(); // status = true } catch(error) { - console.error(`call i18n.System interface failed, error code: ${error.code}, message: ${error.message}`); + let err: BusinessError = error as BusinessError; + console.error(`call i18n.System interface failed, error code: ${err.code}, message: ${err.message}`); } ``` @@ -226,7 +258,7 @@ try { 1. 导入I18n模块。 - ```js + ```ts import I18n from '@ohos.i18n'; ``` @@ -234,7 +266,7 @@ try { 调用getCalendar接口获取指定locale和type的时区对象(i18n为导入的模块)。其中,type表示合法的日历类型,目前合法的日历类型包括:"buddhist", "chinese", "coptic", "ethiopic", "hebrew", "gregory", "indian", "islamic_civil", "islamic_tbla", "islamic_umalqura", "japanese", "persian"。当type没有给出时,采用区域默认的日历类型。 - ```js + ```ts let calendar = I18n.getCalendar("zh-CN", "chinese"); // 创建中文农历日历 ``` @@ -242,7 +274,7 @@ try { 调用setTime接口设置日历对象的时间。setTime接口接收两种类型的参数。一种是传入一个Date对象,另一种是传入一个数值表示从1970.1.1 00:00:00 GMT逝去的毫秒数。 - ```js + ```ts let date1 = new Date(); calendar.setTime(date1); let date2 = 1000; @@ -253,7 +285,7 @@ try { 调用set接口设置日历对象的年、月、日、时、分、秒。 - ```js + ```ts calendar.set(2021, 12, 21, 6, 0, 0); ``` @@ -261,7 +293,7 @@ try { 调用setTimeZone接口和getTimeZone接口来设置、获取日历对象的时区。其中,setTimeZone接口需要传入一个字符串表示需要设置的时区。 - ```js + ```ts calendar.setTimeZone("Asia/Shanghai"); let timezone = calendar.getTimeZone(); // timezone = "China Standard Time" ``` @@ -270,7 +302,7 @@ try { 调用setFirstDayOfWeek接口和getFirstDayOfWeek接口设置、获取日历对象的一周起始日。其中,setFirstDayOfWeek需要传入一个数值表示一周的起始日,1代表周日,7代表周六。 - ```js + ```ts calendar.setFirstDayOfWeek(1); let firstDayOfWeek = calendar.getFirstDayOfWeek(); // firstDayOfWeek = 1 ``` @@ -278,7 +310,7 @@ try { 7. 设置、获取日历对象第一周的最小天数。 调用setMinimalDaysInFirstWeek接口和getMinimalDaysInFirstWeek接口来设置、获取日历对象第一周的最小天数。 - ```js + ```ts calendar.setMinimalDaysInFirstWeek(3); let minimalDaysInFirstWeek = calendar.getMinimalDaysInFirstWeek(); // minimalDaysInFirstWeek = 3 ``` @@ -286,7 +318,7 @@ try { 8. 获取日历对象的本地化显示。 调用getDisplayName来获取日历对象的本地化显示。 - ```js + ```ts let localizedName = calendar.getDisplayName("zh-CN"); // localizedName = "农历" ``` @@ -294,7 +326,7 @@ try { 调用isWeekend接口来判断输入的Date是否为周末。 - ```js + ```ts let date = new Date(2022, 12, 12, 12, 12, 12); let weekend = calendar.isWeekend(date); // weekend = false ``` @@ -316,7 +348,7 @@ try { 1. 导入I18n模块。 - ```js + ```ts import I18n from '@ohos.i18n'; ``` @@ -324,7 +356,7 @@ try { 调用PhoneNumberFormat的构造函数来实例化电话号码格式化对象,需要传入电话号码的国家代码及格式化选项。其中,格式化选项是可选的,包括style选项,该选项的取值包括:"E164", "INTERNATIONAL", "NATIONAL", "RFC3966"。 - ```js + ```ts let phoneNumberFormat = new I18n.PhoneNumberFormat("CN", {type: "E164"}); ``` @@ -332,7 +364,7 @@ try { 调用isValidNumber接口来判断输入的电话号码的格式是否正确。 - ```js + ```ts let validNumber = phoneNumberFormat.isValidNumber("15812341234"); // validNumber = true ``` @@ -340,7 +372,7 @@ try { 调用电话号码格式化对象的format接口来对输入的电话号码进行格式化。 - ```js + ```ts let formattedNumber = phoneNumberFormat.format("15812341234"); // formattedNumber = "+8615812341234" ``` @@ -358,7 +390,7 @@ try { 1. 导入I18n模块。 - ```js + ```ts import I18n from '@ohos.i18n'; ``` @@ -366,9 +398,9 @@ try { 调用[unitConvert](../reference/apis/js-apis-i18n.md#unitconvert9)接口实现度量衡单位转换,并进行格式化显示的功能。 - ```js - let fromUnit = {unit: "cup", measureSystem: "US"}; - let toUnit = {unit: "liter", measureSystem: "SI"}; + ```ts + let fromUnit: I18n.UnitInfo = {unit: "cup", measureSystem: "US"}; + let toUnit: I18n.UnitInfo = {unit: "liter", measureSystem: "SI"}; let number = 1000; let locale = "en-US"; let style = "long"; @@ -392,7 +424,7 @@ try { 1. 导入I18n模块。 - ```js + ```ts import I18n from '@ohos.i18n'; ``` @@ -401,7 +433,7 @@ try { 调用getInstance接口来实例化特定locale对应的字母表索引对象。当locale参数为空时,实例化系统默认Locale的字母表索引对象。 - ```js + ```ts let indexUtil = I18n.getInstance("zh-CN"); ``` @@ -409,7 +441,7 @@ try { 调用getIndexList接口来获取当前Locale对应的字母表索引列表。 - ```js + ```ts let indexList = indexUtil.getIndexList(); // indexList = ["...", "A", "B", "C", ..., "X", "Y", "Z", "..."] ``` @@ -417,7 +449,7 @@ try { 调用addLocale接口,将新的Locale对应的字母表索引添加到当前字母表索引列表中。 - ```js + ```ts indexUtil.addLocale("ar"); ``` @@ -425,7 +457,7 @@ try { 调用getIndex接口来获取某一字符串对应的字母表索引。 - ```js + ```ts let text = "access index"; let index = indexUtil.getIndex(text); // index = "A" ``` @@ -453,7 +485,7 @@ try { 1. 导入I18n模块。 - ```js + ```ts import I18n from '@ohos.i18n'; ``` @@ -461,7 +493,7 @@ try { 调用getLineInstance接口来实例化断行对象。 - ```js + ```ts let locale = "en-US"; let breakIterator = I18n.getLineInstance(locale); ``` @@ -470,7 +502,7 @@ try { 调用setLineBreakText接口和getLineBreakText接口来设置、访问要断行处理的文本。 - ```js + ```ts let text = "Apple is my favorite fruit"; breakIterator.setLineBreakText(text); let breakText = breakIterator.getLineBreakText(); // breakText = "Apple is my favorite fruit" @@ -480,7 +512,7 @@ try { 调用current接口来获取断行对象在当前处理文本中的位置。 - ```js + ```ts let pos = breakIterator.current(); // pos = 0 ``` @@ -488,7 +520,7 @@ try { 系统提供了很多接口可以用于调整断行对象在处理文本中的位置,包括first, last, next, previous, following。 - ```js + ```ts let firstPos = breakIterator.first(); // 将断行对象设置到第一个分割点的位置,即文本的起始位置;firstPos = 0 let lastPos = breakIterator.last(); // 将断行对象设置到最后一个分割点的位置,即文本末尾的下一个位置;lastPos = 26 // 将断行对象向前或向后移动一定数量的分割点。 @@ -504,7 +536,7 @@ try { 调用isBoundary接口来判断一个接口是否为分割点;如果该位置是分割点,则返回true,并且将断行对象移动到该位置;如果该位置不是分割点,则返回false,并且将断行对象移动到该位置后的一个分割点。 - ```js + ```ts let isboundary = breakIterator.isBoundary(5); // isboundary = false ``` @@ -530,7 +562,7 @@ try { 1. 导入I18n模块。 - ```js + ```ts import I18n from '@ohos.i18n'; ``` @@ -538,13 +570,13 @@ try { 调用getTimeZone接口来获取时区对象。 - ```js + ```ts let timezone = I18n.getTimeZone(); // 使用默认参数可以获取系统时区对象。 ``` 获取时区ID、本地化显示、时区偏移量、某一时刻的时区偏移量信息。 - ```js + ```ts let timezoneID = timezone.getID(); // timezoneID = "Asia/Shanghai" let timezoneDisplayName = timezone.getDisplayName(); // timezoneDisplayName = "中国标准时间" let rawOffset = timezone.getRawOffset(); // rawOffset = 28800000 @@ -556,7 +588,7 @@ try { 调用getAvailableIDs接口获取系统支持的时区ID列表。 时区ID列表中的时区ID可以作为getTimeZone接口的参数,来创建TimeZone对象。 - ```js + ```ts let timezoneIDs = I18n.TimeZone.getAvailableIDs(); // timezoneIDs = ["America/Adak", ...],共包含24个时区ID let timezone = I18n.getTimeZone(timezoneIDs[0]); let timezoneDisplayName = timezone.getDisplayName(); // timezoneDisplayName = "夏威夷-阿留申时间" @@ -568,7 +600,7 @@ try { 调用getCityDisplayName接口获取时区城市ID的本地化显示。 调用getTimezoneFromCity接口基于时区城市ID创建时区对象。 - ```js + ```ts let zoneCityIDs = I18n.TimeZone.getAvailableZoneCityIDs(); // ["Auckland", "Magadan", ...] let cityDisplayName = I18n.TimeZone.getCityDisplayName(zoneCityIDs[0], "zh-Hans"); // cityDisplayName = "奥克兰(新西兰)" let timezone = I18n.TimeZone.getTimezoneFromCity(zoneCityIDs[0]); @@ -591,7 +623,7 @@ try { 1. 导入I18n模块。 - ```js + ```ts import I18n from '@ohos.i18n'; ``` @@ -600,7 +632,7 @@ try { 调用getAvailableIDs接口来获取支持音译的ID列表。 每个ID的格式为 source-destination,例如 ASCII-Latin,表示将ASCII转换为Latin的音译ID。 - ```js + ```ts let ids = I18n.Transliterator.getAvailableIDs(); // ids = ["ASCII-Latin", "Accents-Any", ... ],共支持671个语言 ``` @@ -609,7 +641,7 @@ try { 支持音译的ID列表中的ID可以作为getInstance接口的参数,创建音译对象。 调用transform接口,获取音译字符串。 - ```js + ```ts let transliterator = I18n.Transliterator.getInstance("Any-Latin"); // Any-Latin表示将任意文本转换为Latin文本 let transformText = transliterator.transform("你好"); // transformText = "nǐ hǎo " let transliterator2 = I18n.Transliterator.getInstance("Latin-ASCII"); // Latin-ASCII表示将Latin文本转换为ASCII文本 @@ -638,7 +670,7 @@ try { 1. 导入I18n模块。 - ```js + ```ts import I18n from '@ohos.i18n'; ``` @@ -646,56 +678,56 @@ try { 判断字符是否是数字。 - ```js + ```ts let isDigit = I18n.Unicode.isDigit("1"); // isDigit = true isDigit = I18n.Unicode.isDigit("a"); // isDigit = false ``` 判断字符是否是空格符。 - ```js + ```ts let isSpaceChar = I18n.Unicode.isSpaceChar(" "); // isSpaceChar = true isSpaceChar = I18n.Unicode.isSpaceChar("\n"); // isSpaceChar = false ``` 判断字符是否是空白符。 - ```js + ```ts let isWhitespace = I18n.Unicode.isWhitespace(" "); // isWhitespace = true isWhitespace = I18n.Unicode.isWhitespace("\n"); // isWhitespace = true ``` 判断字符是否是从左到右书写的文字。 - ```js + ```ts let isRTL = I18n.Unicode.isRTL("مرحبًا"); // isRTL = true,阿拉伯语的文字是从左到右书写的文字 isRTL = I18n.Unicode.isRTL("a"); // isRTL = false ``` 判断字符是否是表意文字。 - ```js + ```ts let isIdeograph = I18n.Unicode.isIdeograph("你好"); // isIdeograph = true isIdeograph = I18n.Unicode.isIdeograph("a"); // isIdeograph = false ``` 判断字符是否是字母。 - ```js + ```ts let isLetter = I18n.Unicode.isLetter("a"); // isLetter = true isLetter = I18n.Unicode.isLetter("."); // isLetter = false ``` 判断字符是否是小写字母。 - ```js + ```ts let isLowerCase = I18n.Unicode.isLowerCase("a"); // isLetter = true isLowerCase = I18n.Unicode.isLowerCase("A"); // isLetter = false ``` 判断字符是否是大写字母。 - ```js + ```ts let isUpperCase = I18n.Unicode.isUpperCase("a"); // isUpperCase = false isUpperCase = I18n.Unicode.isUpperCase("A"); // isUpperCase = true ``` @@ -704,7 +736,7 @@ try { 调用getType接口获取字符的类型。 - ```js + ```ts let type = I18n.Unicode.getType("a"); // type = U_LOWER_CASE_LETTER ``` @@ -720,7 +752,7 @@ try { 1. 导入I18n模块。 - ```js + ```ts import I18n from '@ohos.i18n'; ``` @@ -729,7 +761,7 @@ try { 调用getDateOrder接口判断某一Locale的日期中,年月日的排列顺序。 接口返回一个字符串,由"y","L","d"三部分组成,分别表示年、月、日,使用中划线进行拼接。例如,"y-L-d"。 - ```js + ```ts let order = I18n.I18NUtil.getDateOrder("zh-CN"); // order = "y-L-d",表示中文中年月日的顺序为年-月-日。 ``` diff --git a/zh-cn/application-dev/internationalization/intl-guidelines.md b/zh-cn/application-dev/internationalization/intl-guidelines.md index 7da78695e67339c394f3d39c1e48f8f6d2667652..75d12379fcf3e51659c3e83a89c7a9b15f85dbb6 100644 --- a/zh-cn/application-dev/internationalization/intl-guidelines.md +++ b/zh-cn/application-dev/internationalization/intl-guidelines.md @@ -24,7 +24,7 @@ 未正确导入包可能会产生不明确的接口行为。 - ```js + ```ts import Intl from '@ohos.intl'; ``` @@ -47,9 +47,9 @@ | kf | 表示字符串排序、比较时是否考虑大小写 | - ```js + ```ts let locale = "zh-CN"; - let options = {caseFirst: "false", calendar: "chinese", collation: "pinyin"}; + let options: Intl.LocaleOptions = {caseFirst: "false", calendar: "chinese", collation: "pinyin"}; let localeObj = new Intl.Locale(locale, options); ``` @@ -57,7 +57,7 @@ 调用toString方法来获取Locale对象的字符串表示,其中包括了语言、区域及其他选项信息。 - ```js + ```ts let localeStr = localeObj.toString(); // localeStr = "zh-CN-u-ca-chinese-co-pinyin-kf-false ``` @@ -65,7 +65,7 @@ 调用maximize方法来最大化区域信息,即当缺少脚本与地区信息时,对其进行补全。 - ```js + ```ts let maximizedLocale = localeObj.maximize(); let maximizedLocaleStr = maximizedLocale.toString(); // localeStr = "zh-Hans-CN-u-ca-chinese-co-pinyin-kf-false ``` @@ -74,7 +74,7 @@ 调用minimize方法来最小化区域信息,即当存在脚本与地区信息时,对其进行删除。 - ```js + ```ts let minimizedLocale = localeObj.minimize(); let minimizedLocaleStr = minimizedLocale.toString(); // zh-u-ca-chinese-co-pinyin-kf-false ``` @@ -99,7 +99,7 @@ 未正确导入包可能会产生不明确的接口行为。 - ```js + ```ts import Intl from '@ohos.intl'; ``` @@ -107,14 +107,14 @@ 一种方法是使用DateTimeFormat提供的默认构造函数,通过访问系统语言和地区设置,获取系统默认Locale,并将其作为DateTimeFormat对象内部的Locale。 - ```js + ```ts let dateTimeFormat = new Intl.DateTimeFormat(); ``` 另一种方法是使用开发者提供的Locale和格式化参数来创建日期时间格式化对象。其中,格式化参数是可选的,完整的格式化参数列表见[DateTimeOptions](../reference/apis/js-apis-intl.md#datetimeoptions6)。 - ```js - let options = {dateStyle: "full", timeStyle: "full"}; + ```ts + let options: Intl.DateTimeOptions = {dateStyle: "full", timeStyle: "full"}; let dateTimeFormat = new Intl.DateTimeFormat("zh-CN", options); ``` @@ -122,8 +122,8 @@ 使用DateTimeFormat的format方法对一个Date对象进行格式化,该方法会返回一个字符串作为格式化的结果。 - ```js - let options = {dateStyle: "full", timeStyle: "full"}; + ```ts + let options: Intl.DateTimeOptions = {dateStyle: "full", timeStyle: "full"}; let dateTimeFormat = new Intl.DateTimeFormat("zh-CN", options); let date = new Date(2022, 12, 12, 12, 12, 12); let formatResult = dateTimeFormat.format(date); // formatResult = "2023年1月12日星期四 中国标准时间 下午12:12:12" @@ -133,7 +133,7 @@ 使用DateTimeFormat的formatRange方法对一个时间段进行格式化。该方法需要传入两个Date对象,分别表示时间段的起止日期,返回一个字符串作为格式化的结果。 - ```js + ```ts let startDate = new Date(2021, 11, 17, 3, 24, 0); let endDate = new Date(2021, 11, 18, 3, 24, 0); let datefmt = new Intl.DateTimeFormat("en-GB"); @@ -144,8 +144,8 @@ DateTimeFormat的resolvedOptions方法会返回一个对象,该对象包含了DateTimeFormat对象的所有相关属性及其值。 - ```js - let options = {dateStyle: "full", timeStyle: "full"}; + ```ts + let options: Intl.DateTimeOptions = {dateStyle: "full", timeStyle: "full"}; let dateTimeFormat = new Intl.DateTimeFormat("zh-CN", options); let resolvedOptions = dateTimeFormat.resolvedOptions(); // resolvedOptions = {"locale": "zh-CN", "calendar": "gregorian", "dateStyle":"full", "timeStyle":"full", "timeZone": "CST"} ``` @@ -169,7 +169,7 @@ 未正确导入包可能会产生不明确的接口行为。 - ```js + ```ts import Intl from '@ohos.intl'; ``` @@ -177,14 +177,14 @@ 一种方法是使用NumberFormat提供的默认构造函数,通过访问系统的语言和地区以获取系统默认Locale并进行设置(intl为导入的模块名)。 - ```js + ```ts let numberFormat = new Intl.NumberFormat(); ``` 另一种方法是使用开发者提供的Locale和格式化参数来创建数字格式化对象。其中,格式化参数是可选的,完整的格式化参数列表参见[NumberOptions](../reference/apis/js-apis-intl.md##numberoptions6)。 - ```js - let options = {compactDisplay: "short", notation: "compact"}; + ```ts + let options: Intl.NumberOptions = {compactDisplay: "short", notation: "compact"}; let numberFormat = new Intl.NumberFormat("zh-CN", options); ``` @@ -192,8 +192,8 @@ 使用NumberFormat的format方法对传入的数字进行格式化。该方法返回一个字符串作为格式化的结果。 - ```js - let options = {compactDisplay: "short", notation: "compact"}; + ```ts + let options: Intl.NumberOptions = {compactDisplay: "short", notation: "compact"}; let numberFormat = new Intl.NumberFormat("zh-CN", options); let number = 1234.5678; let formatResult = numberFormat.format(number); // formatResult = "1235" @@ -203,8 +203,8 @@ NumberFormat的resolvedOptions方法会返回一个对象,该对象包含了NumberFormat对象的所有相关属性及其值。 - ```js - let options = {compactDisplay: "short", notation: "compact"}; + ```ts + let options: Intl.NumberOptions = {compactDisplay: "short", notation: "compact"}; let numberFormat = new Intl.NumberFormat("zh-CN", options); let resolvedOptions = numberFormat.resolvedOptions(); // resolvedOptions = {"locale": "zh-CN", "compactDisplay": "short", "notation": "compact", "numberingSystem": "Latn"} ``` @@ -228,7 +228,7 @@ 未正确导入包可能会产生不明确的接口行为。 - ```js + ```ts import Intl from '@ohos.intl'; ``` @@ -236,14 +236,14 @@ 一种方法是使用Collator提供的默认构造函数,通过访问系统的语言和地区以获取系统默认Locale并进行设置(intl为导入的模块名)。 - ```js + ```ts let collator = new Intl.Collator(); ``` 另一种方法是使用开发者提供的Locale和其他相关参数来创建Collator对象,完整的参数列表参见[CollatorOptions](../reference/apis/js-apis-intl.md#collatoroptions8)。 其中,sensitivity参数用于控制哪些级别的差异会被用于比较两个字符串。取值"base"表示,仅比较字符本身,不考虑重音符号、大小写差异。例如,'a' != 'b','a' == 'á','a' == 'A'。取值"accent"表示考虑重音符号,不考虑大小写的差异。例如,'a' != 'b','a' != 'á','a' == 'A'。取值"case"表示考虑大小写的差异,不考虑重音符号的差异。例如,'a' != 'b','a' == 'á','a' != 'A'。取值"variant"表示考虑重音符号、大小写等方面差异。例如'a' != 'b','a' != 'á','a' != 'A'。 - ```js + ```ts let collator= new Intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort", sensitivity: "case"}); ``` @@ -251,7 +251,7 @@ 使用Collator的compare方法对传入的两个字符串进行比较。该方法返回一个数值作为比较的结果,返回-1表示第一个字符串小于第二个字符串,返回1表示第一个字符大于第二个字符串,返回0表示两个字符串相同。基于两个字符串的比较结果,开发者可以字符串集合进行排序。 - ```js + ```ts let collator= new Intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort", sensitivity: "case"}); let str1 = "first string"; let str2 = "second string"; @@ -265,7 +265,7 @@ Collator的resolvedOptions方法会返回一个对象,该对象包含了Collator对象的所有相关属性及其值。 - ```js + ```ts let collator= new Intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort"}); let options = collator.resolvedOptions(); // options = {"localeMatcher": "best fit", "locale": "zh-CN", "usage": "sort", "sensitivity": "variant", "ignorePunctuation": "false", "numeric": false, "caseFirst": "false", "collation": "default"} ``` @@ -289,7 +289,7 @@ 未正确导入包可能会产生不明确的接口行为。 - ```js + ```ts import Intl from '@ohos.intl'; ``` @@ -297,13 +297,13 @@ 一种方法是使用PluralRules提供的默认构造函数,通过访问系统的语言和地区以获取系统默认Locale并进行设置(intl为导入的模块名)。 - ```js + ```ts let pluralRules = new Intl.PluralRules(); ``` 另一种方法是使用开发者提供的Locale和其他相关参数来创建单复数对象。完整的参数列表参见[PluralRulesOptions](../reference/apis/js-apis-intl.md#pluralrulesoptions8)。 - ```js + ```ts let pluralRules = new Intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"}); ``` @@ -311,7 +311,7 @@ 使用PluralRules的select方法计算传入数字的单复数类别。该方法返回一个字符串作为传入数字的类别,包括:"zero", "one", "two", "few", "many", "other"六个类别。 - ```js + ```ts let pluralRules = new Intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"}); let number = 1234.5678; let categoryResult = pluralRules.select(number); // categoryResult = "other" @@ -337,7 +337,7 @@ 未正确导入包可能会产生不明确的接口行为。 - ```js + ```ts import Intl from '@ohos.intl'; ``` @@ -345,13 +345,13 @@ 一种方法是使用RelativeTimeFormat提供的默认构造函数,通过访问系统的语言和地区以获取系统默认Locale并进行设置(intl为导入的模块名)。 - ```js + ```ts let relativeTimeFormat = new Intl.RelativeTimeFormat(); ``` 另一种方法是使用开发者提供的Locale和格式化参数来创建相对时间格式化对象。其中,格式化参数是可选的,完整的参数列表参见[RelativeTimeFormatInputOptions](../reference/apis/js-apis-intl.md#relativetimeformatinputoptions8)。 - ```js + ```ts let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"}); ``` @@ -359,7 +359,7 @@ 使用RelativeTimeFormat的format方法对相对时间进行格式化。方法接收一个表示相对时间长度的数值和表示单位的字符串,其中单位包括:"year", "quarter", "month", "week", "day", "hour", "minute", "second"。方法返回一个字符串作为格式化的结果。 - ```js + ```ts let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"}); let number = 2; let unit = "year"; @@ -370,7 +370,7 @@ 获取相对时间格式化结果的各个部分,从而自定义格式化结果。 - ```js + ```ts let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"}); let number = 2; let unit = "year"; @@ -381,7 +381,7 @@ RelativeTimeFormat的resolvedOptions方法会返回一个对象,该对象包含了RelativeTimeFormat对象的所有相关属性及其值,完整的属性列表参见[ RelativeTimeFormatResolvedOptions](../reference/apis/js-apis-intl.md#relativetimeformatresolvedoptions8)。 - ```js + ```ts let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"}); let options = relativeTimeFormat.resolvedOptions(); // options = {"locale": "zh-CN", "style": "long", "numeric": "always", "numberingSystem": "latn"} ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-i18n.md b/zh-cn/application-dev/reference/apis/js-apis-i18n.md index 1cfca139061a79fbf4b7c42c9f5a4a903ca80465..2dcc93486ec3ef81f59c2c9903ddbdb80f6457b4 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-i18n.md +++ b/zh-cn/application-dev/reference/apis/js-apis-i18n.md @@ -98,7 +98,7 @@ static getDisplayLanguage(language: string, locale: string, sentenceCase?: boole let displayLanguage: string = I18n.System.getDisplayLanguage("zh", "en-GB"); // displayLanguage = Chinese } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.getDisplayLanguage failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -132,7 +132,7 @@ static getSystemLanguages(): Array<string> let systemLanguages: Array = I18n.System.getSystemLanguages(); // [ "en-Latn-US", "zh-Hans" ] } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.getSystemLanguages failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -172,7 +172,7 @@ static getSystemCountries(language: string): Array<string> let systemCountries: Array = I18n.System.getSystemCountries('zh'); // systemCountries = [ "ZW", "YT", "YE", ..., "ER", "CN", "DE" ],共计240个国家或地区 } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.getSystemCountries failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -213,7 +213,7 @@ static isSuggested(language: string, region?: string): boolean let res: boolean = I18n.System.isSuggested('zh', 'CN'); // res = true } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.isSuggested failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -247,7 +247,7 @@ static getSystemLanguage(): string let systemLanguage: string = I18n.System.getSystemLanguage(); // systemLanguage为当前系统语言 } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.getSystemLanguage failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -285,7 +285,7 @@ static setSystemLanguage(language: string): void I18n.System.setSystemLanguage('zh'); // 设置系统当前语言为 "zh" } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.setSystemLanguage failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -319,7 +319,7 @@ static getSystemRegion(): string let systemRegion: string = I18n.System.getSystemRegion(); // 获取系统当前地区设置 } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.getSystemRegion failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -357,7 +357,7 @@ static setSystemRegion(region: string): void I18n.System.setSystemRegion('CN'); // 设置系统当前地区为 "CN" } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.setSystemRegion failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -391,7 +391,7 @@ static getSystemLocale(): string let systemLocale: string = I18n.System.getSystemLocale(); // 获取系统当前Locale } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.getSystemLocale failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -429,7 +429,7 @@ static setSystemLocale(locale: string): void I18n.System.setSystemLocale('zh-CN'); // 设置系统当前Locale为 "zh-CN" } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.setSystemLocale failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -463,7 +463,7 @@ static is24HourClock(): boolean let is24HourClock: boolean = I18n.System.is24HourClock(); // 系统24小时开关是否开启 } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.is24HourClock failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -502,7 +502,7 @@ static set24HourClock(option: boolean): void I18n.System.set24HourClock(true); } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.set24HourClock failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -544,7 +544,7 @@ static addPreferredLanguage(language: string, index?: number): void I18n.System.addPreferredLanguage(language, index); // 将zh-CN添加到系统偏好语言列表的第1位 } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.addPreferredLanguage failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -584,7 +584,7 @@ static removePreferredLanguage(index: number): void I18n.System.removePreferredLanguage(index); } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.removePreferredLanguage failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -618,7 +618,7 @@ static getPreferredLanguageList(): Array<string> let preferredLanguageList: Array = I18n.System.getPreferredLanguageList(); // 获取系统当前偏好语言列表 } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.getPreferredLanguageList failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -652,7 +652,7 @@ static getFirstPreferredLanguage(): string let firstPreferredLanguage: string = I18n.System.getFirstPreferredLanguage(); // 获取系统当前偏好语言列表中的第一个偏好语言 } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.getFirstPreferredLanguage failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -686,7 +686,7 @@ static getAppPreferredLanguage(): string let appPreferredLanguage: string = I18n.System.getAppPreferredLanguage(); // 获取应用偏好语言 } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.getAppPreferredLanguage failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -724,7 +724,7 @@ static setUsingLocalDigit(flag: boolean): void I18n.System.setUsingLocalDigit(true); // 打开本地化数字开关 } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.setUsingLocalDigit failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -758,7 +758,7 @@ static getUsingLocalDigit(): boolean let status: boolean = I18n.System.getUsingLocalDigit(); // 判断本地化数字开关是否打开 } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call System.getUsingLocalDigit failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -2371,7 +2371,7 @@ getLanguageInfoArray(languages: Array<string>, options?: SortOptions): Arr let sortedLanguages: Array = systemLocaleManager.getLanguageInfoArray(languages, sortOptions); } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call systemLocaleManager.getLanguageInfoArray failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -2420,7 +2420,7 @@ getRegionInfoArray(regions: Array<string>, options?: SortOptions): Array&l let sortedRegions: Array = systemLocaleManager.getRegionInfoArray(regions, sortOptions); } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call systemLocaleManager.getRegionInfoArray failed, error code: ${err.code}, message: ${err.message}.`); } ``` @@ -2452,7 +2452,7 @@ static getTimeZoneCityItemArray(): Array<TimeZoneCityItem> } } catch(error) { let err: BusinessError = error as BusinessError; - console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); + console.error(`call SystemLocaleManager.getTimeZoneCityItemArray failed, error code: ${err.code}, message: ${err.message}.`); } ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-intl.md b/zh-cn/application-dev/reference/apis/js-apis-intl.md index 0f20907149e7dc7102ebaa8aa7dd7b3e579bc4cb..b6822c93300d0d7c51f9339001a6a314b5be34e0 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-intl.md +++ b/zh-cn/application-dev/reference/apis/js-apis-intl.md @@ -11,7 +11,7 @@ ## 导入模块 -```js +```ts import Intl from '@ohos.intl'; ``` @@ -45,7 +45,7 @@ constructor() **系统能力**:SystemCapability.Global.I18n **示例:** - ```js + ```ts // 默认构造函数使用系统当前locale创建Locale对象 let locale = new Intl.Locale(); // 返回系统当前localel @@ -69,7 +69,7 @@ constructor(locale: string, options?: LocaleOptions) | options | [LocaleOptions](#localeoptions6) | 否 | 用于创建区域对象的选项。 | **示例:** - ```js + ```ts // 创建 "zh-CN" Locale对象 let locale = new Intl.Locale("zh-CN"); let localeID = locale.toString(); // localeID = "zh-CN" @@ -91,7 +91,7 @@ toString(): string | string | 区域对象的字符串表示。 | **示例:** - ```js + ```ts // 创建 "en-GB" Locale对象 let locale = new Intl.Locale("en-GB"); let localeID = locale.toString(); // localeID = "en-GB" @@ -113,7 +113,7 @@ maximize(): Locale | [Locale](#locale) | 最大化后的区域对象。 | **示例:** - ```js + ```ts // 创建 "zh" Locale对象 let locale = new Intl.Locale("zh"); // 补齐Locale对象的脚本和地区 @@ -143,7 +143,7 @@ minimize(): Locale | [Locale](#locale) | 最小化后的区域对象。 | **示例:** - ```js + ```ts // 创建 "zh-Hans-CN" Locale对象 let locale = new Intl.Locale("zh-Hans-CN"); // 去除Locale对象的脚本和地区 @@ -187,7 +187,7 @@ constructor() **系统能力**:SystemCapability.Global.I18n **示例:** - ```js + ```ts // 使用系统当前locale创建DateTimeFormat对象 let datefmt= new Intl.DateTimeFormat(); ``` @@ -209,14 +209,14 @@ constructor(locale: string | Array<string>, options?: DateTimeOptions) | options | [DateTimeOptions](#datetimeoptions6) | 否 | 用于创建时间日期格式化的选项。若所有选项均未设置时,year、month、day三个属性的默认值为numeric。 | **示例:** - ```js + ```ts // 使用 "zh-CN" locale创建DateTimeFormat对象,日期风格为full,时间风格为medium let datefmt= new Intl.DateTimeFormat("zh-CN", { dateStyle: 'full', timeStyle: 'medium' }); ``` **示例:** - ```js + ```ts // 使用 ["ban", "zh"] locale列表创建DateTimeFormat对象,因为ban为非法LocaleID,因此使用zh Locale创建DateTimeFormat对象 let datefmt= new Intl.DateTimeFormat(["ban", "zh"], { dateStyle: 'full', timeStyle: 'medium' }); ``` @@ -243,7 +243,7 @@ format(date: Date): string | string | 格式化后的时间日期字符串 | **示例:** - ```js + ```ts let date = new Date(2021, 11, 17, 3, 24, 0); // 使用 en-GB locale创建DateTimeFormat对象 let datefmt = new Intl.DateTimeFormat("en-GB"); @@ -277,7 +277,7 @@ formatRange(startDate: Date, endDate: Date): string | string | 格式化后的时间日期段字符串。 | **示例:** - ```js + ```ts let startDate = new Date(2021, 11, 17, 3, 24, 0); let endDate = new Date(2021, 11, 18, 3, 24, 0); // 使用 en-GB locale创建DateTimeFormat对象 @@ -301,7 +301,7 @@ resolvedOptions(): DateTimeOptions | [DateTimeOptions](#datetimeoptions6) | DateTimeFormat 对象的格式化选项。 | **示例:** - ```js + ```ts let datefmt = new Intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' }); // 返回DateTimeFormat对象的配置项 let options = datefmt.resolvedOptions(); @@ -352,7 +352,7 @@ constructor() **系统能力**:SystemCapability.Global.I18n **示例:** - ```js + ```ts // 使用系统当前locale创建NumberFormat对象 let numfmt = new Intl.NumberFormat(); ``` @@ -374,7 +374,7 @@ constructor(locale: string | Array<string>, options?: NumberOptions) | options | [NumberOptions](#numberoptions6) | 否 | 用于创建数字格式化的选项。 | **示例:** - ```js + ```ts // 使用 en-GB locale创建NumberFormat对象,style设置为decimal,notation设置为scientific let numfmt = new Intl.NumberFormat("en-GB", {style:'decimal', notation:"scientific"}); ``` @@ -402,7 +402,7 @@ format(number: number): string; **示例:** - ```js + ```ts // 使用 ["en-GB", "zh"] locale列表创建NumberFormat对象,因为en-GB为合法LocaleID,因此使用en-GB创建NumberFormat对象 let numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"}); let formattedNumber = numfmt.format(1223); // formattedNumber = 1.223E3 @@ -425,7 +425,7 @@ resolvedOptions(): NumberOptions **示例:** - ```js + ```ts let numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"}); // 获取NumberFormat对象配置项 let options = numfmt.resolvedOptions(); @@ -476,7 +476,7 @@ constructor() **系统能力**:SystemCapability.Global.I18n **示例:** - ```js + ```ts // 使用系统locale创建Collator对象 let collator = new Intl.Collator(); ``` @@ -498,7 +498,7 @@ constructor(locale: string | Array<string>, options?: CollatorOptions) | options | [CollatorOptions](#collatoroptions8) | 否 | 用于创建排序对象的选项。 | **示例:** - ```js + ```ts // 使用 zh-CN locale创建Collator对象,localeMatcher设置为lookup,usage设置为sort let collator = new Intl.Collator("zh-CN", {localeMatcher: "lookup", usage: "sort"}); ``` @@ -526,7 +526,7 @@ compare(first: string, second: string): number | number | 比较结果。当number为负数,表示first排序在second之前;当number为0,表示first与second排序相同;当number为正数,表示first排序在second之后。 | **示例:** - ```js + ```ts // 使用en-GB locale创建Collator对象 let collator = new Intl.Collator("en-GB"); // 比较 "first" 和 "second" 的先后顺序 @@ -549,7 +549,7 @@ resolvedOptions(): CollatorOptions | [CollatorOptions](#collatoroptions8) | 返回的Collator对象的属性。 | **示例:** - ```js + ```ts let collator = new Intl.Collator("zh-Hans", { usage: 'sort', ignorePunctuation: true }); // 获取Collator对象的配置项 let options = collator.resolvedOptions(); @@ -588,7 +588,7 @@ constructor() **系统能力**:SystemCapability.Global.I18n **示例:** - ```js + ```ts // 使用系统locale创建PluralRules对象 let pluralRules = new Intl.PluralRules(); ``` @@ -610,7 +610,7 @@ constructor(locale: string | Array<string>, options?: PluralRulesOptions) | options | [PluralRulesOptions](#pluralrulesoptions8) | 否 | 用于创建单复数对象的选项。 | **示例:** - ```js + ```ts // 使用 zh-CN locale创建PluralRules对象,localeMatcher设置为lookup,type设置为cardinal let pluralRules= new Intl.PluralRules("zh-CN", {"localeMatcher": "lookup", "type": "cardinal"}); ``` @@ -637,7 +637,7 @@ select(n: number): string | string | 单复数类别,取值包括:"zero","one","two", "few", "many", "others"。 | **示例:** - ```js + ```ts // 使用 zh-Hans locale创建PluralRules对象 let zhPluralRules = new Intl.PluralRules("zh-Hans"); // 计算 zh-Hans locale中数字1对应的单复数类别 @@ -680,7 +680,7 @@ constructor() **系统能力**:SystemCapability.Global.I18n **示例:** - ```js + ```ts // 使用系统locale创建RelativeTimeFormat对象 let relativetimefmt = new Intl.RelativeTimeFormat(); ``` @@ -702,7 +702,7 @@ constructor(locale: string | Array<string>, options?: RelativeTimeFormatIn | options | [RelativeTimeFormatInputOptions](#relativetimeformatinputoptions8) | 否 | 用于创建相对时间格式化对象的选项。 | **示例:** - ```js + ```ts // 使用 zh-CN locale创建RelativeTimeFormat对象,localeMatcher设置为lookup,numeric设置为always,style设置为long let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {"localeMatcher": "lookup", "numeric": "always", "style": "long"}); ``` @@ -730,7 +730,7 @@ format(value: number, unit: string): string | string | 格式化后的相对时间。 | **示例:** - ```js + ```ts // 使用 zh-CN locale创建RelativeTimeFormat对象 let relativetimefmt = new Intl.RelativeTimeFormat("zh-CN"); // 计算 zh-CN locale中数字3,单位quarter的本地化表示 @@ -760,7 +760,7 @@ formatToParts(value: number, unit: string): Array<object> | Array<object> | 返回可用于自定义区域设置格式的相对时间格式的对象数组。 | **示例:** - ```js + ```ts // 使用 en locale创建RelativeTimeFormat对象,numeric设置为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"} ] @@ -782,7 +782,7 @@ resolvedOptions(): RelativeTimeFormatResolvedOptions | [RelativeTimeFormatResolvedOptions](#relativetimeformatresolvedoptions8) | RelativeTimeFormat 对象的格式化选项。 | **示例:** - ```js + ```ts // 使用 en-GB locale创建RelativeTimeFormat对象 let relativetimefmt= new Intl.RelativeTimeFormat("en-GB", { style: "short" }); // 获取RelativeTimeFormat对象配置项