提交 4a2f9018 编写于 作者: W wangjian

全球化ArkTs语法整改

Signed-off-by: Nwangjian <wangjian497@huawei.com>
上级 b2defeb2
......@@ -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"}
```
......
......@@ -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&lt;string&gt;
let systemLanguages: Array<string> = 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&lt;string&gt;
let systemCountries: Array<string> = 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&lt;string&gt;
let preferredLanguageList: Array<string> = 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&lt;string&gt;, options?: SortOptions): Arr
let sortedLanguages: Array<I18n.LocaleItem> = 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&lt;string&gt;, options?: SortOptions): Array&l
let sortedRegions: Array<I18n.LocaleItem> = 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&lt;TimeZoneCityItem&gt;
}
} 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}.`);
}
```
......
......@@ -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&lt;string&gt;, 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&nbsp;对象的格式化选项。 |
**示例:**
```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&lt;string&gt;, 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&lt;string&gt;, 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&lt;string&gt;, 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",&nbsp;"few",&nbsp;"many",&nbsp;"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&lt;string&gt;, 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&lt;object&gt;
| Array&lt;object&gt; | 返回可用于自定义区域设置格式的相对时间格式的对象数组。 |
**示例:**
```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&nbsp;对象的格式化选项。 |
**示例:**
```js
```ts
// 使用 en-GB locale创建RelativeTimeFormat对象
let relativetimefmt= new Intl.RelativeTimeFormat("en-GB", { style: "short" });
// 获取RelativeTimeFormat对象配置项
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册