Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
33e0dc90
D
Docs
项目概览
OpenHarmony
/
Docs
接近 2 年 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
33e0dc90
编写于
12月 29, 2022
作者:
S
sunyaozu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix intl
Signed-off-by:
N
sunyaozu
<
sunyaozu@huawei.com
>
上级
ec52511f
变更
4
展开全部
隐藏空白更改
内联
并排
Showing
4 changed file
with
312 addition
and
311 deletion
+312
-311
zh-cn/application-dev/internationalization/i18n-guidelines.md
...n/application-dev/internationalization/i18n-guidelines.md
+78
-82
zh-cn/application-dev/internationalization/intl-guidelines.md
...n/application-dev/internationalization/intl-guidelines.md
+57
-57
zh-cn/application-dev/reference/apis/js-apis-i18n.md
zh-cn/application-dev/reference/apis/js-apis-i18n.md
+110
-110
zh-cn/application-dev/reference/apis/js-apis-intl.md
zh-cn/application-dev/reference/apis/js-apis-intl.md
+67
-62
未找到文件。
zh-cn/application-dev/internationalization/i18n-guidelines.md
浏览文件 @
33e0dc90
此差异已折叠。
点击以展开。
zh-cn/application-dev/internationalization/intl-guidelines.md
浏览文件 @
33e0dc90
...
@@ -48,9 +48,9 @@
...
@@ -48,9 +48,9 @@
```
js
```
js
var
locale
=
"
zh-CN
"
;
let
locale
=
"
zh-CN
"
;
var
options
=
{
caseFirst
:
"
false
"
,
calendar
:
"
chinese
"
,
collation
:
"
pinyin
"
};
let
options
=
{
caseFirst
:
"
false
"
,
calendar
:
"
chinese
"
,
collation
:
"
pinyin
"
};
var
localeObj
=
new
Intl
.
Locale
(
locale
,
options
);
let
localeObj
=
new
Intl
.
Locale
(
locale
,
options
);
```
```
3.
获取Locale的字符串表示。
3.
获取Locale的字符串表示。
...
@@ -58,7 +58,7 @@
...
@@ -58,7 +58,7 @@
调用toString方法来获取Locale对象的字符串表示,其中包括了语言、区域及其他选项信息。
调用toString方法来获取Locale对象的字符串表示,其中包括了语言、区域及其他选项信息。
```
js
```
js
var
localeStr
=
localeObj
.
toString
();
// localeStr = "zh-CN-u-ca-chinese-co-pinyin-kf-false
let
localeStr
=
localeObj
.
toString
();
// localeStr = "zh-CN-u-ca-chinese-co-pinyin-kf-false
```
```
4.
最大化区域信息。
4.
最大化区域信息。
...
@@ -66,8 +66,8 @@
...
@@ -66,8 +66,8 @@
调用maximize方法来最大化区域信息,即当缺少脚本与地区信息时,对其进行补全。
调用maximize方法来最大化区域信息,即当缺少脚本与地区信息时,对其进行补全。
```
js
```
js
var
maximizedLocale
=
localeObj
.
maximize
();
let
maximizedLocale
=
localeObj
.
maximize
();
var
maximizedLocaleStr
=
maximizedLocale
.
toString
();
// localeStr = "zh-Hans-CN-u-ca-chinese-co-pinyin-kf-false
let
maximizedLocaleStr
=
maximizedLocale
.
toString
();
// localeStr = "zh-Hans-CN-u-ca-chinese-co-pinyin-kf-false
```
```
5.
最小化区域信息。
5.
最小化区域信息。
...
@@ -75,8 +75,8 @@
...
@@ -75,8 +75,8 @@
调用minimize方法来最小化区域信息,即当存在脚本与地区信息时,对其进行删除。
调用minimize方法来最小化区域信息,即当存在脚本与地区信息时,对其进行删除。
```
js
```
js
var
minimizedLocale
=
localeObj
.
minimize
();
let
minimizedLocale
=
localeObj
.
minimize
();
var
minimizedLocaleStr
=
minimizedLocale
.
toString
();
// zh-u-ca-chinese-co-pinyin-kf-false
let
minimizedLocaleStr
=
minimizedLocale
.
toString
();
// zh-u-ca-chinese-co-pinyin-kf-false
```
```
## 格式化日期时间
## 格式化日期时间
...
@@ -108,14 +108,14 @@
...
@@ -108,14 +108,14 @@
一种方法是使用DateTimeFormat提供的默认构造函数,通过访问系统语言和地区设置,获取系统默认Locale,并将其作为DateTimeFormat对象内部的Locale。
一种方法是使用DateTimeFormat提供的默认构造函数,通过访问系统语言和地区设置,获取系统默认Locale,并将其作为DateTimeFormat对象内部的Locale。
```
js
```
js
var
dateTimeFormat
=
new
Intl
.
DateTimeFormat
();
let
dateTimeFormat
=
new
Intl
.
DateTimeFormat
();
```
```
另一种方法是使用开发者提供的Locale和格式化参数来创建日期时间格式化对象。其中,格式化参数是可选的,完整的格式化参数列表见[DateTimeOptions](../reference/apis/js-apis-intl.md#datetimeoptions)。
另一种方法是使用开发者提供的Locale和格式化参数来创建日期时间格式化对象。其中,格式化参数是可选的,完整的格式化参数列表见[DateTimeOptions](../reference/apis/js-apis-intl.md#datetimeoptions)。
```
js
```
js
var
options
=
{
dateStyle
:
"
full
"
,
timeStyle
:
"
full
"
};
let
options
=
{
dateStyle
:
"
full
"
,
timeStyle
:
"
full
"
};
var
dateTimeFormat
=
new
Intl
.
DateTimeFormat
(
"
zh-CN
"
,
options
);
let
dateTimeFormat
=
new
Intl
.
DateTimeFormat
(
"
zh-CN
"
,
options
);
```
```
3.
格式化日期时间。
3.
格式化日期时间。
...
@@ -123,10 +123,10 @@
...
@@ -123,10 +123,10 @@
使用DateTimeFormat的format方法对一个Date对象进行格式化,该方法会返回一个字符串作为格式化的结果。
使用DateTimeFormat的format方法对一个Date对象进行格式化,该方法会返回一个字符串作为格式化的结果。
```
js
```
js
var
options
=
{
dateStyle
:
"
full
"
,
timeStyle
:
"
full
"
};
let
options
=
{
dateStyle
:
"
full
"
,
timeStyle
:
"
full
"
};
var
dateTimeFormat
=
new
Intl
.
DateTimeFormat
(
"
zh-CN
"
,
options
);
let
dateTimeFormat
=
new
Intl
.
DateTimeFormat
(
"
zh-CN
"
,
options
);
var
date
=
new
Date
(
2022
,
12
,
12
,
12
,
12
,
12
);
let
date
=
new
Date
(
2022
,
12
,
12
,
12
,
12
,
12
);
var
formatResult
=
dateTimeFormat
.
format
(
date
);
// formatResult = "2023年1月12日星期四 中国标准时间 下午12:12:12"
let
formatResult
=
dateTimeFormat
.
format
(
date
);
// formatResult = "2023年1月12日星期四 中国标准时间 下午12:12:12"
```
```
4.
格式化时间段。
4.
格式化时间段。
...
@@ -134,10 +134,10 @@
...
@@ -134,10 +134,10 @@
使用DateTimeFormat的formatRange方法对一个时间段进行格式化。该方法需要传入两个Date对象,分别表示时间段的起止日期,返回一个字符串作为格式化的结果。
使用DateTimeFormat的formatRange方法对一个时间段进行格式化。该方法需要传入两个Date对象,分别表示时间段的起止日期,返回一个字符串作为格式化的结果。
```
js
```
js
var
startDate
=
new
Date
(
2021
,
11
,
17
,
3
,
24
,
0
);
let
startDate
=
new
Date
(
2021
,
11
,
17
,
3
,
24
,
0
);
var
endDate
=
new
Date
(
2021
,
11
,
18
,
3
,
24
,
0
);
let
endDate
=
new
Date
(
2021
,
11
,
18
,
3
,
24
,
0
);
var
datefmt
=
new
Intl
.
DateTimeFormat
(
"
en-GB
"
);
let
datefmt
=
new
Intl
.
DateTimeFormat
(
"
en-GB
"
);
var
formatRangeResult
=
datefmt
.
formatRange
(
startDate
,
endDate
);
// formatRangeResult = "17/12/2021-18/12/2021"
let
formatRangeResult
=
datefmt
.
formatRange
(
startDate
,
endDate
);
// formatRangeResult = "17/12/2021-18/12/2021"
```
```
5.
访问日期时间格式化对象的相关属性。
5.
访问日期时间格式化对象的相关属性。
...
@@ -145,9 +145,9 @@
...
@@ -145,9 +145,9 @@
DateTimeFormat的resolvedOptions方法会返回一个对象,该对象包含了DateTimeFormat对象的所有相关属性及其值。
DateTimeFormat的resolvedOptions方法会返回一个对象,该对象包含了DateTimeFormat对象的所有相关属性及其值。
```
js
```
js
var
options
=
{
dateStyle
:
"
full
"
,
timeStyle
:
"
full
"
};
let
options
=
{
dateStyle
:
"
full
"
,
timeStyle
:
"
full
"
};
var
dateTimeFormat
=
new
Intl
.
DateTimeFormat
(
"
zh-CN
"
,
options
);
let
dateTimeFormat
=
new
Intl
.
DateTimeFormat
(
"
zh-CN
"
,
options
);
var
resolvedOptions
=
dateTimeFormat
.
resolvedOptions
();
// resolvedOptions = {"locale": "zh-CN", "calendar": "gregorian", "dateStyle":"full", "timeStyle":"full", "timeZone": "CST"}
let
resolvedOptions
=
dateTimeFormat
.
resolvedOptions
();
// resolvedOptions = {"locale": "zh-CN", "calendar": "gregorian", "dateStyle":"full", "timeStyle":"full", "timeZone": "CST"}
```
```
## 数字格式化
## 数字格式化
...
@@ -178,14 +178,14 @@
...
@@ -178,14 +178,14 @@
一种方法是使用NumberFormat提供的默认构造函数,通过访问系统的语言和地区以获取系统默认Locale并进行设置(intl为导入的模块名)。
一种方法是使用NumberFormat提供的默认构造函数,通过访问系统的语言和地区以获取系统默认Locale并进行设置(intl为导入的模块名)。
```
js
```
js
var
numberFormat
=
new
Intl
.
NumberFormat
();
let
numberFormat
=
new
Intl
.
NumberFormat
();
```
```
另一种方法是使用开发者提供的Locale和格式化参数来创建数字格式化对象。其中,格式化参数是可选的,完整的格式化参数列表参见[NumberOptions](../reference/apis/js-apis-intl.md#numberoptions)。
另一种方法是使用开发者提供的Locale和格式化参数来创建数字格式化对象。其中,格式化参数是可选的,完整的格式化参数列表参见[NumberOptions](../reference/apis/js-apis-intl.md#numberoptions)。
```
js
```
js
var
options
=
{
compactDisplay
:
"
short
"
,
notation
:
"
compact
"
};
let
options
=
{
compactDisplay
:
"
short
"
,
notation
:
"
compact
"
};
var
numberFormat
=
new
Intl
.
NumberFormat
(
"
zh-CN
"
,
options
);
let
numberFormat
=
new
Intl
.
NumberFormat
(
"
zh-CN
"
,
options
);
```
```
3.
数字格式化。
3.
数字格式化。
...
@@ -193,10 +193,10 @@
...
@@ -193,10 +193,10 @@
使用NumberFormat的format方法对传入的数字进行格式化。该方法返回一个字符串作为格式化的结果。
使用NumberFormat的format方法对传入的数字进行格式化。该方法返回一个字符串作为格式化的结果。
```
js
```
js
var
options
=
{
compactDisplay
:
"
short
"
,
notation
:
"
compact
"
};
let
options
=
{
compactDisplay
:
"
short
"
,
notation
:
"
compact
"
};
var
numberFormat
=
new
Intl
.
NumberFormat
(
"
zh-CN
"
,
options
);
let
numberFormat
=
new
Intl
.
NumberFormat
(
"
zh-CN
"
,
options
);
var
number
=
1234.5678
let
number
=
1234.5678
var
formatResult
=
numberFormat
.
format
(
number
);
// formatResult = "1235"
let
formatResult
=
numberFormat
.
format
(
number
);
// formatResult = "1235"
```
```
4.
访问数字格式化对象的相关属性。
4.
访问数字格式化对象的相关属性。
...
@@ -204,9 +204,9 @@
...
@@ -204,9 +204,9 @@
NumberFormat的resolvedOptions方法会返回一个对象,该对象包含了NumberFormat对象的所有相关属性及其值。
NumberFormat的resolvedOptions方法会返回一个对象,该对象包含了NumberFormat对象的所有相关属性及其值。
```
js
```
js
var
options
=
{
compactDisplay
:
"
short
"
,
notation
:
"
compact
"
};
let
options
=
{
compactDisplay
:
"
short
"
,
notation
:
"
compact
"
};
var
numberFormat
=
new
Intl
.
NumberFormat
(
"
zh-CN
"
,
options
);
let
numberFormat
=
new
Intl
.
NumberFormat
(
"
zh-CN
"
,
options
);
var
resolvedOptions
=
numberFormat
.
resolvedOptions
();
// resolvedOptions = {"locale": "zh-CN", "compactDisplay": "short", "notation": "compact", "numberingSystem": "Latn"}
let
resolvedOptions
=
numberFormat
.
resolvedOptions
();
// resolvedOptions = {"locale": "zh-CN", "compactDisplay": "short", "notation": "compact", "numberingSystem": "Latn"}
```
```
## 字符串排序
## 字符串排序
...
@@ -237,14 +237,14 @@
...
@@ -237,14 +237,14 @@
一种方法是使用Collator提供的默认构造函数,通过访问系统的语言和地区以获取系统默认Locale并进行设置(intl为导入的模块名)。
一种方法是使用Collator提供的默认构造函数,通过访问系统的语言和地区以获取系统默认Locale并进行设置(intl为导入的模块名)。
```
js
```
js
var
collator
=
new
Intl
.
Collator
();
let
collator
=
new
Intl
.
Collator
();
```
```
另一种方法是使用开发者提供的Locale和其他相关参数来创建Collator对象,完整的参数列表参见[CollatorOptions](../reference/apis/js-apis-intl.md#collatoroptions9)。
另一种方法是使用开发者提供的Locale和其他相关参数来创建Collator对象,完整的参数列表参见[CollatorOptions](../reference/apis/js-apis-intl.md#collatoroptions9)。
其中,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'。
其中,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
```
js
var
collator
=
new
Intl
.
Collator
(
"
zh-CN
"
,
{
localeMatcher
:
"
best fit
"
,
usage
:
"
sort
"
,
sensitivity
:
"
case
"
});
let
collator
=
new
Intl
.
Collator
(
"
zh-CN
"
,
{
localeMatcher
:
"
best fit
"
,
usage
:
"
sort
"
,
sensitivity
:
"
case
"
});
```
```
3.
比较字符串。
3.
比较字符串。
...
@@ -252,10 +252,10 @@
...
@@ -252,10 +252,10 @@
使用Collator的compare方法对传入的两个字符串进行比较。该方法返回一个数值作为比较的结果,返回-1表示第一个字符串小于第二个字符串,返回1表示第一个字符大于第二个字符串,返回0表示两个字符串相同。基于两个字符串的比较结果,开发者可以字符串集合进行排序。
使用Collator的compare方法对传入的两个字符串进行比较。该方法返回一个数值作为比较的结果,返回-1表示第一个字符串小于第二个字符串,返回1表示第一个字符大于第二个字符串,返回0表示两个字符串相同。基于两个字符串的比较结果,开发者可以字符串集合进行排序。
```
js
```
js
var
collator
=
new
Intl
.
Collator
(
"
zh-CN
"
,
{
localeMatcher
:
"
best fit
"
,
usage
:
"
sort
"
,
sensitivity
:
"
case
"
});
let
collator
=
new
Intl
.
Collator
(
"
zh-CN
"
,
{
localeMatcher
:
"
best fit
"
,
usage
:
"
sort
"
,
sensitivity
:
"
case
"
});
var
str1
=
"
first string
"
;
let
str1
=
"
first string
"
;
var
str2
=
"
second string
"
;
let
str2
=
"
second string
"
;
var
compareResult
=
collator
.
compare
(
str1
,
str2
);
// compareResult = -1
let
compareResult
=
collator
.
compare
(
str1
,
str2
);
// compareResult = -1
str1
=
"
first
"
;
str1
=
"
first
"
;
str2
=
"
First
"
;
str2
=
"
First
"
;
compareResult
=
collator
.
compare
(
str1
,
str2
);
// compareResult = -1
compareResult
=
collator
.
compare
(
str1
,
str2
);
// compareResult = -1
...
@@ -266,8 +266,8 @@
...
@@ -266,8 +266,8 @@
Collator的resolvedOptions方法会返回一个对象,该对象包含了Collator对象的所有相关属性及其值。
Collator的resolvedOptions方法会返回一个对象,该对象包含了Collator对象的所有相关属性及其值。
```
js
```
js
var
collator
=
new
Intl
.
Collator
(
"
zh-CN
"
,
{
localeMatcher
:
"
best fit
"
,
usage
:
"
sort
"
});
let
collator
=
new
Intl
.
Collator
(
"
zh-CN
"
,
{
localeMatcher
:
"
best fit
"
,
usage
:
"
sort
"
});
var
options
=
collator
.
resolvedOptions
();
// options = {"localeMatcher": "best fit", "locale": "zh-CN", "usage": "sort", "sensitivity": "variant", "ignorePunctuation": "false", "numeric": false, "caseFirst": "false", "collation": "default"}
let
options
=
collator
.
resolvedOptions
();
// options = {"localeMatcher": "best fit", "locale": "zh-CN", "usage": "sort", "sensitivity": "variant", "ignorePunctuation": "false", "numeric": false, "caseFirst": "false", "collation": "default"}
```
```
## 判定单复数类别
## 判定单复数类别
...
@@ -298,13 +298,13 @@
...
@@ -298,13 +298,13 @@
一种方法是使用PluralRules提供的默认构造函数,通过访问系统的语言和地区以获取系统默认Locale并进行设置(intl为导入的模块名)。
一种方法是使用PluralRules提供的默认构造函数,通过访问系统的语言和地区以获取系统默认Locale并进行设置(intl为导入的模块名)。
```
js
```
js
var
pluralRules
=
new
Intl
.
PluralRules
();
let
pluralRules
=
new
Intl
.
PluralRules
();
```
```
另一种方法是使用开发者提供的Locale和其他相关参数来创建单复数对象。完整的参数列表参见[PluralRulesOptions](../reference/apis/js-apis-intl.md#pluralrulesoptions9)。
另一种方法是使用开发者提供的Locale和其他相关参数来创建单复数对象。完整的参数列表参见[PluralRulesOptions](../reference/apis/js-apis-intl.md#pluralrulesoptions9)。
```
js
```
js
var
pluralRules
=
new
Intl
.
PluralRules
(
"
zh-CN
"
,
{
localeMatcher
:
"
best fit
"
,
type
:
"
cardinal
"
});
let
pluralRules
=
new
Intl
.
PluralRules
(
"
zh-CN
"
,
{
localeMatcher
:
"
best fit
"
,
type
:
"
cardinal
"
});
```
```
3.
计算数字单复数类别。
3.
计算数字单复数类别。
...
@@ -312,9 +312,9 @@
...
@@ -312,9 +312,9 @@
使用PluralRules的select方法计算传入数字的单复数类别。该方法返回一个字符串作为传入数字的类别,包括:"zero", "one", "two", "few", "many", "other"六个类别。
使用PluralRules的select方法计算传入数字的单复数类别。该方法返回一个字符串作为传入数字的类别,包括:"zero", "one", "two", "few", "many", "other"六个类别。
```
js
```
js
var
pluralRules
=
new
Intl
.
PluralRules
(
"
zh-CN
"
,
{
localeMatcher
:
"
best fit
"
,
type
:
"
cardinal
"
});
let
pluralRules
=
new
Intl
.
PluralRules
(
"
zh-CN
"
,
{
localeMatcher
:
"
best fit
"
,
type
:
"
cardinal
"
});
var
number
=
1234.5678
let
number
=
1234.5678
var
categoryResult
=
pluralRules
.
select
(
number
);
// categoryResult = "other"
let
categoryResult
=
pluralRules
.
select
(
number
);
// categoryResult = "other"
```
```
## 相对时间格式化
## 相对时间格式化
...
@@ -346,13 +346,13 @@
...
@@ -346,13 +346,13 @@
一种方法是使用RelativeTimeFormat提供的默认构造函数,通过访问系统的语言和地区以获取系统默认Locale并进行设置(intl为导入的模块名)。
一种方法是使用RelativeTimeFormat提供的默认构造函数,通过访问系统的语言和地区以获取系统默认Locale并进行设置(intl为导入的模块名)。
```
js
```
js
var
relativeTimeFormat
=
new
Intl
.
RelativeTimeFormat
();
let
relativeTimeFormat
=
new
Intl
.
RelativeTimeFormat
();
```
```
另一种方法是使用开发者提供的Locale和格式化参数来创建相对时间格式化对象。其中,格式化参数是可选的,完整的参数列表参见[ RelativeTimeFormatInputOptions](../reference/apis/js-apis-intl.md#relativetimeformatinputoptions9)。
另一种方法是使用开发者提供的Locale和格式化参数来创建相对时间格式化对象。其中,格式化参数是可选的,完整的参数列表参见[ RelativeTimeFormatInputOptions](../reference/apis/js-apis-intl.md#relativetimeformatinputoptions9)。
```
js
```
js
var
relativeTimeFormat
=
new
Intl
.
RelativeTimeFormat
(
"
zh-CN
"
,
{
numeric
:
"
always
"
,
style
:
"
long
"
});
let
relativeTimeFormat
=
new
Intl
.
RelativeTimeFormat
(
"
zh-CN
"
,
{
numeric
:
"
always
"
,
style
:
"
long
"
});
```
```
3.
相对时间格式化。
3.
相对时间格式化。
...
@@ -360,10 +360,10 @@
...
@@ -360,10 +360,10 @@
使用RelativeTimeFormat的format方法对相对时间进行格式化。方法接收一个表示相对时间长度的数值和表示单位的字符串,其中单位包括:"year", "quarter", "month", "week", "day", "hour", "minute", "second"。方法返回一个字符串作为格式化的结果。
使用RelativeTimeFormat的format方法对相对时间进行格式化。方法接收一个表示相对时间长度的数值和表示单位的字符串,其中单位包括:"year", "quarter", "month", "week", "day", "hour", "minute", "second"。方法返回一个字符串作为格式化的结果。
```
js
```
js
var
relativeTimeFormat
=
new
Intl
.
RelativeTimeFormat
(
"
zh-CN
"
,
{
numeric
:
"
always
"
,
style
:
"
long
"
});
let
relativeTimeFormat
=
new
Intl
.
RelativeTimeFormat
(
"
zh-CN
"
,
{
numeric
:
"
always
"
,
style
:
"
long
"
});
var
number
=
2
;
let
number
=
2
;
var
unit
=
"
year
"
let
unit
=
"
year
"
var
formatResult
=
relativeTimeFormat
.
format
(
number
,
unit
);
// 2年后
let
formatResult
=
relativeTimeFormat
.
format
(
number
,
unit
);
// 2年后
```
```
4.
获取相对时间格式化结果的各个部分。
4.
获取相对时间格式化结果的各个部分。
...
@@ -371,10 +371,10 @@
...
@@ -371,10 +371,10 @@
获取相对时间格式化结果的各个部分,从而自定义格式化结果。
获取相对时间格式化结果的各个部分,从而自定义格式化结果。
```
js
```
js
var
relativeTimeFormat
=
new
Intl
.
RelativeTimeFormat
(
"
zh-CN
"
,
{
numeric
:
"
always
"
,
style
:
"
long
"
});
let
relativeTimeFormat
=
new
Intl
.
RelativeTimeFormat
(
"
zh-CN
"
,
{
numeric
:
"
always
"
,
style
:
"
long
"
});
var
number
=
2
;
let
number
=
2
;
var
unit
=
"
year
"
let
unit
=
"
year
"
var
formatPartsResult
=
relativeTimeFormat
.
formatToParts
(
number
,
unit
);
// formatPartsResult = [{"type": "integer", "value": "2", "unit": "year"}, {"type":"literal", "value": "年后"}]
let
formatPartsResult
=
relativeTimeFormat
.
formatToParts
(
number
,
unit
);
// formatPartsResult = [{"type": "integer", "value": "2", "unit": "year"}, {"type":"literal", "value": "年后"}]
```
```
5.
访问相对时间格式化对象的相关属性。
5.
访问相对时间格式化对象的相关属性。
...
@@ -382,8 +382,8 @@
...
@@ -382,8 +382,8 @@
RelativeTimeFormat的resolvedOptions方法会返回一个对象,该对象包含了RelativeTimeFormat对象的所有相关属性及其值,完整的属性列表参见[ RelativeTimeFormatResolvedOptions](../reference/apis/js-apis-intl.md#relativetimeformatresolvedoptions8)。
RelativeTimeFormat的resolvedOptions方法会返回一个对象,该对象包含了RelativeTimeFormat对象的所有相关属性及其值,完整的属性列表参见[ RelativeTimeFormatResolvedOptions](../reference/apis/js-apis-intl.md#relativetimeformatresolvedoptions8)。
```
js
```
js
var
relativeTimeFormat
=
new
Intl
.
RelativeTimeFormat
(
"
zh-CN
"
,
{
numeric
:
"
always
"
,
style
:
"
long
"
});
let
relativeTimeFormat
=
new
Intl
.
RelativeTimeFormat
(
"
zh-CN
"
,
{
numeric
:
"
always
"
,
style
:
"
long
"
});
var
options
=
relativeTimeFormat
.
resolvedOptions
();
// options = {"locale": "zh-CN", "style": "long", "numeric": "always", "numberingSystem": "latn"}
let
options
=
relativeTimeFormat
.
resolvedOptions
();
// options = {"locale": "zh-CN", "style": "long", "numeric": "always", "numberingSystem": "latn"}
```
```
## 相关实例
## 相关实例
...
...
zh-cn/application-dev/reference/apis/js-apis-i18n.md
浏览文件 @
33e0dc90
此差异已折叠。
点击以展开。
zh-cn/application-dev/reference/apis/js-apis-intl.md
浏览文件 @
33e0dc90
...
@@ -48,9 +48,9 @@ constructor()
...
@@ -48,9 +48,9 @@ constructor()
**示例:**
**示例:**
```
js
```
js
// 默认构造函数使用系统当前locale创建Locale对象
// 默认构造函数使用系统当前locale创建Locale对象
var
locale
=
new
Intl
.
Locale
()
let
locale
=
new
Intl
.
Locale
()
// 返回系统当前localel
// 返回系统当前localel
var
localeID
=
locale
.
toString
()
let
localeID
=
locale
.
toString
()
```
```
...
@@ -72,8 +72,8 @@ constructor(locale: string, options?: LocaleOptions)
...
@@ -72,8 +72,8 @@ constructor(locale: string, options?: LocaleOptions)
**示例:**
**示例:**
```
js
```
js
// 创建 "zh-CN" Locale对象
// 创建 "zh-CN" Locale对象
var
locale
=
new
Intl
.
Locale
(
"
zh-CN
"
)
let
locale
=
new
Intl
.
Locale
(
"
zh-CN
"
)
var
localeID
=
locale
.
toString
()
// localeID = "zh-CN"
let
localeID
=
locale
.
toString
()
// localeID = "zh-CN"
```
```
...
@@ -94,8 +94,8 @@ toString(): string
...
@@ -94,8 +94,8 @@ toString(): string
**示例:**
**示例:**
```
js
```
js
// 创建 "en-GB" Locale对象
// 创建 "en-GB" Locale对象
var
locale
=
new
Intl
.
Locale
(
"
en-GB
"
);
let
locale
=
new
Intl
.
Locale
(
"
en-GB
"
);
var
localeID
=
locale
.
toString
();
// localeID = "en-GB"
let
localeID
=
locale
.
toString
();
// localeID = "en-GB"
```
```
...
@@ -116,16 +116,16 @@ maximize(): Locale
...
@@ -116,16 +116,16 @@ maximize(): Locale
**示例:**
**示例:**
```
js
```
js
// 创建 "zh" Locale对象
// 创建 "zh" Locale对象
var
locale
=
new
Intl
.
Locale
(
"
zh
"
);
let
locale
=
new
Intl
.
Locale
(
"
zh
"
);
// 补齐Locale对象的脚本和地区
// 补齐Locale对象的脚本和地区
locale
.
maximize
();
l
et
maximizedLocale
=
l
ocale
.
maximize
();
var
localeID
=
l
ocale
.
toString
();
// localeID = "zh-Hans-CN"
let
localeID
=
maximizedL
ocale
.
toString
();
// localeID = "zh-Hans-CN"
// 创建 "en-US" Locale对象
// 创建 "en-US" Locale对象
locale
=
new
Intl
.
Locale
(
"
en-US
"
);
locale
=
new
Intl
.
Locale
(
"
en-US
"
);
// 补齐Locale对象的脚本
// 补齐Locale对象的脚本
locale
.
maximize
();
maximizedLocale
=
locale
.
maximize
();
localeID
=
l
ocale
.
toString
();
// localeID = "en-Latn-US"
localeID
=
maximizedL
ocale
.
toString
();
// localeID = "en-Latn-US"
```
```
...
@@ -146,16 +146,16 @@ minimize(): Locale
...
@@ -146,16 +146,16 @@ minimize(): Locale
**示例:**
**示例:**
```
js
```
js
// 创建 "zh-Hans-CN" Locale对象
// 创建 "zh-Hans-CN" Locale对象
var
locale
=
new
Intl
.
Locale
(
"
zh-Hans-CN
"
);
let
locale
=
new
Intl
.
Locale
(
"
zh-Hans-CN
"
);
// 去除Locale对象的脚本和地区
// 去除Locale对象的脚本和地区
locale
.
minimize
();
l
et
minimizedLocale
=
l
ocale
.
minimize
();
var
localeID
=
l
ocale
.
toString
();
// localeID = "zh"
let
localeID
=
minimizedL
ocale
.
toString
();
// localeID = "zh"
// 创建 "en-US" Locale对象
// 创建 "en-US" Locale对象
locale
=
new
Intl
.
Locale
(
"
en-US
"
);
locale
=
new
Intl
.
Locale
(
"
en-US
"
);
// 去除Locale对象的地区
// 去除Locale对象的地区
locale
.
minimize
();
minimizedLocale
=
locale
.
minimize
();
localeID
=
l
ocale
.
toString
();
// localeID = "en"
localeID
=
minimizedL
ocale
.
toString
();
// localeID = "en"
```
```
...
@@ -189,7 +189,7 @@ constructor()
...
@@ -189,7 +189,7 @@ constructor()
**示例:**
**示例:**
```
js
```
js
// 使用系统当前locale创建DateTimeFormat对象
// 使用系统当前locale创建DateTimeFormat对象
var
datefmt
=
new
Intl
.
DateTimeFormat
();
let
datefmt
=
new
Intl
.
DateTimeFormat
();
```
```
...
@@ -211,14 +211,14 @@ constructor(locale: string | Array<string>, options?: DateTimeOptions)
...
@@ -211,14 +211,14 @@ constructor(locale: string | Array<string>, options?: DateTimeOptions)
**示例:**
**示例:**
```
js
```
js
// 使用 "zh-CN" locale创建DateTimeFormat对象,日期风格为full,时间风格为medium
// 使用 "zh-CN" locale创建DateTimeFormat对象,日期风格为full,时间风格为medium
var
datefmt
=
new
Intl
.
DateTimeFormat
(
"
zh-CN
"
,
{
dateStyle
:
'
full
'
,
timeStyle
:
'
medium
'
});
let
datefmt
=
new
Intl
.
DateTimeFormat
(
"
zh-CN
"
,
{
dateStyle
:
'
full
'
,
timeStyle
:
'
medium
'
});
```
```
**示例:**
**示例:**
```
js
```
js
// 使用 ["ban", "zh"] locale列表创建DateTimeFormat对象,因为ban为非法LocaleID,因此使用zh Locale创建DateTimeFormat对象
// 使用 ["ban", "zh"] locale列表创建DateTimeFormat对象,因为ban为非法LocaleID,因此使用zh Locale创建DateTimeFormat对象
var
datefmt
=
new
Intl
.
DateTimeFormat
([
"
ban
"
,
"
zh
"
],
{
dateStyle
:
'
full
'
,
timeStyle
:
'
medium
'
});
let
datefmt
=
new
Intl
.
DateTimeFormat
([
"
ban
"
,
"
zh
"
],
{
dateStyle
:
'
full
'
,
timeStyle
:
'
medium
'
});
```
```
...
@@ -244,10 +244,10 @@ format(date: Date): string
...
@@ -244,10 +244,10 @@ format(date: Date): string
**示例:**
**示例:**
```
js
```
js
var
date
=
new
Date
(
2021
,
11
,
17
,
3
,
24
,
0
);
let
date
=
new
Date
(
2021
,
11
,
17
,
3
,
24
,
0
);
// 使用 en-GB locale创建DateTimeFormat对象
// 使用 en-GB locale创建DateTimeFormat对象
var
datefmt
=
new
Intl
.
DateTimeFormat
(
"
en-GB
"
);
let
datefmt
=
new
Intl
.
DateTimeFormat
(
"
en-GB
"
);
var
formattedDate
=
datefmt
.
format
(
date
);
// formattedDate "17/12/2021"
let
formattedDate
=
datefmt
.
format
(
date
);
// formattedDate "17/12/2021"
// 使用 en-GB locale创建DateTimeFormat对象,dateStyle设置为full,timeStyle设置为medium
// 使用 en-GB locale创建DateTimeFormat对象,dateStyle设置为full,timeStyle设置为medium
datefmt
=
new
Intl
.
DateTimeFormat
(
"
en-GB
"
,
{
dateStyle
:
'
full
'
,
timeStyle
:
'
medium
'
});
datefmt
=
new
Intl
.
DateTimeFormat
(
"
en-GB
"
,
{
dateStyle
:
'
full
'
,
timeStyle
:
'
medium
'
});
...
@@ -278,11 +278,11 @@ formatRange(startDate: Date, endDate: Date): string
...
@@ -278,11 +278,11 @@ formatRange(startDate: Date, endDate: Date): string
**示例:**
**示例:**
```
js
```
js
var
startDate
=
new
Date
(
2021
,
11
,
17
,
3
,
24
,
0
);
let
startDate
=
new
Date
(
2021
,
11
,
17
,
3
,
24
,
0
);
var
endDate
=
new
Date
(
2021
,
11
,
18
,
3
,
24
,
0
);
let
endDate
=
new
Date
(
2021
,
11
,
18
,
3
,
24
,
0
);
// 使用 en-GB locale创建DateTimeFormat对象
// 使用 en-GB locale创建DateTimeFormat对象
var
datefmt
=
new
Intl
.
DateTimeFormat
(
"
en-GB
"
);
let
datefmt
=
new
Intl
.
DateTimeFormat
(
"
en-GB
"
);
var
formattedDateRange
=
datefmt
.
formatRange
(
startDate
,
endDate
);
// formattedDateRange = "17/12/2021-18/12/2021"
let
formattedDateRange
=
datefmt
.
formatRange
(
startDate
,
endDate
);
// formattedDateRange = "17/12/2021-18/12/2021"
```
```
...
@@ -302,11 +302,11 @@ resolvedOptions(): DateTimeOptions
...
@@ -302,11 +302,11 @@ resolvedOptions(): DateTimeOptions
**示例:**
**示例:**
```
js
```
js
var
datefmt
=
new
Intl
.
DateTimeFormat
(
"
en-GB
"
,
{
dateStyle
:
'
full
'
,
timeStyle
:
'
medium
'
});
let
datefmt
=
new
Intl
.
DateTimeFormat
(
"
en-GB
"
,
{
dateStyle
:
'
full
'
,
timeStyle
:
'
medium
'
});
// 返回DateTimeFormat对象的配置项
// 返回DateTimeFormat对象的配置项
var
options
=
datefmt
.
resolvedOptions
();
let
options
=
datefmt
.
resolvedOptions
();
var
dateStyle
=
options
.
dateStyle
;
// dateStyle = "full"
let
dateStyle
=
options
.
dateStyle
;
// dateStyle = "full"
var
timeStyle
=
options
.
timeStyle
;
// timeStyle = "medium"
let
timeStyle
=
options
.
timeStyle
;
// timeStyle = "medium"
```
```
...
@@ -353,7 +353,7 @@ constructor()
...
@@ -353,7 +353,7 @@ constructor()
**示例:**
**示例:**
```
js
```
js
// 使用系统当前locale创建NumberFormat对象
// 使用系统当前locale创建NumberFormat对象
var
numfmt
=
new
Intl
.
NumberFormat
();
let
numfmt
=
new
Intl
.
NumberFormat
();
```
```
...
@@ -375,7 +375,7 @@ constructor(locale: string | Array<string>, options?: NumberOptions)
...
@@ -375,7 +375,7 @@ constructor(locale: string | Array<string>, options?: NumberOptions)
**示例:**
**示例:**
```
js
```
js
// 使用 en-GB locale创建NumberFormat对象,style设置为decimal,notation设置为scientific
// 使用 en-GB locale创建NumberFormat对象,style设置为decimal,notation设置为scientific
var
numfmt
=
new
Intl
.
NumberFormat
(
"
en-GB
"
,
{
style
:
'
decimal
'
,
notation
:
"
scientific
"
});
let
numfmt
=
new
Intl
.
NumberFormat
(
"
en-GB
"
,
{
style
:
'
decimal
'
,
notation
:
"
scientific
"
});
```
```
...
@@ -403,8 +403,8 @@ format(number: number): string;
...
@@ -403,8 +403,8 @@ format(number: number): string;
**示例:**
**示例:**
```
js
```
js
// 使用 ["en-GB", "zh"] locale列表创建NumberFormat对象,因为en-GB为合法LocaleID,因此使用en-GB创建NumberFormat对象
// 使用 ["en-GB", "zh"] locale列表创建NumberFormat对象,因为en-GB为合法LocaleID,因此使用en-GB创建NumberFormat对象
var
numfmt
=
new
Intl
.
NumberFormat
([
"
en-GB
"
,
"
zh
"
],
{
style
:
'
decimal
'
,
notation
:
"
scientific
"
});
let
numfmt
=
new
Intl
.
NumberFormat
([
"
en-GB
"
,
"
zh
"
],
{
style
:
'
decimal
'
,
notation
:
"
scientific
"
});
var
formattedNumber
=
numfmt
.
format
(
1223
);
// formattedNumber = 1.223E3
let
formattedNumber
=
numfmt
.
format
(
1223
);
// formattedNumber = 1.223E3
```
```
...
@@ -425,11 +425,11 @@ resolvedOptions(): NumberOptions
...
@@ -425,11 +425,11 @@ resolvedOptions(): NumberOptions
**示例:**
**示例:**
```
js
```
js
var
numfmt
=
new
Intl
.
NumberFormat
([
"
en-GB
"
,
"
zh
"
],
{
style
:
'
decimal
'
,
notation
:
"
scientific
"
});
let
numfmt
=
new
Intl
.
NumberFormat
([
"
en-GB
"
,
"
zh
"
],
{
style
:
'
decimal
'
,
notation
:
"
scientific
"
});
// 获取NumberFormat对象配置项
// 获取NumberFormat对象配置项
var
options
=
numfmt
.
resolvedOptions
();
let
options
=
numfmt
.
resolvedOptions
();
var
style
=
options
.
style
;
// style = decimal
let
style
=
options
.
style
;
// style = decimal
var
notation
=
options
.
notation
// notation = scientific
let
notation
=
options
.
notation
// notation = scientific
```
```
...
@@ -476,7 +476,7 @@ constructor()
...
@@ -476,7 +476,7 @@ constructor()
**示例:**
**示例:**
```
js
```
js
// 使用系统locale创建Collator对象
// 使用系统locale创建Collator对象
var
collator
=
new
Intl
.
Collator
();
let
collator
=
new
Intl
.
Collator
();
```
```
...
@@ -498,7 +498,7 @@ constructor(locale: string | Array<string>, options?: CollatorOptions)
...
@@ -498,7 +498,7 @@ constructor(locale: string | Array<string>, options?: CollatorOptions)
**示例:**
**示例:**
```
js
```
js
// 使用 zh-CN locale创建Collator对象,localeMatcher设置为lookup,usage设置为sort
// 使用 zh-CN locale创建Collator对象,localeMatcher设置为lookup,usage设置为sort
var
collator
=
new
Intl
.
Collator
(
"
zh-CN
"
,
{
localeMatcher
:
"
lookup
"
,
usage
:
"
sort
"
});
let
collator
=
new
Intl
.
Collator
(
"
zh-CN
"
,
{
localeMatcher
:
"
lookup
"
,
usage
:
"
sort
"
});
```
```
...
@@ -526,9 +526,9 @@ compare(first: string, second: string): number
...
@@ -526,9 +526,9 @@ compare(first: string, second: string): number
**示例:**
**示例:**
```
js
```
js
// 使用en-GB locale创建Collator对象
// 使用en-GB locale创建Collator对象
var
collator
=
new
Intl
.
Collator
(
"
en-GB
"
);
let
collator
=
new
Intl
.
Collator
(
"
en-GB
"
);
// 比较 "first" 和 "second" 的先后顺序
// 比较 "first" 和 "second" 的先后顺序
var
compareResult
=
collator
.
compare
(
"
first
"
,
"
second
"
);
// compareResult = -1
let
compareResult
=
collator
.
compare
(
"
first
"
,
"
second
"
);
// compareResult = -1
```
```
...
@@ -548,11 +548,11 @@ resolvedOptions(): CollatorOptions
...
@@ -548,11 +548,11 @@ resolvedOptions(): CollatorOptions
**示例:**
**示例:**
```
js
```
js
var
collator
=
new
Intl
.
Collator
(
"
zh-Hans
"
,
{
usage
:
'
sort
'
,
ignorePunctuation
:
'
true
'
});
let
collator
=
new
Intl
.
Collator
(
"
zh-Hans
"
,
{
usage
:
'
sort
'
,
ignorePunctuation
:
true
});
// 获取Collator对象的配置项
// 获取Collator对象的配置项
var
options
=
collator
.
resolvedOptions
();
let
options
=
collator
.
resolvedOptions
();
var
usage
=
options
.
usage
;
// usage = "sort"
let
usage
=
options
.
usage
;
// usage = "sort"
var
ignorePunctuation
=
options
.
ignorePunctuation
// ignorePunctuation = true
let
ignorePunctuation
=
options
.
ignorePunctuation
// ignorePunctuation = true
```
```
...
@@ -566,7 +566,7 @@ resolvedOptions(): CollatorOptions
...
@@ -566,7 +566,7 @@ resolvedOptions(): CollatorOptions
| ----------------- | ------- | ---- | ---- | ---------------------------------------- |
| ----------------- | ------- | ---- | ---- | ---------------------------------------- |
| localeMatcher | string | 是 | 是 | locale匹配算法,取值范围:"best
fit",
"lookup"。 |
| localeMatcher | string | 是 | 是 | locale匹配算法,取值范围:"best
fit",
"lookup"。 |
| usage | string | 是 | 是 | 比较的用途,取值范围:"sort",
"search"。 |
| usage | string | 是 | 是 | 比较的用途,取值范围:"sort",
"search"。 |
| sensitivity | string | 是 | 是 | 表示字符串中的哪些差异会导致非零结果值,取值范围:"base",
"accent",
"case",
"
var
iant"。 |
| sensitivity | string | 是 | 是 | 表示字符串中的哪些差异会导致非零结果值,取值范围:"base",
"accent",
"case",
"
let
iant"。 |
| ignorePunctuation | boolean | 是 | 是 | 表示是否忽略标点符号,取值范围:true,
false。 |
| ignorePunctuation | boolean | 是 | 是 | 表示是否忽略标点符号,取值范围:true,
false。 |
| collation | string | 是 | 是 | 排序规则,取值范围:"big5han",
"compat",
"dict",
"direct",
"ducet",
"eor",
"gb2312",
"phonebk",
"phonetic",
"pinyin",
"reformed",
"searchjl",
"stroke",
"trad",
"unihan",
"zhuyin"。 |
| collation | string | 是 | 是 | 排序规则,取值范围:"big5han",
"compat",
"dict",
"direct",
"ducet",
"eor",
"gb2312",
"phonebk",
"phonetic",
"pinyin",
"reformed",
"searchjl",
"stroke",
"trad",
"unihan",
"zhuyin"。 |
| numeric | boolean | 是 | 是 | 是否使用数字排序,取值范围:true,
false。 |
| numeric | boolean | 是 | 是 | 是否使用数字排序,取值范围:true,
false。 |
...
@@ -580,14 +580,14 @@ resolvedOptions(): CollatorOptions
...
@@ -580,14 +580,14 @@ resolvedOptions(): CollatorOptions
constructor()
constructor()
创建
PluralRules对象
。
创建
单复数对象来计算数字的单复数类别
。
**系统能力**
:SystemCapability.Global.I18n
**系统能力**
:SystemCapability.Global.I18n
**示例:**
**示例:**
```
js
```
js
// 使用系统locale创建PluralRules对象
// 使用系统locale创建PluralRules对象
var
pluralRules
=
new
Intl
.
PluralRules
();
let
pluralRules
=
new
Intl
.
PluralRules
();
```
```
...
@@ -595,7 +595,7 @@ constructor()
...
@@ -595,7 +595,7 @@ constructor()
constructor(locale: string | Array
<
string
>
, options?: PluralRulesOptions)
constructor(locale: string | Array
<
string
>
, options?: PluralRulesOptions)
创建
PluralRules对象
。
创建
单复数对象来计算数字的单复数类别
。
**系统能力**
:SystemCapability.Global.I18n
**系统能力**
:SystemCapability.Global.I18n
...
@@ -609,7 +609,7 @@ constructor(locale: string | Array<string>, options?: PluralRulesOptions)
...
@@ -609,7 +609,7 @@ constructor(locale: string | Array<string>, options?: PluralRulesOptions)
**示例:**
**示例:**
```
js
```
js
// 使用 zh-CN locale创建PluralRules对象,localeMatcher设置为lookup,type设置为cardinal
// 使用 zh-CN locale创建PluralRules对象,localeMatcher设置为lookup,type设置为cardinal
var
pluralRules
=
new
Intl
.
PluralRules
(
"
zh-CN
"
,
{
"
localeMatcher
"
:
"
lookup
"
,
"
type
"
:
"
cardinal
"
});
let
pluralRules
=
new
Intl
.
PluralRules
(
"
zh-CN
"
,
{
"
localeMatcher
"
:
"
lookup
"
,
"
type
"
:
"
cardinal
"
});
```
```
...
@@ -636,9 +636,14 @@ select(n: number): string
...
@@ -636,9 +636,14 @@ select(n: number): string
**示例:**
**示例:**
```
js
```
js
// 使用 zh-Hans locale创建PluralRules对象
// 使用 zh-Hans locale创建PluralRules对象
var
p
luralRules
=
new
Intl
.
PluralRules
(
"
zh-Hans
"
);
let
zhP
luralRules
=
new
Intl
.
PluralRules
(
"
zh-Hans
"
);
// 计算 zh-Hans locale中数字1对应的单复数类别
// 计算 zh-Hans locale中数字1对应的单复数类别
var
plural
=
pluralRules
.
select
(
1
);
// plural = other
let
plural
=
zhPluralRules
.
select
(
1
);
// plural = other
// 使用 en-US locale创建PluralRules对象
let
enPluralRules
=
new
Intl
.
PluralRules
(
"
en-US
"
);
// 计算 en-US locale中数字1对应的单复数类别
plural
=
enPluralRules
.
select
(
1
);
// plural = one
```
```
...
@@ -673,7 +678,7 @@ constructor()
...
@@ -673,7 +678,7 @@ constructor()
**示例:**
**示例:**
```
js
```
js
// 使用系统locale创建RelativeTimeFormat对象
// 使用系统locale创建RelativeTimeFormat对象
var
relativetimefmt
=
new
Intl
.
RelativeTimeFormat
();
let
relativetimefmt
=
new
Intl
.
RelativeTimeFormat
();
```
```
...
@@ -695,7 +700,7 @@ constructor(locale: string | Array<string>, options?: RelativeTimeFormatIn
...
@@ -695,7 +700,7 @@ constructor(locale: string | Array<string>, options?: RelativeTimeFormatIn
**示例:**
**示例:**
```
js
```
js
// 使用 zh-CN locale创建RelativeTimeFormat对象,localeMatcher设置为lookup,numeric设置为always,style设置为long
// 使用 zh-CN locale创建RelativeTimeFormat对象,localeMatcher设置为lookup,numeric设置为always,style设置为long
var
relativeTimeFormat
=
new
Intl
.
RelativeTimeFormat
(
"
zh-CN
"
,
{
"
localeMatcher
"
:
"
lookup
"
,
"
numeric
"
:
"
always
"
,
"
style
"
:
"
long
"
});
let
relativeTimeFormat
=
new
Intl
.
RelativeTimeFormat
(
"
zh-CN
"
,
{
"
localeMatcher
"
:
"
lookup
"
,
"
numeric
"
:
"
always
"
,
"
style
"
:
"
long
"
});
```
```
...
@@ -723,9 +728,9 @@ format(value: number, unit: string): string
...
@@ -723,9 +728,9 @@ format(value: number, unit: string): string
**示例:**
**示例:**
```
js
```
js
// 使用 zh-CN locale创建RelativeTimeFormat对象
// 使用 zh-CN locale创建RelativeTimeFormat对象
var
relativetimefmt
=
new
Intl
.
RelativeTimeFormat
(
"
zh-CN
"
);
let
relativetimefmt
=
new
Intl
.
RelativeTimeFormat
(
"
zh-CN
"
);
// 计算 zh-CN locale中数字3,单位quarter的本地化表示
// 计算 zh-CN locale中数字3,单位quarter的本地化表示
var
formatResult
=
relativetimefmt
.
format
(
3
,
"
quarter
"
)
;
// formatResult = "3个季度后"
let
formatResult
=
relativetimefmt
.
format
(
3
,
"
quarter
"
);
// formatResult = "3个季度后"
```
```
...
@@ -753,8 +758,8 @@ formatToParts(value: number, unit: string): Array<object>
...
@@ -753,8 +758,8 @@ formatToParts(value: number, unit: string): Array<object>
**示例:**
**示例:**
```
js
```
js
// 使用 en locale创建RelativeTimeFormat对象,numeric设置为auto
// 使用 en locale创建RelativeTimeFormat对象,numeric设置为auto
var
relativetimefmt
=
new
Intl
.
RelativeTimeFormat
(
"
en
"
,
{
"
numeric
"
:
"
auto
"
});
let
relativetimefmt
=
new
Intl
.
RelativeTimeFormat
(
"
en
"
,
{
"
numeric
"
:
"
auto
"
});
var
parts
=
relativetimefmt
.
formatToParts
(
10
,
"
seconds
"
);
// parts = [ {type: "literal", value: "in"}, {type: "integer", value: 10, unit: "second"}, {type: "literal", value: "seconds"} ]
let
parts
=
relativetimefmt
.
formatToParts
(
10
,
"
seconds
"
);
// parts = [ {type: "literal", value: "in"}, {type: "integer", value: 10, unit: "second"}, {type: "literal", value: "seconds"} ]
```
```
...
@@ -775,10 +780,10 @@ resolvedOptions(): RelativeTimeFormatResolvedOptions
...
@@ -775,10 +780,10 @@ resolvedOptions(): RelativeTimeFormatResolvedOptions
**示例:**
**示例:**
```
js
```
js
// 使用 en-GB locale创建RelativeTimeFormat对象
// 使用 en-GB locale创建RelativeTimeFormat对象
var
relativetimefmt
=
new
Intl
.
RelativeTimeFormat
(
"
en-GB
"
,
{
style
:
"
short
"
});
let
relativetimefmt
=
new
Intl
.
RelativeTimeFormat
(
"
en-GB
"
,
{
style
:
"
short
"
});
// 获取RelativeTimeFormat对象配置项
// 获取RelativeTimeFormat对象配置项
var
options
=
relativetimefmt
.
resolvedOptions
();
let
options
=
relativetimefmt
.
resolvedOptions
();
var
style
=
options
.
style
;
// style = "short"
let
style
=
options
.
style
;
// style = "short"
```
```
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录