intl-guidelines.md 20.1 KB
Newer Older
S
shawn_he 已提交
1
# intl Development
H
HelloCrease 已提交
2

S
shawn_he 已提交
3
The **intl** module provides basic i18n capabilities, such as time and date formatting, number formatting, and string sorting, through the standard i18n APIs defined in ECMA 402. For more details about APIs and their usage, see [intl](../reference/apis/js-apis-intl.md).
H
HelloCrease 已提交
4

S
shawn_he 已提交
5
The [I18N](i18n-guidelines.md) module provides enhanced I18N capabilities through supplementary interfaces that are not defined in ECMA 402. It works with the Intl module to provide a complete suite of I18N capabilities.
6

H
HelloCrease 已提交
7 8
## Setting Locale Information

S
shawn_he 已提交
9
[Locale](../reference/apis/js-apis-intl.md#locale) provides APIs to maximize or minimize the locale information.
H
HelloCrease 已提交
10 11 12

### Available APIs

S
shawn_he 已提交
13
| Class| API| Description|
H
HelloCrease 已提交
14
| -------- | -------- | -------- |
S
shawn_he 已提交
15 16 17 18 19
| Locale | constructor()<sup>8+</sup> | Instantiates a **Locale** object.|
| Locale | constructor(locale:string,options?:LocaleOptions) | Instantiates a **Locale** object based on the locale parameter and options.|
| Locale | toString():string | Converts locale information into a string.|
| Locale | maximize():Locale | Maximizes locale information.|
| Locale | minimize():Locale | Minimizes locale information.|
H
HelloCrease 已提交
20 21 22

### How to Develop

S
shawn_he 已提交
23
1. Import the **intl** module.
S
shawn_he 已提交
24

S
shawn_he 已提交
25 26 27
   Importing an incorrect bundle can lead to unexpected API behavior.
   
   ```js
S
shawn_he 已提交
28
   import Intl from '@ohos.intl';
S
shawn_he 已提交
29 30 31 32 33
   ```

2. Instantiates a **Locale** object.

   Create a **Locale** object using the **Locale** constructor. This API receives a string representing the locale and an optional [attribute](../reference/apis/js-apis-intl.md#localeoptions) list. (Note that **intl** is the name of the imported module.)
H
HelloCrease 已提交
34

S
shawn_he 已提交
35 36 37 38
   A **Locale** object consists of four parts: language, script, region, and extension, which are separated by using a hyphen (-).
   -  Language: mandatory. It is represented by a two-letter or three-letter code as defined in ISO-639. For example, **en** indicates English and **zh** indicates Chinese.
   -  Script: optional. It is represented by a four-letter code as defined in ISO-15924. The first letter is in uppercase, and the remaining three letters are in lowercase. For example, **Hant** represents the traditional Chinese, and **Hans** represents the simplified Chinese.
   -  Country or region: optional. It is represented by two-letter code as defined in ISO-3166. Both letters are in uppercase. For example, **CN** represents China, and **US** represents the United States.
S
shawn_he 已提交
39
   -  Extensions: optional. Each extension consists of two parts, key and value. Currently, the extensions listed in the following table are supported (see BCP 47 Extensions). Extensions can be in any sequence and are written in the format of **-key-value**. They are appended to the language, script, and region by using **-u**. For example, **zh-u-nu-latn-ca-chinese** indicates that the latn digital system and chinese calendar system are used. Extensions can also be passed via the second parameter.
S
shawn_he 已提交
40 41 42 43 44 45 46
      | Extended Parameter ID| Description|
      | -------- | -------- |
      | ca | Calendar algorithm.|
      | co | Collation type.|
      | hc | Hour cycle.|
      | nu | Numbering system.|
      | kn | Whether numeric collation is used when sorting or comparing strings.|
S
shawn_he 已提交
47
      | kf | Whether capitalization is considered when sorting or comparing strings.|
S
shawn_he 已提交
48

H
HelloCrease 已提交
49
   
S
shawn_he 已提交
50
   ```js
S
shawn_he 已提交
51 52
   let locale = "zh-CN";
   let options = {caseFirst: "false", calendar: "chinese", collation: "pinyin"};
S
shawn_he 已提交
53
   let localeObj = new Intl.Locale(locale, options);
H
HelloCrease 已提交
54 55
   ```

S
shawn_he 已提交
56
3. Obtain the string representing a **Locale** object.
S
shawn_he 已提交
57

S
shawn_he 已提交
58 59
     Call **toString** to obtain the string representing a **Locale** object, including the language, region, and other options.
     
S
shawn_he 已提交
60
   ```js
S
shawn_he 已提交
61
   let localeStr = localeObj.toString(); // localeStr = "zh-CN-u-ca-chinese-co-pinyin-kf-false
H
HelloCrease 已提交
62 63
   ```

S
shawn_he 已提交
64
4. Maximize locale information.
S
shawn_he 已提交
65

S
shawn_he 已提交
66 67
     Call **maximize** to maximize locale information; that is, supplement the missing script and region information.
     
S
shawn_he 已提交
68
   ```js
S
shawn_he 已提交
69
   let maximizedLocale = localeObj.maximize();
S
shawn_he 已提交
70
   let maximizedLocaleStr = maximizedLocale.toString(); // localeStr = "zh-Hans-CN-u-ca-chinese-co-pinyin-kf-false
H
HelloCrease 已提交
71 72
   ```

S
shawn_he 已提交
73
5. Minimize locale information.
S
shawn_he 已提交
74

S
shawn_he 已提交
75 76
     Call **minimize** to minimize locale information; that is, delete the unnecessary script and region information.
     
S
shawn_he 已提交
77
   ```js
S
shawn_he 已提交
78
   let minimizedLocale = localeObj.minimize();
S
shawn_he 已提交
79
   let minimizedLocaleStr = minimizedLocale.toString(); // zh-u-ca-chinese-co-pinyin-kf-false
H
HelloCrease 已提交
80 81 82 83
   ```

## Formatting the Date and Time

S
shawn_he 已提交
84
[DateTimeFormat](../reference/apis/js-apis-intl.md#datetimeformat) provides APIs to format the date and time for the specified locale.
H
HelloCrease 已提交
85 86 87

### Available APIs

S
shawn_he 已提交
88
| Class| API| Description|
H
HelloCrease 已提交
89
| -------- | -------- | -------- |
S
shawn_he 已提交
90 91 92 93 94
| DateTimeFormat | constructor()<sup>8+</sup> | Creates a **DateTimeFormat** object.|
| DateTimeFormat | constructor(locale:string\|Array&lt;string&gt;,options?:DateTimeOptions) | Creates a **DateTimeFormat** object and sets the locale and other formatting-related attributes.|
| DateTimeFormat | format(date:Date):string | Calculates the date and time based on the locale and other formatting-related attributes of the **DateTimeFormat** object.|
| DateTimeFormat | formatRange(startDate:Date,endDate:Date):string | Calculates the period based on the locale and other formatting-related attributes of the **DateTimeFormat** object.|
| DateTimeFormat | resolvedOptions():DateTimeOptions | Obtains the related attributes of the **DateTimeFormat** object.|
H
HelloCrease 已提交
95 96 97

### How to Develop

S
shawn_he 已提交
98 99 100 101 102
1. Import the **intl** module.

   Importing an incorrect bundle can lead to unexpected API behavior.
   
   ```js
S
shawn_he 已提交
103
   import Intl from '@ohos.intl';
S
shawn_he 已提交
104 105 106
   ```

2. Instantiate a **DateTimeFormat** object.
S
shawn_he 已提交
107

108
   Use the default constructor of **DateTimeFormat** to obtain the system default locale by accessing the system language and region settings, and set it as the locale in the **DateTimeFormat** object.
H
HelloCrease 已提交
109

S
shawn_he 已提交
110
   ```js
S
shawn_he 已提交
111
   let dateTimeFormat = new Intl.DateTimeFormat();
H
HelloCrease 已提交
112 113
   ```

S
shawn_he 已提交
114
     Alternatively, use your own locale and formatting parameters to create a **DateTimeFormat** object. Formatting parameters are optional. For a full list of formatting parameters, see [DateTimeOptions](../reference/apis/js-apis-intl.md#datetimeoptions).
H
HelloCrease 已提交
115
   
S
shawn_he 已提交
116
   ```js
S
shawn_he 已提交
117
   let options = {dateStyle: "full", timeStyle: "full"};
S
shawn_he 已提交
118
   let dateTimeFormat = new Intl.DateTimeFormat("zh-CN", options);
H
HelloCrease 已提交
119 120
   ```

S
shawn_he 已提交
121
3. Format the date and time.
S
shawn_he 已提交
122

S
shawn_he 已提交
123 124
     Call **format** to format a **Date** object. A string is returned as the formatting result.
     
S
shawn_he 已提交
125
   ```js
S
shawn_he 已提交
126 127 128 129
   let options = {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 = "January 12, 2023, Thursday, 12:12:12 pm, China Standard Time"
H
HelloCrease 已提交
130 131
   ```

S
shawn_he 已提交
132
4. Format a period.
S
shawn_he 已提交
133

S
shawn_he 已提交
134 135
     Call **formatRange** to format a period. This API requires the input of two **Date** objects, which respectively indicate the start date and end date of a period. A string is returned as the formatting result.
     
S
shawn_he 已提交
136
   ```js
S
shawn_he 已提交
137 138 139
   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");
S
shawn_he 已提交
140
   let formatRangeResult = datefmt.formatRange(startDate, endDate); // formatRangeResult = "17/12/2021-18/12/2021"
H
HelloCrease 已提交
141 142
   ```

S
shawn_he 已提交
143
5. Access the attributes of the **DateTimeFormat** object.
S
shawn_he 已提交
144

S
shawn_he 已提交
145 146
     Call **resolvedOptions** to obtain an object that contains all related attributes and values of the **DateTimeFormat** object.
     
S
shawn_he 已提交
147
   ```js
S
shawn_he 已提交
148 149 150
   let options = {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"}
H
HelloCrease 已提交
151 152
   ```

S
shawn_he 已提交
153
## Number Formatting
H
HelloCrease 已提交
154

S
shawn_he 已提交
155
[NumberFormat](../reference/apis/js-apis-intl.md#numberformat) provides APIs to implement the number formatting specific to a locale.
H
HelloCrease 已提交
156 157 158

### Available APIs

S
shawn_he 已提交
159
| Class| API| Description|
H
HelloCrease 已提交
160
| -------- | -------- | -------- |
S
shawn_he 已提交
161 162 163 164
| NumberFormat | constructor()<SUP>8+</SUP> | Creates a **NumberFormat** object for the specified locale.|
| NumberFormat | constructor(locale:string\|Array&lt;string&gt;,options?:NumberOptions) | Creates a **NumberFormat** object and sets the locale and other formatting-related attributes.|
| NumberFormat | format(number:number):string | Calculates the number based on the locale and other formatting-related attributes of the **NumberFormat** object.|
| NumberFormat | resolvedOptions():NumberOptions | Obtains the attributes of the **NumberFormat** object.|
H
HelloCrease 已提交
165 166 167

### How to Develop

S
shawn_he 已提交
168
1. Import the **intl** module.
H
HelloCrease 已提交
169

S
shawn_he 已提交
170
   Importing an incorrect bundle can lead to unexpected API behavior.
H
HelloCrease 已提交
171
   
S
shawn_he 已提交
172
   ```js
S
shawn_he 已提交
173
   import Intl from '@ohos.intl';
S
shawn_he 已提交
174 175 176 177 178 179 180 181
   ```

2. Instantiate a **NumberFormat** object.

   Use the default constructor of **NumberFormat** to obtain the system default locale by accessing the system language and region settings and set it as the locale in the **NumberFormat** object (**intl** is the name of the imported module).

   ```js
   let numberFormat = new Intl.NumberFormat();
H
HelloCrease 已提交
182 183
   ```

S
shawn_he 已提交
184
     Alternatively, use your own locale and formatting parameters to create a **NumberFormat** object. Formatting parameters are optional. For a full list of formatting parameters, see [NumberOptions](../reference/apis/js-apis-intl.md#numberoptions).
H
HelloCrease 已提交
185
   
S
shawn_he 已提交
186
   ```js
S
shawn_he 已提交
187
   let options = {compactDisplay: "short", notation: "compact"};
S
shawn_he 已提交
188
   let numberFormat = new Intl.NumberFormat("zh-CN", options);
H
HelloCrease 已提交
189 190
   ```

S
shawn_he 已提交
191
3. Format a number.
S
shawn_he 已提交
192

S
shawn_he 已提交
193 194
     Call **format** to format a number. A string is returned as the formatting result.
     
S
shawn_he 已提交
195
   ```js
S
shawn_he 已提交
196 197
   let options = {compactDisplay: "short", notation: "compact"};
   let numberFormat = new Intl.NumberFormat("zh-CN", options);
S
shawn_he 已提交
198
   let number = 1234.5678;
S
shawn_he 已提交
199
   let formatResult = numberFormat.format(number); // formatResult = "1235"
H
HelloCrease 已提交
200 201
   ```

S
shawn_he 已提交
202
4. Access the attributes of the **NumberFormat** object.
S
shawn_he 已提交
203

S
shawn_he 已提交
204 205
     Call **resolvedOptions** to obtain an object that contains all related attributes and values of the **NumberFormat** object.
     
S
shawn_he 已提交
206
   ```js
S
shawn_he 已提交
207 208 209
   let options = {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"}
H
HelloCrease 已提交
210 211
   ```

S
shawn_he 已提交
212
## String Sorting
H
HelloCrease 已提交
213

S
shawn_he 已提交
214
Users in different regions have different requirements for string sorting. [Collator](../reference/apis/js-apis-intl.md#collator8) provides APIs to sort character strings specific to a locale.
H
HelloCrease 已提交
215 216 217

### Available APIs

S
shawn_he 已提交
218
| Class| API| Description|
H
HelloCrease 已提交
219
| -------- | -------- | -------- |
S
shawn_he 已提交
220 221 222 223
| Collator | constructor()<sup>8+</sup> | Creates a **Collator** object.|
| Collator | constructor(locale:string\|Array&lt;string&gt;,options?:CollatorOptions)<sup>8+</sup> | Creates a **Collator** object and sets the locale and other related attributes.|
| Collator | compare(first:string,second:string):number<sup>8+</sup> | Calculates the comparison result of two strings based on the locale and other attributes of the **Collator** object.|
| Collator | resolvedOptions():CollatorOptions<sup>8+</sup> | Obtains the attributes of the **Collator** object.|
H
HelloCrease 已提交
224 225 226

### How to Develop

S
shawn_he 已提交
227
1. Import the **intl** module.
H
HelloCrease 已提交
228

S
shawn_he 已提交
229
   Importing an incorrect bundle can lead to unexpected API behavior.
H
HelloCrease 已提交
230
   
S
shawn_he 已提交
231
   ```js
S
shawn_he 已提交
232
   import Intl from '@ohos.intl';
H
HelloCrease 已提交
233 234
   ```

S
shawn_he 已提交
235 236 237 238 239 240 241 242
2. Instantiate a **Collator** object.

   Use the default constructor of **Collator** to obtain the system default locale by accessing the system language and region settings and set it as the locale in the **Collator** object (**intl** is the name of the imported module).

   ```js
   let collator = new Intl.Collator();
   ```

S
shawn_he 已提交
243
     Alternatively, use your own locale and formatting parameters to create a **Collator** object. For a full list of parameters, see [CollatorOptions](../reference/apis/js-apis-intl.md#collatoroptions9).
S
shawn_he 已提交
244
     The **sensitivity** parameter is used to specify the levels of differences that will be used for string comparison. The value **base** indicates that only characters are compared, but not the accent and capitalization. For example, 'a' != 'b', 'a' == '', 'a'=='A'. The value **accent** indicates that the accent is considered, but not the capitalization. For example, 'a' != 'b', 'a' == '', 'a'=='A'. The value **case** indicates that the capitalization is considered, but the accent. For example, 'a' != 'b', 'a' == '', 'a'=='A'. The value **variant** indicates that the accent and capitalization are considered. For example, 'a' != 'b', 'a' == '', 'a'=='A'.
H
HelloCrease 已提交
245
   
S
shawn_he 已提交
246
   ```js
S
shawn_he 已提交
247
   let collator= new Intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort", sensitivity: "case"});
H
HelloCrease 已提交
248 249
   ```

S
shawn_he 已提交
250
3. Compare two strings.
S
shawn_he 已提交
251

S
shawn_he 已提交
252 253
     Call **compare** to compare two input strings. This API returns a value as the comparison result. The return value **-1** indicates that the first string is shorter than the second string, the return value **1** indicates that the first string is longer than the second string, and the return value **0** indicates that the two strings are of equal lengths. This allows you to sort character strings based on the comparison result.
     
S
shawn_he 已提交
254
   ```js
S
shawn_he 已提交
255
   let collator= new Intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort", sensitivity: "case"});
S
shawn_he 已提交
256 257
   let str1 = "first string";
   let str2 = "second string";
S
shawn_he 已提交
258 259 260 261
   let compareResult = collator.compare(str1, str2); // compareResult = -1
   str1 = "first";
   str2 = "First";
   compareResult = collator.compare(str1, str2); // compareResult = -1
H
HelloCrease 已提交
262 263
   ```

S
shawn_he 已提交
264
4. Access the attributes of the **Collator** object.
S
shawn_he 已提交
265

S
shawn_he 已提交
266 267
     Call **resolvedOptions** to obtain an object that contains all related attributes and values of the **Collator** object.
     
S
shawn_he 已提交
268
   ```js
S
shawn_he 已提交
269 270
   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"}
H
HelloCrease 已提交
271 272 273 274
   ```

## Determining the Singular-Plural Type

S
shawn_he 已提交
275
According to grammars in certain languages, the singular or plural form of a noun depends on the number prior to the noun. [PluralRules](../reference/apis/js-apis-intl.md#pluralrules8) provides APIs to determine the singular-plural type for a specific locale.
H
HelloCrease 已提交
276 277 278

### Available APIs

S
shawn_he 已提交
279
| Class| API| Description|
H
HelloCrease 已提交
280
| -------- | -------- | -------- |
S
shawn_he 已提交
281 282 283
| PluralRules | constructor()<sup>8+</sup> | Creates a **PluralRules** object.|
| PluralRules | constructor(locale:string\|Array&lt;string&gt;,options?:PluralRulesOptions)<sup>8+</sup> | Creates a **PluralRules** object and sets the locale and other related attributes.|
| PluralRules | select(n:number):string<sup>8+</sup> | Determines the singular-plural type based on the locale and other formatting-related attributes of the **PluralRules** object.|
H
HelloCrease 已提交
284 285 286 287


### How to Develop

S
shawn_he 已提交
288
1. Import the **intl** module.
H
HelloCrease 已提交
289

S
shawn_he 已提交
290
   Importing an incorrect bundle can lead to unexpected API behavior.
H
HelloCrease 已提交
291
   
S
shawn_he 已提交
292
   ```js
S
shawn_he 已提交
293
   import Intl from '@ohos.intl';
H
HelloCrease 已提交
294 295
   ```

S
shawn_he 已提交
296 297 298 299
2. Instantiate a **PluralRules** object.

   Use the default constructor of **PluralRules** to obtain the system default locale by accessing the system language and region settings and set it as the locale in the **PluralRules** object (**intl** is the name of the imported module).

S
shawn_he 已提交
300
   ```js
S
shawn_he 已提交
301
   let pluralRules = new Intl.PluralRules();
H
HelloCrease 已提交
302 303
   ```

S
shawn_he 已提交
304
     Alternatively, use your own locale and formatting parameters to create a **PluralRules** object. For a full list of parameters, see [PluralRulesOptions](../reference/apis/js-apis-intl.md#pluralrulesoptions9).
G
ge-yafang 已提交
305
   
S
shawn_he 已提交
306
   ```js
S
shawn_he 已提交
307
   let pluralRules = new Intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"});
H
HelloCrease 已提交
308 309
   ```

S
shawn_he 已提交
310
3. Determine the singular-plural type.
H
HelloCrease 已提交
311

S
shawn_he 已提交
312 313 314 315
     Call **select** to determine the singular-plural type for an input number. This API returns a string as the category of the input number, which can be any of the following: **zero**, **one**, **two**, **few**, **many**, and **other**.
     
   ```js
   let pluralRules = new Intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"});
S
shawn_he 已提交
316
   let number = 1234.5678;
S
shawn_he 已提交
317 318
   let categoryResult = pluralRules.select(number); // categoryResult = "other"
   ```
H
HelloCrease 已提交
319

S
shawn_he 已提交
320
## Formatting Relative Time
H
HelloCrease 已提交
321

S
shawn_he 已提交
322
[RelativeTimeFormat](../reference/apis/js-apis-intl.md#relativetimeformat8) provides APIs to format the relative time for a specific locale.
H
HelloCrease 已提交
323 324 325

### Available APIs

S
shawn_he 已提交
326
| Class| API| Description|
H
HelloCrease 已提交
327
| -------- | -------- | -------- |
S
shawn_he 已提交
328 329 330 331 332
| RelativeTimeFormat | constructor()<sup>8+</sup> | Creates a **RelativeTimeFormat** object.|
| RelativeTimeFormat | constructor(locale:string\|Array&lt;string&gt;,options?:RelativeTimeFormatInputOptions)<sup>8+</sup> | Creates a **RelativeTimeFormat** object and sets the locale and other formatting-related attributes.|
| RelativeTimeFormat | format(value:number,unit:string):string<sup>8+</sup> | Calculates the relative time format based on the locale and other formatting-related attributes of the **RelativeTimeFormat** object.|
| RelativeTimeFormat | formatToParts(value:number,unit:string):Array&lt;object&gt;<sup>8+</sup> | Obtains each part of the relative time format based on the locale and other formatting-related attributes of the **RelativeTimeFormat** object.|
| RelativeTimeFormat | resolvedOptions():RelativeTimeFormatResolvedOptions<sup>8+</sup> | Obtains the attributes of the **RelativeTimeFormat** object.|
H
HelloCrease 已提交
333 334 335

### How to Develop

S
shawn_he 已提交
336 337 338 339 340
1. Import the **intl** module.

   Importing an incorrect bundle can lead to unexpected API behavior.
   
   ```js
S
shawn_he 已提交
341
   import Intl from '@ohos.intl';
S
shawn_he 已提交
342
   ```
S
shawn_he 已提交
343

S
shawn_he 已提交
344
2. Instantiate a **RelativeTimeFormat** object.
H
HelloCrease 已提交
345

S
shawn_he 已提交
346
   Use the default constructor of **RelativeTimeFormat** to obtain the system default locale by accessing the system language and region settings and set it as the locale in the **RelativeTimeFormat** object (**intl** is the name of the imported module).
H
HelloCrease 已提交
347
   
S
shawn_he 已提交
348
   ```js
S
shawn_he 已提交
349
   let relativeTimeFormat = new Intl.RelativeTimeFormat();
H
HelloCrease 已提交
350 351
   ```

S
shawn_he 已提交
352
     Alternatively, use your own locale and formatting parameters to create a **RelativeTimeFormat** object. Formatting parameters are optional. For a full list of formatting parameters, see [RelativeTimeFormatInputOptions](../reference/apis/js-apis-intl.md#relativetimeformatinputoptions9).
H
HelloCrease 已提交
353
   
S
shawn_he 已提交
354
   ```js
S
shawn_he 已提交
355
   let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"});
H
HelloCrease 已提交
356 357
   ```

S
shawn_he 已提交
358
3. Format the relative time.
S
shawn_he 已提交
359

S
shawn_he 已提交
360 361
     Call **format** to format the relative time. This API receives a numeric value representing the time length and a string-form unit, like **year**, **quarter**, **month**, **week**, **day**, **hour**, **minute**, and **second**. A string is returned as the formatting result.
     
S
shawn_he 已提交
362
   ```js
S
shawn_he 已提交
363
   let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"});
S
shawn_he 已提交
364
   let number = 2;
S
shawn_he 已提交
365
   let unit = "year";
S
shawn_he 已提交
366
   let formatResult = relativeTimeFormat.format(number, unit); // 2 years later
H
HelloCrease 已提交
367 368
   ```

S
shawn_he 已提交
369
4. Obtain each part of the relative time format.
S
shawn_he 已提交
370

S
shawn_he 已提交
371 372
     On obtaining each part of the relative time format, customize the relative time formatting result.
     
S
shawn_he 已提交
373
   ```js
S
shawn_he 已提交
374
   let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"});
S
shawn_he 已提交
375
   let number = 2;
S
shawn_he 已提交
376
   let unit = "year";
S
shawn_he 已提交
377
   let formatPartsResult = relativeTimeFormat.formatToParts(number, unit); // formatPartsResult = [{"type": "integer", "value": "2", "unit": "year"}, {"type":"literal", "value": "years later"}]
H
HelloCrease 已提交
378 379
   ```

S
shawn_he 已提交
380
5. Access the attributes of the **RelativeTimeFormat** object.
S
shawn_he 已提交
381

S
shawn_he 已提交
382 383
     Call **resolvedOptions** to obtain an object that contains all related attributes and values of the **RelativeTimeFormat** object. For a full list of attributes, see [RelativeTimeFormatResolvedOptions](../reference/apis/js-apis-intl.md#relativetimeformatresolvedoptions8).
     
S
shawn_he 已提交
384
   ```js
S
shawn_he 已提交
385 386
   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"}
H
HelloCrease 已提交
387
   ```
S
shawn_he 已提交
388 389 390 391 392

## Samples

The following sample is provided to help you better understand how to develop internationalization capabilities:

S
shawn_he 已提交
393
-[`International`: Internationalization (ArkTS) (API9) (Full SDK)] (https://gitee.com/openharmony/applications_app_samples/tree/master/code/SystemFeature/Internationalnation/International)