@@ -4,12 +4,12 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
...
@@ -4,12 +4,12 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
## Setting Locale Information
## Setting Locale Information
[Locale](../reference/apis/js-apis-intl.md) APIs are used to maximize or minimize locale information.
Use [Locale](../reference/apis/js-apis-intl.md) APIs to maximize or minimize locale information.
### Available APIs
### Available APIs
| Module | API | Description |
| Module | API | Description |
| -------- | -------- | -------- |
| -------- | -------- | -------- |
| ohos.intl | constructor()<sup>8+</sup> | Instantiates a **Locale** object. |
| ohos.intl | constructor()<sup>8+</sup> | Instantiates a **Locale** object. |
| ohos.intl | constructor(locale?: string, options?: options) | Instantiates a **Locale** object based on the locale parameter and options. |
| ohos.intl | constructor(locale?: string, options?: options) | Instantiates a **Locale** object based on the locale parameter and options. |
...
@@ -20,8 +20,8 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
...
@@ -20,8 +20,8 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
### How to Develop
### How to Develop
1. Instantiate a **Locale** object.
1. Instantiate a **Locale** object.<br>
Create a **Locale** object using the **Locale** constructor. This method receives a string representing the locale and an optional [Attributes](../reference/apis/js-apis-intl.md) list (**intl** is the name of the imported module).
Create a **Locale** object by using the **Locale** constructor. This method receives a string representing the locale and an optional [Attributes](../reference/apis/js-apis-intl.md) list. In the code below, **intl** is the name of the imported module.
```
```
...
@@ -30,22 +30,22 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
...
@@ -30,22 +30,22 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
var localeObj = new intl.Locale(locale, options);
var localeObj = new intl.Locale(locale, options);
```
```
2. Obtain the string representing a **Locale** object.
2. Obtain the string representing a **Locale** object.<br>
Call the **toString** method to obtain the string representing a **Locale** object, including the language, region, and other options.
Call the **toString** method to obtain the string representing a **Locale** object, which includes the language, region, and other options.
```
```
var localeStr = localeObj.toString();
var localeStr = localeObj.toString();
```
```
3. Maximize locale information.
3. Maximize locale information.<br>
Call the **maximize** method to maximize locale information; that is, supplement the missing script and region information.
Call the **maximize** method to maximize locale information; that is, supplement the missing script and region information.
```
```
var maximizedLocale = localeObj.maximize();
var maximizedLocale = localeObj.maximize();
```
```
4. Minimize locale information.
4. Minimize locale information.<br>
Call the **minimize** method to minimize locale information; that is, delete the unnecessary script and region information.
Call the **minimize** method to minimize locale information; that is, delete the unnecessary script and region information.
```
```
var minimizedLocale = localeObj.minimize();
var minimizedLocale = localeObj.minimize();
...
@@ -54,12 +54,12 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
...
@@ -54,12 +54,12 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
## Formatting the Date and Time
## Formatting the Date and Time
[DateTimeFormat](../reference/apis/js-apis-intl.md) APIs are used to format the date and time for a specific locale.
Use [DateTimeFormat](../reference/apis/js-apis-intl.md) APIs to format the date and time for a specific locale.
### Available APIs
### Available APIs
| Module | API | Description |
| Module | API | Description |
| -------- | -------- | -------- |
| -------- | -------- | -------- |
| ohos.intl | constructor()<sup>8+</sup> | Creates a **DateTimeFormat** object. |
| ohos.intl | constructor()<sup>8+</sup> | Creates a **DateTimeFormat** object. |
| ohos.intl | constructor(locale: string \| Array<string>, options?: DateTimeOptions) | Creates a **DateTimeFormat** object and sets the locale and other formatting-related attributes. |
| ohos.intl | constructor(locale: string \| Array<string>, options?: DateTimeOptions) | Creates a **DateTimeFormat** object and sets the locale and other formatting-related attributes. |
...
@@ -70,31 +70,31 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
...
@@ -70,31 +70,31 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
### How to Develop
### How to Develop
1. Instantiate a **DateTimeFormat** object.
1. Instantiate a **DateTimeFormat** object.<br>
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 (**intl** is the name of the imported module).
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. In the code below, **intl** is the name of the imported module.
```
```
var dateTimeFormat = new intl.DateTimeFormat();
var dateTimeFormat = new intl.DateTimeFormat();
```
```
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).
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).
```
```
var options = {dateStyle: "full", timeStyle: "full"};
var options = {dateStyle: "full", timeStyle: "full"};
var dateTimeFormat = new intl.DateTimeFormat("zh-CN", options);
var dateTimeFormat = new intl.DateTimeFormat("zh-CN", options);
```
```
2. Format the date and time.
2. Format the date and time.<br>
Use the **format** method of **DateTimeFormat** to format a **Date** object. A string is returned as the formatting result.
Use the **format** method of **DateTimeFormat** to format a **Date** object. This method returns a string representing the formatting result.
```
```
Date date = new Date();
Date date = new Date();
var formatResult = dateTimeFormat.format(date);
var formatResult = dateTimeFormat.format(date);
```
```
3. Format a period.
3. Format a period.<br>
Use the **formatRange** method of **DateTimeFormat** to format a period. This method 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.
Use the **formatRange** method of **DateTimeFormat** to format a period. This method requires input of two **Date** objects, which respectively indicate the start date and end date of a period. This method returns a string representing the formatting result.
```
```
Date startDate = new Date();
Date startDate = new Date();
...
@@ -102,8 +102,8 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
...
@@ -102,8 +102,8 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
var formatResult = dateTimeFormat.formatRange(startDate, endDate);
var formatResult = dateTimeFormat.formatRange(startDate, endDate);
```
```
4.Access the attributes of the **DateTimeFormat** object.
4.Obtain attributes of the **DateTimeFormat** object.<br>
The **resolvedOptions** method of **DateTimeFormat** returns an object that contains all related attributes and values of the **DateTimeFormat** object.
The **resolvedOptions** method of **DateTimeFormat** returns an object that contains all related attributes and values of the **DateTimeFormat** object.
```
```
var options = dateTimeFormat.resolvedOptions();
var options = dateTimeFormat.resolvedOptions();
...
@@ -112,12 +112,12 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
...
@@ -112,12 +112,12 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
## Number Formatting
## Number Formatting
[NumberFormat](../reference/apis/js-apis-intl.md) APIs are used to format a number for a specific locale.
Use [NumberFormat](../reference/apis/js-apis-intl.md) APIs to format a number for a specific locale.
### Available APIs
### Available APIs
| Module | API | Description |
| Module | API | Description |
| -------- | -------- | -------- |
| -------- | -------- | -------- |
| ohos.intl | constructor()<sup>8+</sup> | Creates a **NumberFormat** object. |
| ohos.intl | constructor()<sup>8+</sup> | Creates a **NumberFormat** object. |
| ohos.intl | constructor(locale: string \| Array<string>, options?: NumberOptions) | Creates a **NumberFormat** object and sets the locale and other formatting-related attributes. |
| ohos.intl | constructor(locale: string \| Array<string>, options?: NumberOptions) | Creates a **NumberFormat** object and sets the locale and other formatting-related attributes. |
...
@@ -127,31 +127,31 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
...
@@ -127,31 +127,31 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
### How to Develop
### How to Develop
1. Instantiate a **NumberFormat** object.
1. Instantiate a **NumberFormat** object.<br>
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).
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. In the code below, **intl** is the name of the imported module.
```
```
var numberFormat = new intl.NumberFormat();
var numberFormat = new intl.NumberFormat();
```
```
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).
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).
```
```
var options = {compactDisplay: "short", notation: "compact"};
var options = {compactDisplay: "short", notation: "compact"};
var numberFormat = new intl.NumberFormat("zh-CN", options);
var numberFormat = new intl.NumberFormat("zh-CN", options);
```
```
2. Format a number.
2. Format a number.<br>
Use the **format** method of **NumberFormat** to format a number. A string is returned as the formatting result.
Use the **format** method of **NumberFormat** to format a number. A string is returned as the formatting result.
```
```
var number = 1234.5678
var number = 1234.5678
var formatResult = numberFormat.format(number);
var formatResult = numberFormat.format(number);
```
```
3.Access the attributes of the **NumberFormat** object.
3.Obtain attributes of the **NumberFormat** object.<br>
The **resolvedOptions** method of NumberFormat returns an object that contains all related attributes and values of the **NumberFormat** object.
The **resolvedOptions** method of NumberFormat returns an object that contains all related attributes and values of the **NumberFormat** object.
```
```
var options = numberFormat.resolvedOptions();
var options = numberFormat.resolvedOptions();
...
@@ -160,12 +160,12 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
...
@@ -160,12 +160,12 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4
## String Sorting
## String Sorting
Users in different regions have different requirements for string sorting. [Collator](../reference/apis/js-apis-intl.md) APIs are used to sort strings based on a specific locale.
Use[Collator](../reference/apis/js-apis-intl.md) APIs to sort strings based on a specific locale. Users in different regions have different preferences for string sorting.
### Available APIs
### Available APIs
| Module | API | Description |
| Module | API | Description |
| -------- | -------- | -------- |
| -------- | -------- | -------- |
| ohos.intl | constructor()<sup>8+</sup> | Creates a **Collator** object. |
| ohos.intl | constructor()<sup>8+</sup> | Creates a **Collator** object. |
| ohos.intl | constructor(locale: string \| Array<string>, options?: CollatorOptions)<sup>8+</sup> | Creates a **Collator** object and sets the locale and other related attributes. |
| ohos.intl | constructor(locale: string \| Array<string>, options?: CollatorOptions)<sup>8+</sup> | Creates a **Collator** object and sets the locale and other related attributes. |
...
@@ -175,22 +175,22 @@ Users in different regions have different requirements for string sorting. [Coll
...
@@ -175,22 +175,22 @@ Users in different regions have different requirements for string sorting. [Coll
### How to Develop
### How to Develop
1. Instantiate a **Collator** object.
1. Instantiate a **Collator** object.<br>
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).
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. In the code below, **intl** is the name of the imported module.
```
```
var collator = new intl.Collator();
var collator = new intl.Collator();
```
```
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).
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).
```
```
var collator= new intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort"};
var collator= new intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort"};
```
```
2. Compare two strings.
2. Compare two strings.<br>
Use the **compare** method of **Collator** to compare two input strings. This method 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.
Use the **compare** method of **Collator** to compare two input strings. This method 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.
```
```
var str1 = "first string";
var str1 = "first string";
...
@@ -198,8 +198,8 @@ Users in different regions have different requirements for string sorting. [Coll
...
@@ -198,8 +198,8 @@ Users in different regions have different requirements for string sorting. [Coll
var compareResult = collator.compare(str1, str2);
var compareResult = collator.compare(str1, str2);
```
```
3.Access the attributes of the **Collator** object.
3.Obtain attributes of the **Collator** object.<br>
The **resolvedOptions** method of **Collator** returns an object that contains all related attributes and values of the **Collator** object.
The **resolvedOptions** method of **Collator** returns an object that contains all related attributes and values of the **Collator** object.
```
```
var options = collator.resolvedOptions();
var options = collator.resolvedOptions();
...
@@ -208,12 +208,12 @@ Users in different regions have different requirements for string sorting. [Coll
...
@@ -208,12 +208,12 @@ Users in different regions have different requirements for string sorting. [Coll
## Determining the Singular-Plural Type
## Determining the Singular-Plural Type
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) APIs are used to determine the singular-plural type for a specific locale.
Use [PluralRules](../reference/apis/js-apis-intl.md) APIs to determine the singular-plural type for a specific locale. According to the grammar of certain languages, the singular or plural form of a noun depends on its preceding number.
### Available APIs
### Available APIs
| Module | API | Description |
| Module | API | Description |
| -------- | -------- | -------- |
| -------- | -------- | -------- |
| ohos.intl | constructor()<sup>8+</sup> | Creates a **PluralRules** object. |
| ohos.intl | constructor()<sup>8+</sup> | Creates a **PluralRules** object. |
| ohos.intl | constructor(locale: string \| Array<string>, options?: PluralRulesOptions)<sup>8+</sup> | Creates a **PluralRules** object and sets the locale and other related attributes. |
| ohos.intl | constructor(locale: string \| Array<string>, options?: PluralRulesOptions)<sup>8+</sup> | Creates a **PluralRules** object and sets the locale and other related attributes. |
...
@@ -222,22 +222,22 @@ According to grammars in certain languages, the singular or plural form of a nou
...
@@ -222,22 +222,22 @@ According to grammars in certain languages, the singular or plural form of a nou
### How to Develop
### How to Develop
1. Instantiate a **PluralRules** object.
1. Instantiate a **PluralRules** object.<br>
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).
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. In the code below, **intl** is the name of the imported module.
```
```
var pluralRules = new intl.PluralRules();
var pluralRules = new intl.PluralRules();
```
```
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).
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).
```
```
var plurals = new intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"};
var plurals = new intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"};
```
```
2. Determine the singular or plural category.
2. Determine the singular or plural category.<br>
Use the **select** method of **PluralRules** to determine the singular-plural type for an input number. This method returns a string as the category of the input number, which can be any of the following: **zero**, **one**, **two**, **few**, **many**, and **other**.
Use the **select** method of **PluralRules** to determine the singular-plural type for an input number. This method returns a string as the category of the input number, which can be any of the following: **zero**, **one**, **two**, **few**, **many**, and **other**.
```
```
var number = 1234.5678
var number = 1234.5678
...
@@ -247,12 +247,12 @@ According to grammars in certain languages, the singular or plural form of a nou
...
@@ -247,12 +247,12 @@ According to grammars in certain languages, the singular or plural form of a nou
## Formatting Relative Time
## Formatting Relative Time
[RelativeTimeFormat](../reference/apis/js-apis-intl.md) APIs are used to format the relative time for a specific locale.
Use [RelativeTimeFormat](../reference/apis/js-apis-intl.md) APIs to format the relative time for a specific locale.
### Available APIs
### Available APIs
| Module | API | Description |
| Module | API | Description |
| -------- | -------- | -------- |
| -------- | -------- | -------- |
| ohos.intl | constructor()<sup>8+</sup> | Creates a **RelativeTimeFormat** object. |
| ohos.intl | constructor()<sup>8+</sup> | Creates a **RelativeTimeFormat** object. |
| ohos.intl | constructor(locale: string \| Array<string>, options?: RelativeTimeFormatInputOptions)<sup>8+</sup> | Creates a **RelativeTimeFormat** object and sets the locale and other formatting-related attributes. |
| ohos.intl | constructor(locale: string \| Array<string>, options?: RelativeTimeFormatInputOptions)<sup>8+</sup> | Creates a **RelativeTimeFormat** object and sets the locale and other formatting-related attributes. |
...
@@ -263,22 +263,22 @@ According to grammars in certain languages, the singular or plural form of a nou
...
@@ -263,22 +263,22 @@ According to grammars in certain languages, the singular or plural form of a nou
### How to Develop
### How to Develop
1. Instantiate a **RelativeTimeFormat** object.
1. Instantiate a **RelativeTimeFormat** object.<br>
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).
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. In the code below, **intl** is the name of the imported module.
```
```
var relativeTimeFormat = new intl.RelativeTimeFormat();
var relativeTimeFormat = new intl.RelativeTimeFormat();
```
```
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).
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).
```
```
var relativeTimeFormat = new intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"};
var relativeTimeFormat = new intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"};
```
```
2. Format the relative time.
2. Format the relative time.<br>
Use the **format** method of **RelativeTimeFormat** to format the relative time. This method 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.
Use the **format** method of **RelativeTimeFormat** to format the relative time. This method receives a numeric value representing the time length and a string-form unit, like **year**, **quarter**, **month**, **week**, **day**, **hour**, **minute**, and **second**. This method returns a string representing the formatting result.
```
```
var number = 2;
var number = 2;
...
@@ -286,8 +286,8 @@ According to grammars in certain languages, the singular or plural form of a nou
...
@@ -286,8 +286,8 @@ According to grammars in certain languages, the singular or plural form of a nou
var formatResult = relativeTimeFormat.format(number, unit);
var formatResult = relativeTimeFormat.format(number, unit);
```
```
3. Obtain each part of the relative time format.
3. Obtain each part of the relative time format.<br>
On obtaining each part of the relative time format, customize the relative time formatting result.
Upon obtaining each part of the relative time format, customize the relative time formatting result.
```
```
var number = 2;
var number = 2;
...
@@ -295,8 +295,8 @@ According to grammars in certain languages, the singular or plural form of a nou
...
@@ -295,8 +295,8 @@ According to grammars in certain languages, the singular or plural form of a nou
var formatResult = relativeTimeFormat.formatToParts(number, unit);
var formatResult = relativeTimeFormat.formatToParts(number, unit);
```
```
4.Access the attributes of the **RelativeTimeFormat** object.
4.Obtain attributes of the **RelativeTimeFormat** object.<br>
The **resolvedOptions** method of **RelativeTimeFormat** returns 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).
The **resolvedOptions** method of **RelativeTimeFormat** returns 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).
```
```
var options = numberFormat.resolvedOptions();
var options = numberFormat.resolvedOptions();
...
@@ -306,4 +306,4 @@ According to grammars in certain languages, the singular or plural form of a nou
...
@@ -306,4 +306,4 @@ According to grammars in certain languages, the singular or plural form of a nou
The following sample is provided to help you better understand how to develop internationalization capabilities:
The following sample is provided to help you better understand how to develop internationalization capabilities: