未验证 提交 91db6f8a 编写于 作者: O openharmony_ci 提交者: Gitee

!9366 翻译已完成8932+8855+8850+8932

Merge pull request !9366 from shawn_he/8855-a
# Internationalization Development (i18n) # Internationalization Development (I18N)
This development guide describes how to use i18n APIs that are not defined in ECMA 402. This module provides system-related or enhanced I18N capabilities, such as locale management, phone number formatting, and calendar, through supplementary I18N APIs that are not defined in ECMA 402. For more details about APIs and their usage, see [I18N](../reference/apis/js-apis-i18n.md).
The [Intl](intl-guidelines.md) module provides basic I18N capabilities through the standard I18N APIs defined in ECMA 402. It works with the I18N module to provide a complete suite of I18N capabilities.
## Obtaining System Language and Region Information ## Obtaining System Language and Region Information
APIs are provided to access the system language and region information. You can use APIs provided in the following table to obtain the system language and region information.
### Available APIs ### Available APIs
| Module | API | Description | | Module | API | Description |
| -------- | -------- | -------- | | -------- | -------- | -------- |
| ohos.i18n | getSystemLanguage(): string | Obtains the system language. | | ohos.i18n | getSystemLanguage(): string | Obtains the system language. |
| ohos.i18n | getSystemRegion(): string | Obtains the system region. | | ohos.i18n | getSystemRegion(): string | Obtains the system region. |
...@@ -17,52 +19,57 @@ APIs are provided to access the system language and region information. ...@@ -17,52 +19,57 @@ APIs are provided to access the system language and region information.
| ohos.i18n | isRTL(locale: string): boolean<sup>7+</sup> | Checks whether the locale uses a right-to-left (RTL) language. | | ohos.i18n | isRTL(locale: string): boolean<sup>7+</sup> | Checks whether the locale uses a right-to-left (RTL) language. |
| ohos.i18n | is24HourClock(): boolean<sup>7+</sup> | Checks whether the system uses a 24-hour clock. | | ohos.i18n | is24HourClock(): boolean<sup>7+</sup> | Checks whether the system uses a 24-hour clock. |
| ohos.i18n | getDisplayLanguage(language: string, locale: string, sentenceCase?: boolean): string | Obtains the localized display of a language. | | ohos.i18n | getDisplayLanguage(language: string, locale: string, sentenceCase?: boolean): string | Obtains the localized display of a language. |
| ohos.i18n | getDisplayCountry(country: string, locale: string, sentenceCase?: boolean): string | Obtains the localized display of a country. | | ohos.i18n | getDisplayCountry(country: string, locale: string, sentenceCase?: boolean): string | Obtains the localized display of a country name. |
### How to Develop ### How to Develop
1. Obtain the system language. 1. Obtain the system language.
Call the **getSystemLanguage** method to obtain the system language (**i18n** is the name of the imported module). Call the **getSystemLanguage** method to obtain the system language (**i18n** is the name of the imported module).
``` ```js
var language = i18n.getSystemLanguage(); var language = i18n.getSystemLanguage();
``` ```
2. Obtains the system region. 2. Obtain the system region.
Call the **getSystemRegion** method to obtain the system region. Call the **getSystemRegion** method to obtain the system region.
``` ```js
var region = i18n.getSystemRegion(); var region = i18n.getSystemRegion();
``` ```
3. Obtain the system locale. 3. Obtain the system locale.
Call the **getSystemLocale** method to obtain the system locale. Call the **getSystemLocale** method to obtain the system locale.
``` ```js
var locale = i18n.getSystemLocale(); var locale = i18n.getSystemLocale();
``` ```
4. Check whether the locale's language is RTL. 4. Check whether the locale's language is RTL.
Call the **isRTL** method to check whether the locale's language is RTL.
Call the **isRTL** method to check whether the locale's language is RTL.
``` ```js
var rtl = i18n.isRTL("zh-CN"); var rtl = i18n.isRTL("zh-CN");
``` ```
5. Check whether the system uses a 24-hour clock. 5. Check whether the system uses a 24-hour clock.
Call the **is24HourClock** method to check whether the system uses a 24-hour clock. Call the **is24HourClock** method to check whether the system uses a 24-hour clock.
``` ```js
var hourClock = i18n.is24HourClock(); var hourClock = i18n.is24HourClock();
``` ```
6. Obtain the localized display of a language. 6. Obtain the localized display of a language.
Call the **getDisplayLanguage** method to obtain the localized display of a language. **language** indicates the language to be localized, **locale** indicates the locale, and **sentenceCase** indicates whether the first letter of the result must be capitalized. Call the **getDisplayLanguage** method to obtain the localized display of a language. **language** indicates the language to be localized, **locale** indicates the locale, and **sentenceCase** indicates whether the first letter of the result must be capitalized.
``` ```js
var language = "en"; var language = "en";
var locale = "zh-CN"; var locale = "zh-CN";
var sentenceCase = false; var sentenceCase = false;
...@@ -70,9 +77,10 @@ APIs are provided to access the system language and region information. ...@@ -70,9 +77,10 @@ APIs are provided to access the system language and region information.
``` ```
7. Obtain the localized display of a country. 7. Obtain the localized display of a country.
Call the **getDisplayCountry** method to obtain the localized display of a country. **country** indicates the country to be localized, **locale** indicates the locale, and **sentenceCase** indicates whether the first letter of the result must be capitalized.
``` Call the **getDisplayCountry** method to obtain the localized display of a country name. **country** indicates the country code (a two-letter code in compliance with ISO-3166, for example, CN), **locale** indicates the locale, and **sentenceCase** indicates whether the first letter of the result must be capitalized.
```js
var country = "US"; var country = "US";
var locale = "zh-CN"; var locale = "zh-CN";
var sentenceCase = false; var sentenceCase = false;
...@@ -82,7 +90,7 @@ APIs are provided to access the system language and region information. ...@@ -82,7 +90,7 @@ APIs are provided to access the system language and region information.
## Obtaining Calendar Information ## Obtaining Calendar Information
[Calendar](../reference/apis/js-apis-intl.md) APIs are used to obtain calendar information, for example, the localized display of the calendar, the first day of a week, and the minimum count of days in the first week of a year. [Calendar](../reference/apis/js-apis-i18n.md#calendar8) APIs are used to obtain calendar information, for example, the localized display of the calendar, the first day of a week, and the minimum count of days in the first week of a year.
### Available APIs ### Available APIs
...@@ -106,17 +114,19 @@ APIs are provided to access the system language and region information. ...@@ -106,17 +114,19 @@ APIs are provided to access the system language and region information.
### How to Develop ### How to Develop
1. Instantiate a **Calendar** object. 1. Instantiate a **Calendar** object.
Call the **getCalendar** method to obtain the time zone object of a specific locale and type (**i18n** is the name of the imported module). **type** indicates the valid calendar type, for example, **buddhist**, **chinese**, **coptic**, **ethiopic**, **hebrew**, **gregory**, **indian**, **islamic_civil**, **islamic_tbla**, **islamic_umalqura**, **japanese**, and **persian**. If **type** is left unspecified, the default calendar type of the locale is used. Call the **getCalendar** method to obtain the time zone object of a specific locale and type (**i18n** is the name of the imported module). **type** indicates the valid calendar type, for example, **buddhist**, **chinese**, **coptic**, **ethiopic**, **hebrew**, **gregory**, **indian**, **islamic_civil**, **islamic_tbla**, **islamic_umalqura**, **japanese**, and **persian**. If **type** is left unspecified, the default calendar type of the locale is used.
``` ```js
var calendar = i18n.getCalendar("zh-CN", "gregory); var calendar = i18n.getCalendar("zh-CN", "gregory");
``` ```
2. Set the time for the **Calendar** object. 2. Set the time for the **Calendar** object.
Call the **setTime** method to set the time of the **Calendar** object. This method receives two types of parameters. One is a **Date** object, and the other is a value indicating the number of milliseconds elapsed since January 1, 1970, 00:00:00 GMT. Call the **setTime** method to set the time of the **Calendar** object. This method receives two types of parameters. One is a **Date** object, and the other is a value indicating the number of milliseconds elapsed since January 1, 1970, 00:00:00 GMT.
``` ```js
var date1 = new Date(); var date1 = new Date();
calendar.setTime(date1); calendar.setTime(date1);
var date2 = 1000; var date2 = 1000;
...@@ -124,51 +134,57 @@ APIs are provided to access the system language and region information. ...@@ -124,51 +134,57 @@ APIs are provided to access the system language and region information.
``` ```
3. Set the year, month, day, hour, minute, and second for the **Calendar** object. 3. Set the year, month, day, hour, minute, and second for the **Calendar** object.
Call the **set** method to set the year, month, day, hour, minute, and second for the **Calendar** object. Call the **set** method to set the year, month, day, hour, minute, and second for the **Calendar** object.
``` ```js
calendar.set(2021, 12, 21, 6, 0, 0) calendar.set(2021, 12, 21, 6, 0, 0)
``` ```
4. Set and obtain the time zone for the **Calendar** object. 4. Set and obtain the time zone for the **Calendar** object.
Call the **setTimeZone** and **getTimeZone** methods to set and obtain the time zone for the **Calendar** object. The **setTimeZone** method requires an input string to indicate the time zone to be set. Call the **setTimeZone** and **getTimeZone** methods to set and obtain the time zone for the **Calendar** object. The **setTimeZone** method requires an input string to indicate the time zone to be set.
``` ```js
calendar.setTimeZone("Asia/Shanghai"); calendar.setTimeZone("Asia/Shanghai");
var timezone = calendar.getTimeZone(); var timezone = calendar.getTimeZone();
``` ```
5. Set and obtain the first day of a week for the **Calendar** object. 5. Set and obtain the first day of a week for the **Calendar** object.
Call the **setFirstDayOfWeek** and **getFirstDayOfWeek** methods to set and obtain the first day of a week for the **Calendar** object. **setFirstDayOfWeek** must be set to a value indicating the first day of a week. The value **1** indicates Sunday, and the value **7** indicates Saturday. Call the **setFirstDayOfWeek** and **getFirstDayOfWeek** methods to set and obtain the first day of a week for the **Calendar** object. **setFirstDayOfWeek** must be set to a value indicating the first day of a week. The value **1** indicates Sunday, and the value **7** indicates Saturday.
``` ```js
calendar.setFirstDayOfWeek(1); calendar.setFirstDayOfWeek(1);
var firstDayOfWeek = calendar.getFirstDayOfWeek(); var firstDayOfWeek = calendar.getFirstDayOfWeek();
``` ```
6. Set and obtain the minimum count of days in the first week for the **Calendar** object. 6. Set and obtain the minimum count of days in the first week for the **Calendar** object.
Call the **setMinimalDaysInFirstWeek** and **getMinimalDaysInFirstWeek** methods to set and obtain the minimum count of days in the first week for the **Calendar** object. Call the **setMinimalDaysInFirstWeek** and **getMinimalDaysInFirstWeek** methods to set and obtain the minimum count of days in the first week for the **Calendar** object.
``` ```js
calendar.setMinimalDaysInFirstWeek(3); calendar.setMinimalDaysInFirstWeek(3);
var minimalDaysInFirstWeek = calendar.getMinimalDaysInFirstWeek(); var minimalDaysInFirstWeek = calendar.getMinimalDaysInFirstWeek();
``` ```
7. Obtain the localized display of the **Calendar** object. 7. Obtain the localized display of the **Calendar** object.
Call the **getDisplayName** method to obtain the localized display of the **Calendar** object. Call the **getDisplayName** method to obtain the localized display of the **Calendar** object.
``` ```js
var localizedName = calendar.getDisplayName("zh-CN"); var localizedName = calendar.getDisplayName("zh-CN");
``` ```
8. Check whether a date is a weekend. 8. Check whether a date is a weekend.
Call the **isWeekend** method to determine whether the input date is a weekend. Call the **isWeekend** method to determine whether the input date is a weekend.
``` ```js
var date = new Date(); var date = new Date();
var weekend = calendar.isWeekend(date); var weekend = calendar.isWeekend(date);
``` ```
...@@ -176,12 +192,12 @@ APIs are provided to access the system language and region information. ...@@ -176,12 +192,12 @@ APIs are provided to access the system language and region information.
## Formatting a Phone Number ## Formatting a Phone Number
[PhoneNumberFormat](../reference/apis/js-apis-intl.md) APIs are used to format phone numbers in different countries and check whether the phone number formats are correct. [PhoneNumberFormat](../reference/apis/js-apis-i18n.md#phonenumberformat8) APIs are used to format phone numbers in different countries and check whether the phone number formats are correct.
### Available APIs ### Available APIs
| Module | API | Description | | Module | API | Description |
| -------- | -------- | -------- | | -------- | -------- | -------- |
| ohos.i18n | constructor(country: string, options?: PhoneNumberFormatOptions)<sup>8+</sup> | Instantiates a **PhoneNumberFormat** object. | | ohos.i18n | constructor(country: string, options?: PhoneNumberFormatOptions)<sup>8+</sup> | Instantiates a **PhoneNumberFormat** object. |
| ohos.i18n | isValidNumber(number: string): boolean<sup>8+</sup> | Checks whether the value of **number** is a phone number in the correct format. | | ohos.i18n | isValidNumber(number: string): boolean<sup>8+</sup> | Checks whether the value of **number** is a phone number in the correct format. |
...@@ -191,47 +207,49 @@ APIs are provided to access the system language and region information. ...@@ -191,47 +207,49 @@ APIs are provided to access the system language and region information.
### How to Develop ### How to Develop
1. Instantiate a **PhoneNumberFormat** object. 1. Instantiate a **PhoneNumberFormat** object.
Call the **PhoneNumberFormat** constructor to instantiate a **PhoneNumberFormat** object. The country code and formatting options of the phone number need to be passed into this constructor. The formatting options are optional, including a style option. Values of this option include: **E164**, **INTERNATIONAL**, **NATIONAL**, and **RFC3966**. Call the **PhoneNumberFormat** constructor to instantiate a **PhoneNumberFormat** object. The country code and formatting options of the phone number need to be passed into this constructor. The formatting options are optional, including a style option. Values of this option include: **E164**, **INTERNATIONAL**, **NATIONAL**, and **RFC3966**.
``` ```js
var phoneNumberFormat = new i18n.PhoneNubmerFormat("CN", {type: "E164"}); var phoneNumberFormat = new i18n.PhoneNumberFormat("CN", {type: "E164"});
``` ```
2. Check whether the phone number format is correct. 2. Check whether the phone number format is correct.
Call the **isValidNumber** method to check whether the format of the input phone number is correct. Call the **isValidNumber** method to check whether the format of the input phone number is correct.
``` ```js
var validNumber = phoneNumberFormat.isValidNumber("15812341234"); var validNumber = phoneNumberFormat.isValidNumber("15812341234");
``` ```
3. Format a phone number. 3. Format a phone number.
Call the **format** method of **PhoneNumberFormat** to format the input phone number. Call the **format** method of **PhoneNumberFormat** to format the input phone number.
``` ```js
var formattedNumber = phoneNumberFormat.format("15812341234"); var formattedNumber = phoneNumberFormat.format("15812341234");
``` ```
## Measurement Conversion ## Measurement Conversion
An API can be called to implement measurement conversion. The **unitConvert** API is provided to help you implement measurement conversion.
### Available APIs ### Available APIs
| Module | API | Description | | Module | API | Description |
| -------- | -------- | -------- | | -------- | -------- | -------- |
| ohos.i18n | unitConvert(fromUnit: UnitInfo, toUnit: UnitInfo, value: number, locale: string, style?: string): string<sup>8+</sup> | Converts one measurement unit (**fromUnit**) into another (**toUnit**) and formats the unit based on the specified locale and style. | | ohos.i18n | unitConvert(fromUnit: UnitInfo, toUnit: UnitInfo, value: number, locale: string, style?: string): string<sup>8+</sup> | Converts one measurement unit (**fromUnit**) into another (**toUnit**) and formats the unit based on the specified locale and style. |
### How to Develop ### How to Develop
1. Convert a measurement unit. Call the [unitConvert](../reference/apis/js-apis-i18n.md#unitconvert8) method to convert a measurement unit and format the display result.
Call the [unitConvert](../reference/apis/js-apis-intl.md) method to convert a measurement unit and format the display result.
``` ```js
var fromUnit = {unit: "cup", measureSystem: "US"}; var fromUnit = {unit: "cup", measureSystem: "US"};
var toUnit = {unit: "liter", measureSystem: "SI"}; var toUnit = {unit: "liter", measureSystem: "SI"};
var number = 1000; var number = 1000;
...@@ -243,7 +261,7 @@ An API can be called to implement measurement conversion. ...@@ -243,7 +261,7 @@ An API can be called to implement measurement conversion.
## Alphabet Index ## Alphabet Index
[IndexUtil](../reference/apis/js-apis-intl.md) APIs are used to obtain the alphabet indexes of different locales and calculate the index to which a string belongs. [IndexUtil](../reference/apis/js-apis-i18n.md#indexutil8) APIs are used to obtain the alphabet indexes of different locales and calculate the index to which a string belongs.
### Available APIs ### Available APIs
...@@ -259,31 +277,35 @@ An API can be called to implement measurement conversion. ...@@ -259,31 +277,35 @@ An API can be called to implement measurement conversion.
### How to Develop ### How to Develop
1. Instantiate an **IndexUtil** object. 1. Instantiate an **IndexUtil** object.
Call the **getInstance** method to instantiate an **IndexUtil** object for a specific locale. When the **locale** parameter is empty, instantiate an **IndexUtil** object of the default locale. Call the **getInstance** method to instantiate an **IndexUtil** object for a specific locale. When the **locale** parameter is empty, instantiate an **IndexUtil** object of the default locale.
``` ```js
var indexUtil = getInstance("zh-CN"); var indexUtil = i18n.getInstance("zh-CN");
``` ```
2. Obtain the index list. 2. Obtain the index list.
Call the **getIndexList** method to obtain the alphabet index list of the current locale. Call the **getIndexList** method to obtain the alphabet index list of the current locale.
``` ```js
var indexList = indexUtil.getIndexList(); var indexList = indexUtil.getIndexList();
``` ```
3. Add an index. 3. Add an index.
Call the **addLocale** method to add the alphabet index of a new locale to the current index list. Call the **addLocale** method to add the alphabet index of a new locale to the current index list.
``` ```js
indexUtil.addLocale("ar") indexUtil.addLocale("ar")
``` ```
4. Obtain the index of a string. 4. Obtain the index of a string.
Call the **getIndex** method to obtain the alphabet index of a string. Call the **getIndex** method to obtain the alphabet index of a string.
``` ```js
var text = "access index"; var text = "access index";
indexUtil.getIndex(text); indexUtil.getIndex(text);
``` ```
...@@ -291,7 +313,7 @@ An API can be called to implement measurement conversion. ...@@ -291,7 +313,7 @@ An API can be called to implement measurement conversion.
## Obtaining Line Breaks of Text ## Obtaining Line Breaks of Text
When a text is displayed in more than one line, [BreakIterator](../reference/apis/js-apis-intl.md) APIs are used to obtain the line break positions of the text. When a text is displayed in more than one line, [BreakIterator8](../reference/apis/js-apis-i18n.md#breakiterator8) APIs are used to obtain the line break positions of the text.
### Available APIs ### Available APIs
...@@ -313,52 +335,57 @@ When a text is displayed in more than one line, [BreakIterator](../reference/api ...@@ -313,52 +335,57 @@ When a text is displayed in more than one line, [BreakIterator](../reference/api
### How to Develop ### How to Develop
1. Instantiate a **BreakIterator** object. 1. Instantiate a **BreakIterator** object.
Call the **getLineInstance** method to instantiate a **BreakIterator** object. Call the **getLineInstance** method to instantiate a **BreakIterator** object.
``` ```js
var locale = "en-US" var locale = "en-US"
var breakIterator = i18n.getLineInstance(locale); var breakIterator = i18n.getLineInstance(locale);
``` ```
2. Set and access the text that requires line breaking. 2. Set and access the text that requires line breaking.
Call the **setLineBreakText** and **getLineBreakText** methods to set and access the text that requires line breaking. Call the **setLineBreakText** and **getLineBreakText** methods to set and access the text that requires line breaking.
``` ```js
var text = "Apple is my favorite fruit"; var text = "Apple is my favorite fruit";
breakIterator.setLineBreakText(text); breakIterator.setLineBreakText(text);
var breakText = breakIterator.getLineBreakText(); var breakText = breakIterator.getLineBreakText();
``` ```
3. Obtain the current position of the **BreakIterator** object. 3. Obtain the current position of the **BreakIterator** object.
Call the **current** method to obtain the current position of the **BreakIterator** object in the text being processed. Call the **current** method to obtain the current position of the **BreakIterator** object in the text being processed.
``` ```js
var pos = breakIterator.current(); var pos = breakIterator.current();
``` ```
4. Set the position of a **BreakIterator** object. 4. Set the position of a **BreakIterator** object.
The following APIs are provided to adjust the **first**, **last**, **next**, **previous**, or **following** position of the **BreakIterator** object in the text to be processed. The following APIs are provided to adjust the **first**, **last**, **next**, **previous**, or **following** position of the **BreakIterator** object in the text to be processed.
``` ```js
var firstPos = breakIterator.first(); // Sets a BreakIterator object to the first break point, that is, the start position of the text. var firstPos = breakIterator.first(); // Set a BreakIterator object to the first break point, that is, the start position of the text.
var lastPos = breakIterator.last(); // Sets a BreakIterator object to the last break point, that is, the position after the text end. var lastPos = breakIterator.last(); // Set a BreakIterator object to the last break point, that is, the position after the text end.
// Moves a BreakIterator object forward or backward by a certain number of break points. // Move a BreakIterator object forward or backward by a certain number of break points.
// If a positive number is input, move backward. If a negative number is input, move forward. If no value is input, move one position backward. // If a positive number is input, move backward. If a negative number is input, move forward. If no value is input, move one position backward.
// When the object is moved out of the text length range, -1 is returned. // When the object is moved out of the text length range, -1 is returned.
var nextPos = breakIterator.next(-2); var nextPos = breakIterator.next(-2);
var previousPos = breakIterator.previous(); // Moves a BreakIterator object to the previous break point. When the text length is out of the range, -1 is returned. var previousPos = breakIterator.previous(); // Move a BreakIterator object to the previous break point. When the text length is out of the range, -1 is returned.
// Moves a BreakIterator object to the break point following the position specified by offset. If the object is moved out of the text length range, -1 is returned. // Move a BreakIterator object to the break point following the position specified by offset. If the object is moved out of the text length range, -1 is returned.
var followingPos = breakIterator.following(10); var followingPos = breakIterator.following(10);
``` ```
5. Determine whether a position is a break point. 5. Determine whether a position is a break point.
Call the **isBoundary** method to determine whether a position is a break point. If yes, **true** is returned and the **BreakIterator** object is moved to this position. If no, **false** is returned and the **BreakIterator** object is moved to a break point after this position. Call the **isBoundary** method to determine whether a position is a break point. If yes, **true** is returned and the **BreakIterator** object is moved to this position. If no, **false** is returned and the **BreakIterator** object is moved to a break point after this position.
``` ```js
var isboundary = breakIterator.isBoundary(5); var isboundary = breakIterator.isBoundary(5);
``` ```
# Internationalization Development (intl) # Internationalization Development (Intl)
This development guide describes how to use i18n APIs that are defined in ECMA 402. This 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).
The [I18N](i18n-guidelines.md) module provides enhanced I18N capabilities through supplementary APIs that are not defined in ECMA 402. It works with the Intl module to provide a complete suite of I18N capabilities.
> **NOTE**
>
> In the code snippets in this document, **intl** refers to the name of the imported module.
## 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#locale) 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. |
...@@ -21,45 +27,63 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4 ...@@ -21,45 +27,63 @@ 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.
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#localeoptions) list.
``` 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.
- 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 Latin numbering system and Chinese calendar system are used. Extensions can also be passed via the second parameter.
| 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.|
| kf | Whether upper case or lower case is considered when sorting or comparing strings.|
```js
var locale = "zh-CN"; var locale = "zh-CN";
var options = {caseFirst: false, calendar: "chinese", collation: pinyin}; var options = {caseFirst: false, calendar: "chinese", collation: "pinyin"};
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.
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.
```js
var localeStr = localeObj.toString(); var localeStr = localeObj.toString();
``` ```
3. Maximize locale information. 3. Maximize locale information.
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.
``` ```js
var maximizedLocale = localeObj.maximize(); var maximizedLocale = localeObj.maximize();
``` ```
4. Minimize locale information. 4. Minimize locale information.
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.
``` ```js
var minimizedLocale = localeObj.minimize(); var minimizedLocale = localeObj.minimize();
``` ```
## 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#datetimeformat) 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&lt;string&gt;, options?: DateTimeOptions) | Creates a **DateTimeFormat** object and sets the locale and other formatting-related attributes. | | ohos.intl | constructor(locale: string \| Array&lt;string&gt;, options?: DateTimeOptions) | Creates a **DateTimeFormat** object and sets the locale and other formatting-related attributes. |
...@@ -71,149 +95,160 @@ This development guide describes how to use i18n APIs that are defined in ECMA 4 ...@@ -71,149 +95,160 @@ 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.
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.
```
```js
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#datetimeoptions).
``` ```js
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.
Use the **format** method of **DateTimeFormat** to format a **Date** object. A string is returned as the formatting result.
``` Call the **format** method to format the date and time in the **DateTimeFormat** object. This method returns a string representing the formatting result.
Date date = new Date();
```js
var date = new Date();
var formatResult = dateTimeFormat.format(date); var formatResult = dateTimeFormat.format(date);
``` ```
3. Format a period. 3. Format a period.
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.
``` Call the **formatRange** method to format the period in the **DateTimeFormat** object. 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 endDate = new Date(); ```js
var formatResult = dateTimeFormat.formatRange(startDate, endDate); var startDate = new Date(2021, 11, 17, 3, 24, 0);
var endDate = new Date(2021, 11, 18, 3, 24, 0);
var datefmt = new Intl.DateTimeFormat("en-GB");
datefmt.formatRange(startDate, endDate);
``` ```
4. Access the attributes of the **DateTimeFormat** object. 4. Obtain attributes of the **DateTimeFormat** object.
The **resolvedOptions** method of **DateTimeFormat** returns an object that contains all related attributes and values of the **DateTimeFormat** object.
``` Call the **resolvedOptions** method to obtain attributes of the **DateTimeFormat** object. This method will return an array that contains all attributes and values of the object.
```js
var options = dateTimeFormat.resolvedOptions(); var options = dateTimeFormat.resolvedOptions();
``` ```
## Number Formatting ## Formatting Numbers
[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#numberformat) APIs to format numbers 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&lt;string&gt;, options?: NumberOptions) | Creates a **NumberFormat** object and sets the locale and other formatting-related attributes. | | ohos.intl | constructor(locale: string \| Array&lt;string&gt;, options?: NumberOptions) | Creates a **NumberFormat** object and sets the locale and other formatting-related attributes. |
| ohos.intl | format(number: number): string | Calculates the number based on the locale and other formatting-related attributes of the **NumberFormat** object. | | ohos.intl | format(number: number): string | Calculates the number based on the locale and other formatting-related attributes of the **NumberFormat** object. |
| ohos.intl | resolvedOptions(): NumberOptions | Obtains the attributes of the **NumberFormat** object. | | ohos.intl | resolvedOptions(): NumberOptions | Obtains attributes of the **NumberFormat** object. |
### How to Develop ### How to Develop
1. Instantiate a **NumberFormat** object. 1. 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).
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.
```
```js
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#numberoptions).
``` ```js
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.
Use the **format** method of **NumberFormat** to format a number. A string is returned as the formatting result.
``` Call the **format** method to format a number. A string is returned as the formatting result.
```js
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.
The **resolvedOptions** method of NumberFormat returns an object that contains all related attributes and values of the **NumberFormat** object.
``` Call the **resolvedOptions** method to obtain attributes of the **NumberFormat** object. This method will return an array that contains all attributes and values of the object.
```js
var options = numberFormat.resolvedOptions(); var options = numberFormat.resolvedOptions();
``` ```
## String Sorting ## Sorting Strings
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#collator8) 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&lt;string&gt;, options?: CollatorOptions)<sup>8+</sup> | Creates a **Collator** object and sets the locale and other related attributes. | | ohos.intl | constructor(locale: string \| Array&lt;string&gt;, options?: CollatorOptions)<sup>8+</sup> | Creates a **Collator** object and sets the locale and other related attributes. |
| ohos.intl | 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. | | ohos.intl | 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. |
| ohos.intl | resolvedOptions(): CollatorOptions<sup>8+</sup> | Obtains the attributes of the **Collator** object. | | ohos.intl | resolvedOptions(): CollatorOptions<sup>8+</sup> | Obtains attributes of the **Collator** object. |
### How to Develop ### How to Develop
1. Instantiate a **Collator** object. 1. 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).
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.
```
```js
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#collatoroptions8).
``` ```js
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.
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.
``` Call the **compare** method 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. This allows you to sort character strings based on the comparison result.
```js
var str1 = "first string"; var str1 = "first string";
var str2 = "second string"; var str2 = "second string";
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.
The **resolvedOptions** method of **Collator** returns an object that contains all related attributes and values of the **Collator** object.
``` Call the **resolvedOptions** method to obtain attributes of the **Collator** object. This method will return an array that contains all attributes and values of the object.
```js
var options = collator.resolvedOptions(); var options = collator.resolvedOptions();
``` ```
## 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#pluralrules8) 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&lt;string&gt;, options?: PluralRulesOptions)<sup>8+</sup> | Creates a **PluralRules** object and sets the locale and other related attributes. | | ohos.intl | constructor(locale: string \| Array&lt;string&gt;, options?: PluralRulesOptions)<sup>8+</sup> | Creates a **PluralRules** object and sets the locale and other related attributes. |
...@@ -223,81 +258,95 @@ According to grammars in certain languages, the singular or plural form of a nou ...@@ -223,81 +258,95 @@ 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.
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.
```
```js
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#pluralrulesoptions8).
``` ```js
var plurals = new intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"}; var pluralRules = new intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"});
``` ```
2. Determine the singular or plural category. 2. Determine the singular-plural type.
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**.
``` Call the **select** method to determine the singular-plural type of an input number. This method will return a string representing the singular-plural type, which can be any of the following: **zero**, **one**, **two**, **few**, **many**, and **other**.
```js
var number = 1234.5678 var number = 1234.5678
var categoryResult = plurals.select(number); var categoryResult = plurals.select(number);
``` ```
## Formatting Relative Time ## Formatting the 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#relativetimeformat8) 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&lt;string&gt;, options?: RelativeTimeFormatInputOptions)<sup>8+</sup> | Creates a **RelativeTimeFormat** object and sets the locale and other formatting-related attributes. | | ohos.intl | 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. |
| ohos.intl | 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. | | ohos.intl | 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. |
| ohos.intl | formatToParts(value: number, unit: string): Array&lt;object&gt;<sup>8+</sup> | Returns each part of the relative time format based on the locale and other formatting-related attributes of the **RelativeTimeFormat** object. | | ohos.intl | formatToParts(value: number, unit: string): Array&lt;object&gt;<sup>8+</sup> | Returns each part of the relative time format based on the locale and other formatting-related attributes of the **RelativeTimeFormat** object. |
| ohos.intl | resolvedOptions(): RelativeTimeFormatResolvedOptions<sup>8+</sup> | Obtains the attributes of the **RelativeTimeFormat** object. | | ohos.intl | resolvedOptions(): RelativeTimeFormatResolvedOptions<sup>8+</sup> | Obtains attributes of the **RelativeTimeFormat** object. |
### How to Develop ### How to Develop
1. Instantiate a **RelativeTimeFormat** object. 1. Instantiate a **RelativeTimeFormat** object.
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.
```
```js
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#relativetimeformatinputoptions8).
``` ```js
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.
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.
``` Call the **format** method 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.
```js
var number = 2; var number = 2;
var unit = "year" var unit = "year"
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.
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.
```js
var number = 2; var number = 2;
var unit = "year" var unit = "year"
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.
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).
``` Call the **resolvedOptions** method to obtain attributes of the **RelativeTimeFormat** object. This method will return an array that contains all attributes and values of the object. For a full list of attributes, see [RelativeTimeFormatResolvedOptions](../reference/apis/js-apis-intl.md#relativetimeformatresolvedoptions8).
```js
var options = numberFormat.resolvedOptions(); var options = numberFormat.resolvedOptions();
``` ```
## Samples
The following sample is provided to help you better understand how to develop internationalization capabilities:
-[`International`: Internationalization (JS) (API8)](https://gitee.com/openharmony/applications_app_samples/tree/master/UI/International)
-[`International`: Internationalization (eTS) (API8) (Full SDK)](https://gitee.com/openharmony/applications_app_samples/tree/master/common/International)
# Internationalization – i18n # Internationalization – I18N
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** This module provides system-related or enhanced I18N capabilities, such as locale management, phone number formatting, and calendar, through supplementary I18N APIs that are not defined in ECMA 402.
> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
The [Intl](intl-guidelines.md) module provides basic I18N capabilities through the standard I18N APIs defined in ECMA 402. It works with the I18N module to provide a complete suite of I18N capabilities.
> **NOTE**
> >
> - This module contains enhanced i18n APIs, which are not defined in ECMA 402. > The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import ## Modules to Import
...@@ -50,14 +54,14 @@ Obtains the localized script for the specified country. ...@@ -50,14 +54,14 @@ Obtains the localized script for the specified country.
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------------ | ------- | ---- | ---------------- | | ------------ | ------- | ---- | ----------------------------- |
| country | string | Yes | Specified country. | | country | string | Yes | Specified country. |
| locale | string | Yes | Locale ID. | | locale | string | Yes | Locale ID. |
| sentenceCase | boolean | No | Whether to use sentence case for the localized script.| | sentenceCase | boolean | No | Whether to use sentence case for the localized script.|
**Return Value** **Return Value**
| Type | Description | | Type | Description |
| ------ | ------------- | | ------ | ------------------------------------------ |
| string | Localized script for the specified country.| | string | Localized script for the specified country.|
**Example** **Example**
...@@ -77,8 +81,8 @@ Checks whether the localized script for the specified language is displayed from ...@@ -77,8 +81,8 @@ Checks whether the localized script for the specified language is displayed from
**Parameters** **Parameters**
| Name | Type | Description | | Name | Type | Description |
| ------ | ------ | ------- | | ------ | ------ | ----------- |
| locale | string | Locale ID.| | locale | string | Locale ID. |
**Return Value** **Return Value**
| Type | Description | | Type | Description |
...@@ -115,7 +119,9 @@ Obtains the system language. ...@@ -115,7 +119,9 @@ Obtains the system language.
setSystemLanguage(language: string): boolean setSystemLanguage(language: string): boolean
Sets the system language. Sets the system language. Currently, this API does not support real-time updating of the system language.
**System API**: This is a system API.
**Required permission**: ohos.permission.UPDATE_CONFIGURATION **Required permission**: ohos.permission.UPDATE_CONFIGURATION
...@@ -145,9 +151,9 @@ getSystemLanguages(): Array&lt;string&gt; ...@@ -145,9 +151,9 @@ getSystemLanguages(): Array&lt;string&gt;
Obtains the list of system languages. Obtains the list of system languages.
**System capability**: SystemCapability.Global.I18n **System API**: This is a system API.
**System API**: This is a system API and cannot be called by third-party applications. **System capability**: SystemCapability.Global.I18n
**Return Value** **Return Value**
| Type | Description | | Type | Description |
...@@ -166,9 +172,9 @@ getSystemCountries(language: string): Array&lt;string&gt; ...@@ -166,9 +172,9 @@ getSystemCountries(language: string): Array&lt;string&gt;
Obtains the list of countries and regions supported for the specified language. Obtains the list of countries and regions supported for the specified language.
**System capability**: SystemCapability.Global.I18n **System API**: This is a system API.
**System API**: This is a system API and cannot be called by third-party applications. **System capability**: SystemCapability.Global.I18n
**Parameters** **Parameters**
| Name | Type | Description | | Name | Type | Description |
...@@ -211,12 +217,12 @@ setSystemRegion(region: string): boolean ...@@ -211,12 +217,12 @@ setSystemRegion(region: string): boolean
Sets the system region. Sets the system region.
**System API**: This is a system API.
**Required permission**: ohos.permission.UPDATE_CONFIGURATION **Required permission**: ohos.permission.UPDATE_CONFIGURATION
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters** **Parameters**
| Name | Type | Description | | Name | Type | Description |
| ------ | ------ | ----- | | ------ | ------ | ----- |
...@@ -258,12 +264,12 @@ setSystemLocale(locale: string): boolean ...@@ -258,12 +264,12 @@ setSystemLocale(locale: string): boolean
Sets the system locale. Sets the system locale.
**System API**: This is a system API.
**Required permission**: ohos.permission.UPDATE_CONFIGURATION **Required permission**: ohos.permission.UPDATE_CONFIGURATION
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters** **Parameters**
| Name | Type | Description | | Name | Type | Description |
| ------ | ------ | --------------- | | ------ | ------ | --------------- |
...@@ -286,9 +292,9 @@ isSuggested(language: string, region?: string): boolean ...@@ -286,9 +292,9 @@ isSuggested(language: string, region?: string): boolean
Checks whether the system language matches the specified region. Checks whether the system language matches the specified region.
**System capability**: SystemCapability.Global.I18n **System API**: This is a system API.
**System API**: This is a system API and cannot be called by third-party applications. **System capability**: SystemCapability.Global.I18n
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
...@@ -673,6 +679,31 @@ Formats a phone number. ...@@ -673,6 +679,31 @@ Formats a phone number.
phonenumberfmt.format("15812312312"); phonenumberfmt.format("15812312312");
``` ```
### getLocationName<sup>9+</sup>
getLocationName(number: string, locale: string): string
Obtains the home location of a phone number.
**System capability**: SystemCapability.Global.I18n
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ---------- |
| number | string | Yes | Phone number.|
| locale | string | Yes | Locale ID.|
**Return value**
| Type | Description |
| ------ | ---------- |
| string | Home location of the phone number.|
**Example**
```js
var phonenumberfmt = new i18n.PhoneNumberFormat("CN");
phonenumberfmt.isValidNumber("15812312312");
```
## PhoneNumberFormatOptions<sup>8+</sup> ## PhoneNumberFormatOptions<sup>8+</sup>
...@@ -727,6 +758,28 @@ Converts one measurement unit into another and formats the unit based on the spe ...@@ -727,6 +758,28 @@ Converts one measurement unit into another and formats the unit based on the spe
i18n.Util.unitConvert({unit: "cup", measureSystem: "US"}, {unit: "liter", measureSystem: "SI"}, 1000, "en-US", "long"); i18n.Util.unitConvert({unit: "cup", measureSystem: "US"}, {unit: "liter", measureSystem: "SI"}, 1000, "en-US", "long");
``` ```
### getDateOrder<sup>9+</sup>
static getDateOrder(locale: string): string
Obtains the sequence of the year, month, and day in the specified locale.
**System capability**: SystemCapability.Global.I18n
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------- | ---- | ---------------------------------------- |
| locale | string | Yes | Locale used for formatting, for example, **zh-Hans-CN**. |
**Return value**
| Type | Description |
| ------ | ----------------------- |
| string | Sequence of the year, month, and day.|
**Example**
```
i18n.Util.getDateOrder("zh-CN");
```
## getInstance<sup>8+</sup> ## getInstance<sup>8+</sup>
...@@ -816,7 +869,7 @@ Obtains the index of a text object. ...@@ -816,7 +869,7 @@ Obtains the index of a text object.
**Example** **Example**
``` ```
var indexUtil= i18n.getInstance("zh-CN"); var indexUtil= i18n.getInstance("zh-CN");
indexUtil.getIndex("hi"); // Return h. indexUtil.getIndex("hi"); // Return hi.
``` ```
...@@ -1442,10 +1495,10 @@ Obtains the **TimeZone** object corresponding to the specified time zone ID. ...@@ -1442,10 +1495,10 @@ Obtains the **TimeZone** object corresponding to the specified time zone ID.
``` ```
## RelativeTimeFormat<sup>8+</sup> ## TimeZone
### getID<sup>8+</sup> ### getID
getID(): string getID(): string
...@@ -1465,7 +1518,7 @@ Obtains the ID of the specified **TimeZone** object. ...@@ -1465,7 +1518,7 @@ Obtains the ID of the specified **TimeZone** object.
``` ```
### getDisplayName<sup>8+</sup> ### getDisplayName
getDisplayName(locale?: string, isDST?: boolean): string getDisplayName(locale?: string, isDST?: boolean): string
...@@ -1491,7 +1544,7 @@ Obtains the representation of a **TimeZone** object in the specified locale. ...@@ -1491,7 +1544,7 @@ Obtains the representation of a **TimeZone** object in the specified locale.
``` ```
### getRawOffset<sup>8+</sup> ### getRawOffset
getRawOffset(): number getRawOffset(): number
...@@ -1511,7 +1564,7 @@ Obtains the offset between the time zone represented by a **TimeZone** object an ...@@ -1511,7 +1564,7 @@ Obtains the offset between the time zone represented by a **TimeZone** object an
``` ```
### getOffset<sup>8+</sup> ### getOffset
getOffset(date?: number): number getOffset(date?: number): number
...@@ -1529,3 +1582,207 @@ Obtains the offset between the time zone represented by a **TimeZone** object an ...@@ -1529,3 +1582,207 @@ Obtains the offset between the time zone represented by a **TimeZone** object an
var timezone = i18n.getTimeZone(); var timezone = i18n.getTimeZone();
timezone.getOffset(1234567890); timezone.getOffset(1234567890);
``` ```
### getAvailableIDs<sup>9+</sup>
static getAvailableIDs(): Array&lt;string&gt;
Obtains the list of time zone IDs supported by the system.
**System capability**: SystemCapability.Global.I18n
**Return value**
| Type | Description |
| ------------------- | ----------- |
| Array&lt;string&gt; | List of time zone IDs supported by the system.|
**Example**
```ts
var ids = i18n.TimeZone.getAvailableIDs();
```
### getAvailableZoneCityIDs<sup>9+</sup>
static getAvailableZoneCityIDs(): Array&lt;string&gt;
Obtains the list of time zone city IDs supported by the system.
**System capability**: SystemCapability.Global.I18n
**Return value**
| Type | Description |
| ------------------- | ------------- |
| Array&lt;string&gt; | List of time zone city IDs supported by the system.|
**Example**
```ts
var cityIDs = i18n.TimeZone.getAvailableZoneCityIDs();
```
### getCityDisplayName<sup>9+</sup>
static getCityDisplayName(cityID: string, locale: string): string
Obtains the localized display of a time zone city in the specified locale.
**System capability**: SystemCapability.Global.I18n
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------ |
| cityID | string | Yes | Time zone city ID.|
| locale | string | Yes | Locale ID. |
**Return value**
| Type | Description |
| ------ | ------------------ |
| string | Localized display of the time zone city in the specified locale.|
**Example**
```ts
var displayName = i18n.TimeZone.getCityDisplayName("Shanghai", "zh-CN");
```
### getTimezoneFromCity<sup>9+</sup>
static getTimezoneFromCity(cityID: string): TimeZone
Obtains the **TimeZone** object corresponding to the specified time zone city ID.
**System capability**: SystemCapability.Global.I18n
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------ |
| cityID | string | Yes | Time zone city ID.|
**Return value**
| Type | Description |
| -------- | ----------- |
| TimeZone | **TimeZone** object corresponding to the specified time zone city ID.|
**Example**
```ts
var timezone = i18n.TimeZone.getTimezoneFromCity("Shanghai");
```
## i18n.setUsingLocalDigit<sup>9+</sup>
setUsingLocalDigit(flag: boolean): boolean
Sets whether to turn on the local digit switch.
This is a system API.
**Permission required**: ohos.permission.UPDATE_CONFIGURATION
**System capability**: SystemCapability.Global.I18n
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ------- | ---- | ------------------------------- |
| flag | boolean | Yes | Whether to turn on the local digit switch. The value **true** means to turn on the local digit switch, and the value **false** indicates the opposite.|
**Return value**
| Type | Description |
| ------- | ----------------------------------- |
| boolean | Result indicating whether the local digit switch is successfully set. The value **true** indicates that the local digit switch is successfully set, and the value **false** indicates the opposite.|
**Example**
```ts
var status = i18n.setUsingLocalDigit(true);
```
## i18n.getUsingLocalDigit<sup>9+</sup>
getUsingLocalDigit(): boolean
Checks whether the local digit switch is turned on.
**System capability**: SystemCapability.Global.I18n
**Return value**
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Result indicating whether the local digit switch is turned on. The value **true** indicates that the local digit switch is turned on, and the value **false** indicates the opposite.|
**Example**
```ts
var status = i18n.getUsingLocalDigit();
```
## Transliterator<sup>9+</sup>
### getAvailableIDs<sup>9+</sup>
static getAvailableIDs(): string[]
Obtains a list of IDs supported by the **Transliterator** object.
**System capability**: SystemCapability.Global.I18n
**Return value**
| Type | Description |
| -------- | ---------- |
| string[] | List of IDs supported by the **Transliterator** object.|
**Example**
```ts
i18n.Transliterator.getAvailableIDs();
```
### getInstance<sup>9+</sup>
static getInstance(id: string): Transliterator
Creates a **Transliterator** object.
**System capability**: SystemCapability.Global.I18n
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | -------- |
| id | string | Yes | ID supported by the **Transliterator** object.|
**Return value**
| Type | Description |
| ---------------------------------- | ----- |
| [Transliterator](#transliterator9) | **Transliterator** object.|
**Example**
```ts
var transliterator = i18n.Transliterator.getInstance("Any-Latn");
```
### transform<sup>9+</sup>
transform(text: string): string
Converts the input string from the source format to the target format.
**System capability**: SystemCapability.Global.I18n
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ------ |
| text | string | Yes | Input string.|
**Return value**
| Type | Description |
| ------ | -------- |
| string | Target string.|
**Example**
```ts
var transliterator = i18n.Transliterator.getInstance("Any-Latn");
transliterator.transform ("China");
```
# Internationalization – intl # Internationalization – Intl
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** This 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.
> - The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
The [I18N](i18n-guidelines.md) module provides enhanced I18N capabilities through supplementary APIs that are not defined in ECMA 402. It works with the Intl module to provide a complete suite of I18N capabilities.
> **NOTE**
> >
> - This module contains standard i18n APIs, which are defined in ECMA 402. > The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import ## Modules to Import
``` ```js
import Intl from '@ohos.intl'; import Intl from '@ohos.intl';
``` ```
...@@ -43,7 +46,7 @@ Creates a Locale object. ...@@ -43,7 +46,7 @@ Creates a Locale object.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Example** **Example**
``` ```js
var locale = new Intl.Locale(); var locale = new Intl.Locale();
``` ```
...@@ -63,7 +66,7 @@ Creates a Locale object. ...@@ -63,7 +66,7 @@ Creates a Locale object.
| options | LocaleOptions | No | Options for creating the **Locale** object. | | options | LocaleOptions | No | Options for creating the **Locale** object. |
**Example** **Example**
``` ```js
var locale = new Intl.Locale("zh-CN"); var locale = new Intl.Locale("zh-CN");
``` ```
...@@ -82,7 +85,7 @@ Converts locale information to a string. ...@@ -82,7 +85,7 @@ Converts locale information to a string.
| string | String containing locale information.| | string | String containing locale information.|
**Example** **Example**
``` ```js
var locale = new Intl.Locale("zh-CN"); var locale = new Intl.Locale("zh-CN");
locale.toString(); locale.toString();
``` ```
...@@ -102,7 +105,7 @@ Maximizes information of the **Locale** object. If the script and locale informa ...@@ -102,7 +105,7 @@ Maximizes information of the **Locale** object. If the script and locale informa
| [Locale](#locale) | **Locale** object with the maximized information.| | [Locale](#locale) | **Locale** object with the maximized information.|
**Example** **Example**
``` ```js
var locale = new Intl.Locale("zh-CN"); var locale = new Intl.Locale("zh-CN");
locale.maximize(); locale.maximize();
``` ```
...@@ -122,7 +125,7 @@ Minimizes information of the **Locale** object. If the script and locale informa ...@@ -122,7 +125,7 @@ Minimizes information of the **Locale** object. If the script and locale informa
| [Locale](#locale) | **Locale** object with the minimized information.| | [Locale](#locale) | **Locale** object with the minimized information.|
**Example** **Example**
``` ```js
var locale = new Intl.Locale("zh-CN"); var locale = new Intl.Locale("zh-CN");
locale.minimize(); locale.minimize();
``` ```
...@@ -156,7 +159,7 @@ Creates a **DateTimeOptions** object for the specified locale. ...@@ -156,7 +159,7 @@ Creates a **DateTimeOptions** object for the specified locale.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Example** **Example**
``` ```js
var datefmt= new Intl.DateTimeFormat(); var datefmt= new Intl.DateTimeFormat();
``` ```
...@@ -176,13 +179,13 @@ Creates a **DateTimeOptions** object for the specified locale. ...@@ -176,13 +179,13 @@ Creates a **DateTimeOptions** object for the specified locale.
| options | [DateTimeOptions](#datetimeoptions) | No | Options for creating a **DateTimeFormat** object. | | options | [DateTimeOptions](#datetimeoptions) | No | Options for creating a **DateTimeFormat** object. |
**Example** **Example**
``` ```js
var datefmt= new Intl.DateTimeFormat("zh-CN", { dateStyle: 'full', timeStyle: 'medium' }); var datefmt= new Intl.DateTimeFormat("zh-CN", { dateStyle: 'full', timeStyle: 'medium' });
``` ```
**Example** **Example**
``` ```js
var datefmt= new Intl.DateTimeFormat(["ban", "zh"], { dateStyle: 'full', timeStyle: 'medium' }); var datefmt= new Intl.DateTimeFormat(["ban", "zh"], { dateStyle: 'full', timeStyle: 'medium' });
``` ```
...@@ -206,7 +209,7 @@ Formats the specified date and time. ...@@ -206,7 +209,7 @@ Formats the specified date and time.
| string | A string containing the formatted date and time.| | string | A string containing the formatted date and time.|
**Example** **Example**
``` ```js
var date = new Date(2021, 11, 17, 3, 24, 0); var date = new Date(2021, 11, 17, 3, 24, 0);
var datefmt = new Intl.DateTimeFormat("en-GB"); var datefmt = new Intl.DateTimeFormat("en-GB");
datefmt.format(date); datefmt.format(date);
...@@ -233,7 +236,7 @@ Formats the specified date range. ...@@ -233,7 +236,7 @@ Formats the specified date range.
| string | A string containing the formatted date and time range.| | string | A string containing the formatted date and time range.|
**Example** **Example**
``` ```js
var startDate = new Date(2021, 11, 17, 3, 24, 0); var startDate = new Date(2021, 11, 17, 3, 24, 0);
var endDate = new Date(2021, 11, 18, 3, 24, 0); var endDate = new Date(2021, 11, 18, 3, 24, 0);
var datefmt = new Intl.DateTimeFormat("en-GB"); var datefmt = new Intl.DateTimeFormat("en-GB");
...@@ -255,7 +258,7 @@ Obtains the formatting options for **DateTimeFormat** object. ...@@ -255,7 +258,7 @@ Obtains the formatting options for **DateTimeFormat** object.
| [DateTimeOptions](#datetimeoptions) | Formatting options for **DateTimeFormat** objects.| | [DateTimeOptions](#datetimeoptions) | Formatting options for **DateTimeFormat** objects.|
**Example** **Example**
``` ```js
var datefmt = new Intl.DateTimeFormat("en-GB"); var datefmt = new Intl.DateTimeFormat("en-GB");
datefmt.resolvedOptions(); datefmt.resolvedOptions();
``` ```
...@@ -302,7 +305,7 @@ Creates a **NumberFormat** object for the specified locale. ...@@ -302,7 +305,7 @@ Creates a **NumberFormat** object for the specified locale.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Example** **Example**
``` ```js
var numfmt = new Intl.NumberFormat(); var numfmt = new Intl.NumberFormat();
``` ```
...@@ -322,7 +325,7 @@ Parameters ...@@ -322,7 +325,7 @@ Parameters
| options | [NumberOptions](#numberoptions) | No | Options for creating a **NumberFormat** object. | | options | [NumberOptions](#numberoptions) | No | Options for creating a **NumberFormat** object. |
**Example** **Example**
``` ```js
var numfmt = new Intl.NumberFormat("en-GB", {style:'decimal', notation:"scientific"}); var numfmt = new Intl.NumberFormat("en-GB", {style:'decimal', notation:"scientific"});
``` ```
...@@ -347,7 +350,7 @@ Formats a number. ...@@ -347,7 +350,7 @@ Formats a number.
**Example** **Example**
``` ```js
var numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"}); var numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
numfmt.format(1223); numfmt.format(1223);
``` ```
...@@ -368,7 +371,7 @@ Obtains the options of the **NumberFormat** object. ...@@ -368,7 +371,7 @@ Obtains the options of the **NumberFormat** object.
**Example** **Example**
``` ```js
var numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"}); var numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
numfmt.resolvedOptions(); numfmt.resolvedOptions();
``` ```
...@@ -415,7 +418,7 @@ Creates a Collator object. ...@@ -415,7 +418,7 @@ Creates a Collator object.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Example** **Example**
``` ```js
var collator = new Intl.Collator(); var collator = new Intl.Collator();
``` ```
...@@ -436,7 +439,7 @@ Creates a Collator object. ...@@ -436,7 +439,7 @@ Creates a Collator object.
| options | [CollatorOptions](#collatoroptions) | No | Options for creating a **Collator** object. | | options | [CollatorOptions](#collatoroptions) | No | Options for creating a **Collator** object. |
**Example** **Example**
``` ```js
var collator = new Intl.Collator("zh-CN", {localeMatcher: "lookup", usage: "sort"}); var collator = new Intl.Collator("zh-CN", {localeMatcher: "lookup", usage: "sort"});
``` ```
...@@ -461,7 +464,7 @@ Compares two strings based on the sorting policy of the **Collator** object. ...@@ -461,7 +464,7 @@ Compares two strings based on the sorting policy of the **Collator** object.
| number | Comparison result. If the value is a negative number, the first string is before the second string. If the value of number is **0**, the first string is equal to the second string. If the value of number is a positive number, the first string is after the second string.| | number | Comparison result. If the value is a negative number, the first string is before the second string. If the value of number is **0**, the first string is equal to the second string. If the value of number is a positive number, the first string is after the second string.|
**Example** **Example**
``` ```js
var collator = new Intl.Collator("zh-Hans"); var collator = new Intl.Collator("zh-Hans");
collator.compare("first", "second"); collator.compare("first", "second");
``` ```
...@@ -481,13 +484,13 @@ Returns properties reflecting the locale and collation options of a **Collator** ...@@ -481,13 +484,13 @@ Returns properties reflecting the locale and collation options of a **Collator**
| [CollatorOptions](#collatoroptions) | Properties of the **Collator** object.| | [CollatorOptions](#collatoroptions) | Properties of the **Collator** object.|
**Example** **Example**
``` ```js
var collator = new Intl.Collator("zh-Hans"); var collator = new Intl.Collator("zh-Hans");
var options = collator.resolvedOptions(); var options = collator.resolvedOptions();
``` ```
## CollatorOptions<sup>8+</sup><a name=collatoroptions></a> ## CollatorOptions<sup>8+</sup>
Represents the properties of a **Collator** object. Represents the properties of a **Collator** object.
...@@ -516,7 +519,7 @@ Create a **PluralRules** object. ...@@ -516,7 +519,7 @@ Create a **PluralRules** object.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Example** **Example**
``` ```js
var pluralRules = new Intl.PluralRules(); var pluralRules = new Intl.PluralRules();
``` ```
...@@ -536,8 +539,8 @@ Parameters ...@@ -536,8 +539,8 @@ Parameters
| options | [PluralRulesOptions](#pluralrulesoptions) | No | Options for creating a **PluralRules** object. | | options | [PluralRulesOptions](#pluralrulesoptions) | No | Options for creating a **PluralRules** object. |
**Example** **Example**
``` ```js
var pluralRules= new Intl.PluraRules("zh-CN", {"localeMatcher": "lookup", "type": "cardinal"}); var pluralRules= new Intl.PluralRules("zh-CN", {"localeMatcher": "lookup", "type": "cardinal"});
``` ```
...@@ -560,13 +563,13 @@ Obtains a string that represents the singular-plural type of the specified numbe ...@@ -560,13 +563,13 @@ Obtains a string that represents the singular-plural type of the specified numbe
| string | Singular-plural type. The value can be any of the following: **one**, **two**, **few**, **many**, **others**.| | string | Singular-plural type. The value can be any of the following: **one**, **two**, **few**, **many**, **others**.|
**Example** **Example**
``` ```js
var pluralRules = new Intl.PluralRules("zh-Hans"); var pluralRules = new Intl.PluralRules("zh-Hans");
pluralRules.select(1); pluralRules.select(1);
``` ```
## PluralRulesOptions<sup>8+</sup><a name=pluralrulesoptions></a> ## PluralRulesOptions<sup>8+</sup>
Represents the properties of a **PluralRules** object. Represents the properties of a **PluralRules** object.
...@@ -595,7 +598,7 @@ Creates a **RelativeTimeFormat** object. ...@@ -595,7 +598,7 @@ Creates a **RelativeTimeFormat** object.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Example** **Example**
``` ```js
var relativetimefmt = new Intl.RelativeTimeFormat(); var relativetimefmt = new Intl.RelativeTimeFormat();
``` ```
...@@ -615,7 +618,7 @@ Parameters ...@@ -615,7 +618,7 @@ Parameters
| options | [RelativeTimeFormatInputOptions](#relativetimeformatinputoptions) | No | Options for creating a **RelativeTimeFormat** object. | | options | [RelativeTimeFormatInputOptions](#relativetimeformatinputoptions) | No | Options for creating a **RelativeTimeFormat** object. |
**Example** **Example**
``` ```js
var relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {"localeMatcher": "lookup", "numeric": "always", "style": "long"}); var relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {"localeMatcher": "lookup", "numeric": "always", "style": "long"});
``` ```
...@@ -640,7 +643,7 @@ Formats the value and unit based on the specified locale and formatting options. ...@@ -640,7 +643,7 @@ Formats the value and unit based on the specified locale and formatting options.
| string | Relative time after formatting.| | string | Relative time after formatting.|
**Example** **Example**
``` ```js
var relativetimefmt = new Intl.RelativeTimeFormat("zh-CN"); var relativetimefmt = new Intl.RelativeTimeFormat("zh-CN");
relativetimefmt.format(3, "quarter") relativetimefmt.format(3, "quarter")
``` ```
...@@ -666,7 +669,7 @@ Returns an array of RelativeTimeFormat objects in parts for locale-aware formatt ...@@ -666,7 +669,7 @@ Returns an array of RelativeTimeFormat objects in parts for locale-aware formatt
| Array&lt;object&gt; | An array of **RelativeTimeFormat** objects in parts.| | Array&lt;object&gt; | An array of **RelativeTimeFormat** objects in parts.|
**Example** **Example**
``` ```js
var relativetimefmt = new Intl.RelativeTimeFormat("en", {"numeric": "auto"}); var relativetimefmt = new Intl.RelativeTimeFormat("en", {"numeric": "auto"});
var parts = relativetimefmt.format(10, "seconds"); var parts = relativetimefmt.format(10, "seconds");
``` ```
...@@ -686,13 +689,13 @@ Obtains the formatting options for **RelativeTimeFormat** objects. ...@@ -686,13 +689,13 @@ Obtains the formatting options for **RelativeTimeFormat** objects.
| [RelativeTimeFormatResolvedOptions](#relativetimeformatresolvedoptions) | Formatting options for **RelativeTimeFormat** objects.| | [RelativeTimeFormatResolvedOptions](#relativetimeformatresolvedoptions) | Formatting options for **RelativeTimeFormat** objects.|
**Example** **Example**
``` ```js
var relativetimefmt= new Intl.RelativeTimeFormat("en-GB"); var relativetimefmt= new Intl.RelativeTimeFormat("en-GB");
relativetimefmt.resolvedOptions(); relativetimefmt.resolvedOptions();
``` ```
## RelativeTimeFormatInputOptions<sup>8+</sup><a name=relativetimeformatinputoptions></a> ## RelativeTimeFormatInputOptions<sup>8+</sup>
Represents the properties of a **RelativeTimeFormat** object. Represents the properties of a **RelativeTimeFormat** object.
...@@ -705,7 +708,7 @@ Represents the properties of a **RelativeTimeFormat** object. ...@@ -705,7 +708,7 @@ Represents the properties of a **RelativeTimeFormat** object.
| style | string | Yes | Yes | Length of an internationalized message. The value can be **long**, **short**, or **narrow**.| | style | string | Yes | Yes | Length of an internationalized message. The value can be **long**, **short**, or **narrow**.|
## RelativeTimeFormatResolvedOptions<sup>8+</sup><a name=relativetimeformatresolvedoptions></a> ## RelativeTimeFormatResolvedOptions<sup>8+</sup>
Represents the properties of a **RelativeTimeFormat** object. Represents the properties of a **RelativeTimeFormat** object.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册