提交 579d38b4 编写于 作者: S shawn_he

update doc

Signed-off-by: Nshawn_he <shawn.he@huawei.com>
上级 a8c2a3f7
...@@ -28,7 +28,7 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -28,7 +28,7 @@ You can use APIs provided in the following table to obtain the system language a
```js ```js
var language = i18n.getSystemLanguage(); let language = i18n.getSystemLanguage();
``` ```
2. Obtain the system region. 2. Obtain the system region.
...@@ -36,7 +36,7 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -36,7 +36,7 @@ You can use APIs provided in the following table to obtain the system language a
Call the **getSystemRegion** method to obtain the system region. Call the **getSystemRegion** method to obtain the system region.
```js ```js
var region = i18n.getSystemRegion(); let region = i18n.getSystemRegion();
``` ```
3. Obtain the system locale. 3. Obtain the system locale.
...@@ -44,7 +44,7 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -44,7 +44,7 @@ You can use APIs provided in the following table to obtain the system language a
Call the **getSystemLocale** method to obtain the system locale. Call the **getSystemLocale** method to obtain the system locale.
```js ```js
var locale = i18n.getSystemLocale(); let locale = i18n.getSystemLocale();
``` ```
4. Check whether the locale's language is RTL. 4. Check whether the locale's language is RTL.
...@@ -53,7 +53,7 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -53,7 +53,7 @@ You can use APIs provided in the following table to obtain the system language a
```js ```js
var rtl = i18n.isRTL("zh-CN"); let 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.
...@@ -61,7 +61,7 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -61,7 +61,7 @@ You can use APIs provided in the following table to obtain the system language a
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 ```js
var hourClock = i18n.is24HourClock(); let hourClock = i18n.is24HourClock();
``` ```
6. Obtain the localized display of a language. 6. Obtain the localized display of a language.
...@@ -69,10 +69,10 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -69,10 +69,10 @@ You can use APIs provided in the following table to obtain the system language a
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 ```js
var language = "en"; let language = "en";
var locale = "zh-CN"; let locale = "zh-CN";
var sentenceCase = false; let sentenceCase = false;
var localizedLanguage = i18n.getDisplayLanguage(language, locale, sentenceCase); let localizedLanguage = i18n.getDisplayLanguage(language, locale, sentenceCase);
``` ```
7. Obtain the localized display of a country. 7. Obtain the localized display of a country.
...@@ -80,10 +80,10 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -80,10 +80,10 @@ You can use APIs provided in the following table to obtain the system language a
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. 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 ```js
var country = "US"; let country = "US";
var locale = "zh-CN"; let locale = "zh-CN";
var sentenceCase = false; let sentenceCase = false;
var localizedCountry = i18n.getDisplayCountry(country, locale, sentenceCase); let localizedCountry = i18n.getDisplayCountry(country, locale, sentenceCase);
``` ```
...@@ -118,7 +118,7 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -118,7 +118,7 @@ You can use APIs provided in the following table to obtain the system language a
```js ```js
var calendar = i18n.getCalendar("zh-CN", "gregory"); let calendar = i18n.getCalendar("zh-CN", "gregory");
``` ```
2. Set the time for the **Calendar** object. 2. Set the time for the **Calendar** object.
...@@ -126,9 +126,9 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -126,9 +126,9 @@ You can use APIs provided in the following table to obtain the system language a
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 ```js
var date1 = new Date(); let date1 = new Date();
calendar.setTime(date1); calendar.setTime(date1);
var date2 = 1000; let date2 = 1000;
calendar.setTime(date2); calendar.setTime(date2);
``` ```
...@@ -147,7 +147,7 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -147,7 +147,7 @@ You can use APIs provided in the following table to obtain the system language a
```js ```js
calendar.setTimeZone("Asia/Shanghai"); calendar.setTimeZone("Asia/Shanghai");
var timezone = calendar.getTimeZone(); let 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.
...@@ -157,7 +157,7 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -157,7 +157,7 @@ You can use APIs provided in the following table to obtain the system language a
```js ```js
calendar.setFirstDayOfWeek(1); calendar.setFirstDayOfWeek(1);
var firstDayOfWeek = calendar.getFirstDayOfWeek(); let 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.
...@@ -166,7 +166,7 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -166,7 +166,7 @@ You can use APIs provided in the following table to obtain the system language a
```js ```js
calendar.setMinimalDaysInFirstWeek(3); calendar.setMinimalDaysInFirstWeek(3);
var minimalDaysInFirstWeek = calendar.getMinimalDaysInFirstWeek(); let minimalDaysInFirstWeek = calendar.getMinimalDaysInFirstWeek();
``` ```
7. Obtain the localized display of the **Calendar** object. 7. Obtain the localized display of the **Calendar** object.
...@@ -175,7 +175,7 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -175,7 +175,7 @@ You can use APIs provided in the following table to obtain the system language a
```js ```js
var localizedName = calendar.getDisplayName("zh-CN"); let localizedName = calendar.getDisplayName("zh-CN");
``` ```
8. Check whether a date is a weekend. 8. Check whether a date is a weekend.
...@@ -184,8 +184,8 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -184,8 +184,8 @@ You can use APIs provided in the following table to obtain the system language a
```js ```js
var date = new Date(); let date = new Date();
var weekend = calendar.isWeekend(date); let weekend = calendar.isWeekend(date);
``` ```
...@@ -211,21 +211,21 @@ You can use APIs provided in the following table to obtain the system language a ...@@ -211,21 +211,21 @@ You can use APIs provided in the following table to obtain the system language a
```js ```js
var phoneNumberFormat = new i18n.PhoneNumberFormat("CN", {type: "E164"}); let 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 ```js
var validNumber = phoneNumberFormat.isValidNumber("15812341234"); let 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 ```js
var formattedNumber = phoneNumberFormat.format("15812341234"); let formattedNumber = phoneNumberFormat.format("15812341234");
``` ```
...@@ -248,11 +248,11 @@ The **unitConvert** API is provided to help you implement measurement conversion ...@@ -248,11 +248,11 @@ The **unitConvert** API is provided to help you implement measurement conversion
```js ```js
var fromUnit = {unit: "cup", measureSystem: "US"}; let fromUnit = {unit: "cup", measureSystem: "US"};
var toUnit = {unit: "liter", measureSystem: "SI"}; let toUnit = {unit: "liter", measureSystem: "SI"};
var number = 1000; let number = 1000;
var locale = "en-US"; let locale = "en-US";
var style = "long"; let style = "long";
i18n.Util.unitConvert(fromUtil, toUtil, number, locale, style); i18n.Util.unitConvert(fromUtil, toUtil, number, locale, style);
``` ```
...@@ -280,7 +280,7 @@ The **unitConvert** API is provided to help you implement measurement conversion ...@@ -280,7 +280,7 @@ The **unitConvert** API is provided to help you implement measurement conversion
```js ```js
var indexUtil = i18n.getInstance("zh-CN"); let indexUtil = i18n.getInstance("zh-CN");
``` ```
2. Obtain the index list. 2. Obtain the index list.
...@@ -288,7 +288,7 @@ The **unitConvert** API is provided to help you implement measurement conversion ...@@ -288,7 +288,7 @@ The **unitConvert** API is provided to help you implement measurement conversion
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 ```js
var indexList = indexUtil.getIndexList(); let indexList = indexUtil.getIndexList();
``` ```
3. Add an index. 3. Add an index.
...@@ -304,7 +304,7 @@ The **unitConvert** API is provided to help you implement measurement conversion ...@@ -304,7 +304,7 @@ The **unitConvert** API is provided to help you implement measurement conversion
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 ```js
var text = "access index"; let text = "access index";
indexUtil.getIndex(text); indexUtil.getIndex(text);
``` ```
...@@ -338,8 +338,8 @@ When a text is displayed in more than one line, [BreakIterator8](../reference/ap ...@@ -338,8 +338,8 @@ When a text is displayed in more than one line, [BreakIterator8](../reference/ap
```js ```js
var locale = "en-US" let locale = "en-US"
var breakIterator = i18n.getLineInstance(locale); let breakIterator = i18n.getLineInstance(locale);
``` ```
2. Set and access the text that requires line breaking. 2. Set and access the text that requires line breaking.
...@@ -348,9 +348,9 @@ When a text is displayed in more than one line, [BreakIterator8](../reference/ap ...@@ -348,9 +348,9 @@ When a text is displayed in more than one line, [BreakIterator8](../reference/ap
```js ```js
var text = "Apple is my favorite fruit"; let text = "Apple is my favorite fruit";
breakIterator.setLineBreakText(text); breakIterator.setLineBreakText(text);
var breakText = breakIterator.getLineBreakText(); let breakText = breakIterator.getLineBreakText();
``` ```
3. Obtain the current position of the **BreakIterator** object. 3. Obtain the current position of the **BreakIterator** object.
...@@ -359,7 +359,7 @@ When a text is displayed in more than one line, [BreakIterator8](../reference/ap ...@@ -359,7 +359,7 @@ When a text is displayed in more than one line, [BreakIterator8](../reference/ap
```js ```js
var pos = breakIterator.current(); let pos = breakIterator.current();
``` ```
4. Set the position of a **BreakIterator** object. 4. Set the position of a **BreakIterator** object.
...@@ -368,15 +368,15 @@ When a text is displayed in more than one line, [BreakIterator8](../reference/ap ...@@ -368,15 +368,15 @@ When a text is displayed in more than one line, [BreakIterator8](../reference/ap
```js ```js
var firstPos = breakIterator.first(); // Set a BreakIterator object to the first break point, that is, the start position of the text. let firstPos = breakIterator.first(); // Set a BreakIterator object to the first break point, that is, the start position of the text.
var lastPos = breakIterator.last(); // Set a BreakIterator object to the last break point, that is, the position after the text end. let lastPos = breakIterator.last(); // Set a BreakIterator object to the last break point, that is, the position after the text end.
// Move 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); let nextPos = breakIterator.next(-2);
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. let previousPos = breakIterator.previous(); // Move a BreakIterator object to the previous break point. When the text length is out of the 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. // 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); let followingPos = breakIterator.following(10);
``` ```
5. Determine whether a position is a break point. 5. Determine whether a position is a break point.
...@@ -385,7 +385,7 @@ When a text is displayed in more than one line, [BreakIterator8](../reference/ap ...@@ -385,7 +385,7 @@ When a text is displayed in more than one line, [BreakIterator8](../reference/ap
```js ```js
var isboundary = breakIterator.isBoundary(5); let isboundary = breakIterator.isBoundary(5);
``` ```
``` ```
\ No newline at end of file
...@@ -44,9 +44,9 @@ Use [Locale](../reference/apis/js-apis-intl.md#locale) APIs to maximize or minim ...@@ -44,9 +44,9 @@ Use [Locale](../reference/apis/js-apis-intl.md#locale) APIs to maximize or minim
```js ```js
var locale = "zh-CN"; let locale = "zh-CN";
var options = {caseFirst: "false", calendar: "chinese", collation: "pinyin"}; let options = {caseFirst: "false", calendar: "chinese", collation: "pinyin"};
var localeObj = new intl.Locale(locale, options); let localeObj = new intl.Locale(locale, options);
``` ```
2. Obtain the string representing a **Locale** object. 2. Obtain the string representing a **Locale** object.
...@@ -54,7 +54,7 @@ Use [Locale](../reference/apis/js-apis-intl.md#locale) APIs to maximize or minim ...@@ -54,7 +54,7 @@ Use [Locale](../reference/apis/js-apis-intl.md#locale) APIs to maximize or minim
Call the **toString** method to obtain the string representing a **Locale** object, which includes 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 ```js
var localeStr = localeObj.toString(); let localeStr = localeObj.toString();
``` ```
3. Maximize locale information. 3. Maximize locale information.
...@@ -62,7 +62,7 @@ Use [Locale](../reference/apis/js-apis-intl.md#locale) APIs to maximize or minim ...@@ -62,7 +62,7 @@ Use [Locale](../reference/apis/js-apis-intl.md#locale) APIs to maximize or minim
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 ```js
var maximizedLocale = localeObj.maximize(); let maximizedLocale = localeObj.maximize();
``` ```
4. Minimize locale information. 4. Minimize locale information.
...@@ -70,7 +70,7 @@ Use [Locale](../reference/apis/js-apis-intl.md#locale) APIs to maximize or minim ...@@ -70,7 +70,7 @@ Use [Locale](../reference/apis/js-apis-intl.md#locale) APIs to maximize or minim
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 ```js
var minimizedLocale = localeObj.minimize(); let minimizedLocale = localeObj.minimize();
``` ```
...@@ -98,14 +98,14 @@ Use [DateTimeFormat](../reference/apis/js-apis-intl.md#datetimeformat) APIs to f ...@@ -98,14 +98,14 @@ Use [DateTimeFormat](../reference/apis/js-apis-intl.md#datetimeformat) APIs to f
```js ```js
var dateTimeFormat = new intl.DateTimeFormat(); let 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#datetimeoptions). 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 ```js
var options = {dateStyle: "full", timeStyle: "full"}; let options = {dateStyle: "full", timeStyle: "full"};
var dateTimeFormat = new intl.DateTimeFormat("zh-CN", options); let dateTimeFormat = new intl.DateTimeFormat("zh-CN", options);
``` ```
2. Format the date and time. 2. Format the date and time.
...@@ -113,8 +113,8 @@ Use [DateTimeFormat](../reference/apis/js-apis-intl.md#datetimeformat) APIs to f ...@@ -113,8 +113,8 @@ Use [DateTimeFormat](../reference/apis/js-apis-intl.md#datetimeformat) APIs to f
Call the **format** method to format the date and time in the **DateTimeFormat** object. This method returns a string representing 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.
```js ```js
var date = new Date(); let date = new Date();
var formatResult = dateTimeFormat.format(date); let formatResult = dateTimeFormat.format(date);
``` ```
3. Format a period. 3. Format a period.
...@@ -122,9 +122,9 @@ Use [DateTimeFormat](../reference/apis/js-apis-intl.md#datetimeformat) APIs to f ...@@ -122,9 +122,9 @@ Use [DateTimeFormat](../reference/apis/js-apis-intl.md#datetimeformat) APIs to f
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. 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.
```js ```js
var startDate = new Date(2021, 11, 17, 3, 24, 0); let startDate = new Date(2021, 11, 17, 3, 24, 0);
var endDate = new Date(2021, 11, 18, 3, 24, 0); let endDate = new Date(2021, 11, 18, 3, 24, 0);
var datefmt = new Intl.DateTimeFormat("en-GB"); let datefmt = new Intl.DateTimeFormat("en-GB");
datefmt.formatRange(startDate, endDate); datefmt.formatRange(startDate, endDate);
``` ```
...@@ -133,7 +133,7 @@ Use [DateTimeFormat](../reference/apis/js-apis-intl.md#datetimeformat) APIs to f ...@@ -133,7 +133,7 @@ Use [DateTimeFormat](../reference/apis/js-apis-intl.md#datetimeformat) APIs to f
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. 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 ```js
var options = dateTimeFormat.resolvedOptions(); let options = dateTimeFormat.resolvedOptions();
``` ```
...@@ -160,14 +160,14 @@ Use [NumberFormat](../reference/apis/js-apis-intl.md#numberformat) APIs to forma ...@@ -160,14 +160,14 @@ Use [NumberFormat](../reference/apis/js-apis-intl.md#numberformat) APIs to forma
```js ```js
var numberFormat = new intl.NumberFormat(); let 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#numberoptions). 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 ```js
var options = {compactDisplay: "short", notation: "compact"}; let options = {compactDisplay: "short", notation: "compact"};
var numberFormat = new intl.NumberFormat("zh-CN", options); let numberFormat = new intl.NumberFormat("zh-CN", options);
``` ```
2. Format a number. 2. Format a number.
...@@ -175,8 +175,8 @@ Use [NumberFormat](../reference/apis/js-apis-intl.md#numberformat) APIs to forma ...@@ -175,8 +175,8 @@ Use [NumberFormat](../reference/apis/js-apis-intl.md#numberformat) APIs to forma
Call the **format** method 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 ```js
var number = 1234.5678 let number = 1234.5678
var formatResult = numberFormat.format(number); let formatResult = numberFormat.format(number);
``` ```
3. Obtain attributes of the **NumberFormat** object. 3. Obtain attributes of the **NumberFormat** object.
...@@ -184,7 +184,7 @@ Use [NumberFormat](../reference/apis/js-apis-intl.md#numberformat) APIs to forma ...@@ -184,7 +184,7 @@ Use [NumberFormat](../reference/apis/js-apis-intl.md#numberformat) APIs to forma
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. 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 ```js
var options = numberFormat.resolvedOptions(); let options = numberFormat.resolvedOptions();
``` ```
...@@ -211,13 +211,13 @@ Use [Collator](../reference/apis/js-apis-intl.md#collator8) APIs to sort strings ...@@ -211,13 +211,13 @@ Use [Collator](../reference/apis/js-apis-intl.md#collator8) APIs to sort strings
```js ```js
var collator = new intl.Collator(); let 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#collatoroptions9). Alternatively, use your own locale and formatting parameters to create a **Collator** object. For a full list of parameters, see [CollatorOptions](../reference/apis/js-apis-intl.md#collatoroptions9).
```js ```js
var collator= new intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort"}); let collator= new intl.Collator("zh-CN", {localeMatcher: "best fit", usage: "sort"});
``` ```
2. Compare two strings. 2. Compare two strings.
...@@ -225,9 +225,9 @@ Use [Collator](../reference/apis/js-apis-intl.md#collator8) APIs to sort strings ...@@ -225,9 +225,9 @@ Use [Collator](../reference/apis/js-apis-intl.md#collator8) APIs to sort strings
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. 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 ```js
var str1 = "first string"; let str1 = "first string";
var str2 = "second string"; let str2 = "second string";
var compareResult = collator.compare(str1, str2); let compareResult = collator.compare(str1, str2);
``` ```
3. Obtain attributes of the **Collator** object. 3. Obtain attributes of the **Collator** object.
...@@ -235,7 +235,7 @@ Use [Collator](../reference/apis/js-apis-intl.md#collator8) APIs to sort strings ...@@ -235,7 +235,7 @@ Use [Collator](../reference/apis/js-apis-intl.md#collator8) APIs to sort strings
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. 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 ```js
var options = collator.resolvedOptions(); let options = collator.resolvedOptions();
``` ```
...@@ -261,13 +261,13 @@ Use [PluralRules](../reference/apis/js-apis-intl.md#pluralrules8) APIs to determ ...@@ -261,13 +261,13 @@ Use [PluralRules](../reference/apis/js-apis-intl.md#pluralrules8) APIs to determ
```js ```js
var pluralRules = new intl.PluralRules(); let 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#pluralrulesoptions9). Alternatively, use your own locale and formatting parameters to create a **PluralRules** object. For a full list of parameters, see [PluralRulesOptions](../reference/apis/js-apis-intl.md#pluralrulesoptions9).
```js ```js
var pluralRules = new intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"}); let pluralRules = new intl.PluralRules("zh-CN", {localeMatcher: "best fit", type: "cardinal"});
``` ```
2. Determine the singular-plural type. 2. Determine the singular-plural type.
...@@ -275,8 +275,8 @@ Use [PluralRules](../reference/apis/js-apis-intl.md#pluralrules8) APIs to determ ...@@ -275,8 +275,8 @@ Use [PluralRules](../reference/apis/js-apis-intl.md#pluralrules8) APIs to determ
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**. 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 ```js
var number = 1234.5678 let number = 1234.5678
var categoryResult = plurals.select(number); let categoryResult = plurals.select(number);
``` ```
...@@ -304,13 +304,13 @@ Use [RelativeTimeFormat](../reference/apis/js-apis-intl.md#relativetimeformat8) ...@@ -304,13 +304,13 @@ Use [RelativeTimeFormat](../reference/apis/js-apis-intl.md#relativetimeformat8)
```js ```js
var relativeTimeFormat = new intl.RelativeTimeFormat(); let 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#relativetimeformatinputoptions9). Alternatively, use your own locale and formatting parameters to create a **RelativeTimeFormat** object. Formatting parameters are optional. For a full list of formatting parameters, see [RelativeTimeFormatInputOptions](../reference/apis/js-apis-intl.md#relativetimeformatinputoptions9).
```js ```js
var relativeTimeFormat = new intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"}); let relativeTimeFormat = new intl.RelativeTimeFormat("zh-CN", {numeric: "always", style: "long"});
``` ```
2. Format the relative time. 2. Format the relative time.
...@@ -318,9 +318,9 @@ Use [RelativeTimeFormat](../reference/apis/js-apis-intl.md#relativetimeformat8) ...@@ -318,9 +318,9 @@ Use [RelativeTimeFormat](../reference/apis/js-apis-intl.md#relativetimeformat8)
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. 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 ```js
var number = 2; let number = 2;
var unit = "year" let unit = "year"
var formatResult = relativeTimeFormat.format(number, unit); let formatResult = relativeTimeFormat.format(number, unit);
``` ```
3. Obtain each part of the relative time format. 3. Obtain each part of the relative time format.
...@@ -328,9 +328,9 @@ Use [RelativeTimeFormat](../reference/apis/js-apis-intl.md#relativetimeformat8) ...@@ -328,9 +328,9 @@ Use [RelativeTimeFormat](../reference/apis/js-apis-intl.md#relativetimeformat8)
Upon 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 ```js
var number = 2; let number = 2;
var unit = "year" let unit = "year"
var formatResult = relativeTimeFormat.formatToParts(number, unit); let formatResult = relativeTimeFormat.formatToParts(number, unit);
``` ```
4. Obtain attributes of the **RelativeTimeFormat** object. 4. Obtain attributes of the **RelativeTimeFormat** object.
...@@ -338,7 +338,7 @@ Use [RelativeTimeFormat](../reference/apis/js-apis-intl.md#relativetimeformat8) ...@@ -338,7 +338,7 @@ Use [RelativeTimeFormat](../reference/apis/js-apis-intl.md#relativetimeformat8)
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). 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 ```js
var options = numberFormat.resolvedOptions(); let options = numberFormat.resolvedOptions();
``` ```
## Samples ## Samples
......
# Internationalization – I18N # @ohos.i18n (Internationalization)
The I18N 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 **i18n** 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 [Intl](js-apis-intl.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. The [intl](js-apis-intl.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** > **NOTE**
> 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 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.
>
> - 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 details about the basic i18n capabilities, see [intl](js-apis-intl.md).
## Modules to Import ## Modules to Import
```js ```js
import i18n from '@ohos.i18n'; import I18n from '@ohos.i18n';
``` ```
...@@ -39,16 +42,16 @@ Obtains the localized script for the specified country. ...@@ -39,16 +42,16 @@ Obtains the localized script for the specified country.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
var displayCountry = i18n.System.getDisplayCountry("zh-CN", "en-GB"); let displayCountry = I18n.System.getDisplayCountry("zh-CN", "en-GB"); // displayCountry = "China"
} catch(error) { } catch(error) {
console.error(`call System.getDisplayCountry failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.getDisplayCountry failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -78,16 +81,16 @@ Obtains the localized script for the specified language. ...@@ -78,16 +81,16 @@ Obtains the localized script for the specified language.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
var displayLanguage = i18n.System.getDisplayLanguage("zh", "en-GB"); let displayLanguage = I18n.System.getDisplayLanguage("zh", "en-GB"); // displayLanguage = Chinese
} catch(error) { } catch(error) {
console.error(`call System.getDisplayLanguage failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.getDisplayLanguage failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -109,16 +112,16 @@ Obtains the list of system languages. ...@@ -109,16 +112,16 @@ Obtains the list of system languages.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
var systemLanguages = i18n.System.getSystemLanguages(); let systemLanguages = I18n.System.getSystemLanguages(); // [ "en-Latn-US", "zh-Hans" ]
} catch(error) { } catch(error) {
console.error(`call System.getSystemLanguages failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.getSystemLanguages failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -134,9 +137,9 @@ Obtains the list of countries and regions supported for the specified language. ...@@ -134,9 +137,9 @@ Obtains the list of countries and regions supported for the specified language.
**Parameters** **Parameters**
| Name | Type | Description | | Name | Type | Mandatory | Description |
| -------- | ------ | ----- | | -------- | ------ | ---- | ----- |
| language | string | Language ID.| | language | string | Yes | Language ID.|
**Return value** **Return value**
...@@ -146,16 +149,16 @@ Obtains the list of countries and regions supported for the specified language. ...@@ -146,16 +149,16 @@ Obtains the list of countries and regions supported for the specified language.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
var systemCountries = i18n.System.getSystemCountries('zh'); let systemCountries = I18n.System.getSystemCountries('zh'); // systemCountries = [ "ZW", "YT", "YE", ..., "ER", "CN", "DE" ], 240 countries or regions in total
} catch(error) { } catch(error) {
console.error(`call System.getSystemCountries failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.getSystemCountries failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -184,16 +187,16 @@ Checks whether the system language matches the specified region. ...@@ -184,16 +187,16 @@ Checks whether the system language matches the specified region.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
var res = i18n.System.isSuggested('zh', 'CN'); let res = I18n.System.isSuggested('zh', 'CN'); // res = true
} catch(error) { } catch(error) {
console.error(`call System.isSuggested failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.isSuggested failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -215,16 +218,16 @@ Obtains the system language. ...@@ -215,16 +218,16 @@ Obtains the system language.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
var systemLanguage = i18n.System.getSystemLanguage(); let systemLanguage = I18n.System.getSystemLanguage(); // systemLanguage indicates the current system language.
} catch(error) { } catch(error) {
console.error(`call System.getSystemLanguage failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.getSystemLanguage failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -236,7 +239,7 @@ static setSystemLanguage(language: string): void ...@@ -236,7 +239,7 @@ static setSystemLanguage(language: string): void
Sets the system language. Currently, this API does not support real-time updating of the system language. Sets the system language. Currently, this API does not support real-time updating of the system language.
This is a system API. **System API**: This is a system API.
**Permission required**: ohos.permission.UPDATE_CONFIGURATION **Permission required**: ohos.permission.UPDATE_CONFIGURATION
...@@ -245,21 +248,21 @@ This is a system API. ...@@ -245,21 +248,21 @@ This is a system API.
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ------ | ----- | ----- | | -------- | ------ | ---- | ----- |
| language | string | Yes | Language ID.| | language | string | Yes | Language ID.|
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
i18n.System.setSystemLanguage('zh'); I18n.System.setSystemLanguage('zh'); // Set the current system language to zh.
} catch(error) { } catch(error) {
console.error(`call System.setSystemLanguage failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.setSystemLanguage failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -281,16 +284,16 @@ Obtains the system region. ...@@ -281,16 +284,16 @@ Obtains the system region.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
var systemRegion = i18n.System.getSystemRegion(); let systemRegion = I18n.System.getSystemRegion(); // Obtain the current system region.
} catch(error) { } catch(error) {
console.error(`call System.getSystemRegion failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.getSystemRegion failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -302,7 +305,7 @@ static setSystemRegion(region: string): void ...@@ -302,7 +305,7 @@ static setSystemRegion(region: string): void
Sets the system region. Sets the system region.
This is a system API. **System API**: This is a system API.
**Permission required**: ohos.permission.UPDATE_CONFIGURATION **Permission required**: ohos.permission.UPDATE_CONFIGURATION
...@@ -311,21 +314,21 @@ This is a system API. ...@@ -311,21 +314,21 @@ This is a system API.
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ------ | ----- | ----- | | ------ | ------ | ---- | ----- |
| region | string | Yes | Region ID.| | region | string | Yes | Region ID.|
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
i18n.System.setSystemRegion('CN'); I18n.System.setSystemRegion('CN'); // Set the current system region to CN.
} catch(error) { } catch(error) {
console.error(`call System.setSystemRegion failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.setSystemRegion failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -347,16 +350,16 @@ Obtains the system locale. ...@@ -347,16 +350,16 @@ Obtains the system locale.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
var systemLocale = i18n.System.getSystemLocale(); let systemLocale = I18n.System.getSystemLocale(); // Obtain the current system locale.
} catch(error) { } catch(error) {
console.error(`call System.getSystemLocale failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.getSystemLocale failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -368,7 +371,7 @@ static setSystemLocale(locale: string): void ...@@ -368,7 +371,7 @@ static setSystemLocale(locale: string): void
Sets the system locale. Sets the system locale.
This is a system API. **System API**: This is a system API.
**Permission required**: ohos.permission.UPDATE_CONFIGURATION **Permission required**: ohos.permission.UPDATE_CONFIGURATION
...@@ -377,21 +380,21 @@ This is a system API. ...@@ -377,21 +380,21 @@ This is a system API.
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ------ | ----- | ----- | | ------ | ------ | ---- | --------------- |
| locale | string | Yes | System locale ID, for example, **zh-CN**.| | locale | string | Yes | System locale ID, for example, **zh-CN**.|
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
i18n.System.setSystemLocale('zh-CN'); I18n.System.setSystemLocale('zh-CN'); // Set the current system locale to zh-CN.
} catch(error) { } catch(error) {
console.error(`call System.setSystemLocale failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.setSystemLocale failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -413,16 +416,16 @@ Checks whether the 24-hour clock is used. ...@@ -413,16 +416,16 @@ Checks whether the 24-hour clock is used.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
var is24HourClock = i18n.System.is24HourClock(); let is24HourClock = I18n.System.is24HourClock(); // Check whether the 24-hour clock is enabled.
} catch(error) { } catch(error) {
console.error(`call System.is24HourClock failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.is24HourClock failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -434,7 +437,7 @@ static set24HourClock(option: boolean): void ...@@ -434,7 +437,7 @@ static set24HourClock(option: boolean): void
Sets the 24-hour clock. Sets the 24-hour clock.
This is a system API. **System API**: This is a system API.
**Permission required**: ohos.permission.UPDATE_CONFIGURATION **Permission required**: ohos.permission.UPDATE_CONFIGURATION
...@@ -448,17 +451,17 @@ This is a system API. ...@@ -448,17 +451,17 @@ This is a system API.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
// Set the system time to the 24-hour clock. // Set the system time to the 24-hour clock.
try { try {
i18n.System.set24HourClock(true); I18n.System.set24HourClock(true);
} catch(error) { } catch(error) {
console.error(`call System.set24HourClock failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.set24HourClock failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -470,7 +473,7 @@ static addPreferredLanguage(language: string, index?: number): void ...@@ -470,7 +473,7 @@ static addPreferredLanguage(language: string, index?: number): void
Adds a preferred language to the specified position on the preferred language list. Adds a preferred language to the specified position on the preferred language list.
This is a system API. **System API**: This is a system API.
**Permission required**: ohos.permission.UPDATE_CONFIGURATION **Permission required**: ohos.permission.UPDATE_CONFIGURATION
...@@ -485,19 +488,19 @@ This is a system API. ...@@ -485,19 +488,19 @@ This is a system API.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
// Add zh-CN to the preferred language list. // Add zh-CN to the preferred language list.
var language = 'zh-CN'; let language = 'zh-CN';
var index = 0; let index = 0;
try { try {
i18n.System.addPreferredLanguage(language, index); I18n.System.addPreferredLanguage(language, index); // Add zh-CN to the first place in the preferred language list.
} catch(error) { } catch(error) {
console.error(`call System.addPreferredLanguage failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.addPreferredLanguage failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -509,7 +512,7 @@ static removePreferredLanguage(index: number): void ...@@ -509,7 +512,7 @@ static removePreferredLanguage(index: number): void
Deletes a preferred language from the specified position on the preferred language list. Deletes a preferred language from the specified position on the preferred language list.
This is a system API. **System API**: This is a system API.
**Permission required**: ohos.permission.UPDATE_CONFIGURATION **Permission required**: ohos.permission.UPDATE_CONFIGURATION
...@@ -523,18 +526,18 @@ This is a system API. ...@@ -523,18 +526,18 @@ This is a system API.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
// Delete the first preferred language from the preferred language list. // Delete the first preferred language from the preferred language list.
var index = 0; let index = 0;
try { try {
i18n.System.removePreferredLanguage(index); I18n.System.removePreferredLanguage(index);
} catch(error) { } catch(error) {
console.error(`call System.removePreferredLanguage failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.removePreferredLanguage failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -544,7 +547,7 @@ For details about the error codes, see [I18N Error Codes](../errorcodes/errorcod ...@@ -544,7 +547,7 @@ For details about the error codes, see [I18N Error Codes](../errorcodes/errorcod
static getPreferredLanguageList(): Array&lt;string&gt; static getPreferredLanguageList(): Array&lt;string&gt;
Obtains the list of preferred languages. Obtains the preferred language list.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
...@@ -552,20 +555,20 @@ Obtains the list of preferred languages. ...@@ -552,20 +555,20 @@ Obtains the list of preferred languages.
| Type | Description | | Type | Description |
| ------------------- | --------- | | ------------------- | --------- |
| Array&lt;string&gt; | List of preferred languages.| | Array&lt;string&gt; | Preferred language list.|
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
var preferredLanguageList = i18n.System.getPreferredLanguageList(); let preferredLanguageList = I18n.System.getPreferredLanguageList(); // Obtain the current preferred language list.
} catch(error) { } catch(error) {
console.error(`call System.getPreferredLanguageList failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.getPreferredLanguageList failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -587,16 +590,16 @@ Obtains the first language in the preferred language list. ...@@ -587,16 +590,16 @@ Obtains the first language in the preferred language list.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
var firstPreferredLanguage = i18n.System.getFirstPreferredLanguage(); let firstPreferredLanguage = I18n.System.getFirstPreferredLanguage(); // Obtain the first language in the preferred language list.
} catch(error) { } catch(error) {
console.error(`call System.getFirstPreferredLanguage failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.getFirstPreferredLanguage failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -618,16 +621,16 @@ Obtains the preferred language of an application. ...@@ -618,16 +621,16 @@ Obtains the preferred language of an application.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```js ```js
try { try {
var appPreferredLanguage = i18n.System.getAppPreferredLanguage(); let appPreferredLanguage = I18n.System.getAppPreferredLanguage(); // Obtain the preferred language of an application.
} catch(error) { } catch(error) {
console.error(`call System.getAppPreferredLanguage failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.getAppPreferredLanguage failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -637,9 +640,9 @@ For details about the error codes, see [I18N Error Codes](../errorcodes/errorcod ...@@ -637,9 +640,9 @@ For details about the error codes, see [I18N Error Codes](../errorcodes/errorcod
static setUsingLocalDigit(flag: boolean): void static setUsingLocalDigit(flag: boolean): void
Sets whether to turn on the local digit switch. Sets whether to enable the local digit switch.
This is a system API. **System API**: This is a system API.
**Permission required**: ohos.permission.UPDATE_CONFIGURATION **Permission required**: ohos.permission.UPDATE_CONFIGURATION
...@@ -649,20 +652,20 @@ This is a system API. ...@@ -649,20 +652,20 @@ This is a system API.
| Name | Type | Mandatory | Description | | 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.| | flag | boolean | Yes | Whether to enable the local digit switch. The value **true** means to enable the local digit switch, and the value **false** indicates the opposite.|
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```ts ```ts
try { try {
i18n.System.setUsingLocalDigit(true); I18n.System.setUsingLocalDigit(true); // Enable the local digit switch.
} catch(error) { } catch(error) {
console.error(`call System.setUsingLocalDigit failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.setUsingLocalDigit failed, error code: ${error.code}, message: ${error.message}.`)
} }
...@@ -684,23 +687,23 @@ Checks whether the local digit switch is turned on. ...@@ -684,23 +687,23 @@ Checks whether the local digit switch is turned on.
**Error codes** **Error codes**
For details about the error codes, see [I18N Error Codes](../errorcodes/errorcode-i18n.md). For details about the error codes, see [i18n Error Codes](../errorcodes/errorcode-i18n.md).
| ID| Error Message| | ID | Error Message |
| -------- | ---------------------------------------- | | ------ | ---------------------- |
| 890001 | Unspported para value. | | 890001 | Unspported para value. |
**Example** **Example**
```ts ```ts
try { try {
var status = i18n.System.getUsingLocalDigit(); let status = I18n.System.getUsingLocalDigit(); // Check whether the local digit switch is enabled.
} catch(error) { } catch(error) {
console.error(`call System.getUsingLocalDigit failed, error code: ${error.code}, message: ${error.message}.`) console.error(`call System.getUsingLocalDigit failed, error code: ${error.code}, message: ${error.message}.`)
} }
``` ```
## i18n.isRTL<sup>7+</sup> ## I18n.isRTL<sup>7+</sup>
isRTL(locale: string): boolean isRTL(locale: string): boolean
...@@ -711,7 +714,7 @@ Checks whether the localized script for the specified language is displayed from ...@@ -711,7 +714,7 @@ Checks whether the localized script for the specified language is displayed from
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ------ | ----- | ----- | | ------ | ------ | ---- | ------- |
| locale | string | Yes | Locale ID.| | locale | string | Yes | Locale ID.|
**Return value** **Return value**
...@@ -727,7 +730,7 @@ Checks whether the localized script for the specified language is displayed from ...@@ -727,7 +730,7 @@ Checks whether the localized script for the specified language is displayed from
``` ```
## i18n.getCalendar<sup>8+</sup> ## I18n.getCalendar<sup>8+</sup>
getCalendar(locale: string, type? : string): Calendar getCalendar(locale: string, type? : string): Calendar
...@@ -750,7 +753,7 @@ Obtains a **Calendar** object. ...@@ -750,7 +753,7 @@ Obtains a **Calendar** object.
**Example** **Example**
```js ```js
i18n.getCalendar("zh-Hans", "gregory"); I18n.getCalendar("zh-Hans", "chinese"); // Obtain the Calendar object for the Chinese lunar calendar.
``` ```
...@@ -773,8 +776,8 @@ Sets the date for this **Calendar** object. ...@@ -773,8 +776,8 @@ Sets the date for this **Calendar** object.
**Example** **Example**
```js ```js
var calendar = i18n.getCalendar("en-US", "gregory"); let calendar = I18n.getCalendar("en-US", "gregory");
var date = new Date(2021, 10, 7, 8, 0, 0, 0); let date = new Date(2021, 10, 7, 8, 0, 0, 0);
calendar.setTime(date); calendar.setTime(date);
``` ```
...@@ -795,7 +798,7 @@ Sets the date and time for this **Calendar** object. The value is represented by ...@@ -795,7 +798,7 @@ Sets the date and time for this **Calendar** object. The value is represented by
**Example** **Example**
```js ```js
var calendar = i18n.getCalendar("en-US", "gregory"); let calendar = I18n.getCalendar("en-US", "gregory");
calendar.setTime(10540800000); calendar.setTime(10540800000);
``` ```
...@@ -821,7 +824,7 @@ Sets the year, month, day, hour, minute, and second for this **Calendar** object ...@@ -821,7 +824,7 @@ Sets the year, month, day, hour, minute, and second for this **Calendar** object
**Example** **Example**
```js ```js
var calendar = i18n.getCalendar("zh-Hans"); let calendar = I18n.getCalendar("zh-Hans");
calendar.set(2021, 10, 1, 8, 0, 0); // set time to 2021.10.1 08:00:00 calendar.set(2021, 10, 1, 8, 0, 0); // set time to 2021.10.1 08:00:00
``` ```
...@@ -842,7 +845,7 @@ Sets the time zone of this **Calendar** object. ...@@ -842,7 +845,7 @@ Sets the time zone of this **Calendar** object.
**Example** **Example**
```js ```js
var calendar = i18n.getCalendar("zh-Hans"); let calendar = I18n.getCalendar("zh-Hans");
calendar.setTimeZone("Asia/Shanghai"); calendar.setTimeZone("Asia/Shanghai");
``` ```
...@@ -863,9 +866,9 @@ Obtains the time zone of this **Calendar** object. ...@@ -863,9 +866,9 @@ Obtains the time zone of this **Calendar** object.
**Example** **Example**
```js ```js
var calendar = i18n.getCalendar("zh-Hans"); let calendar = I18n.getCalendar("zh-Hans");
calendar.setTimeZone("Asia/Shanghai"); calendar.setTimeZone("Asia/Shanghai");
calendar.getTimeZone(); // Asia/Shanghai" let timezone = calendar.getTimeZone(); // timezone = "Asia/Shanghai"
``` ```
...@@ -885,8 +888,8 @@ Obtains the start day of a week for this **Calendar** object. ...@@ -885,8 +888,8 @@ Obtains the start day of a week for this **Calendar** object.
**Example** **Example**
```js ```js
var calendar = i18n.getCalendar("en-US", "gregory"); let calendar = I18n.getCalendar("en-US", "gregory");
calendar.getFirstDayOfWeek(); let firstDayOfWeek = calendar.getFirstDayOfWeek(); // firstDayOfWeek = 1
``` ```
...@@ -906,8 +909,9 @@ Sets the start day of a week for this **Calendar** object. ...@@ -906,8 +909,9 @@ Sets the start day of a week for this **Calendar** object.
**Example** **Example**
```js ```js
var calendar = i18n.getCalendar("zh-Hans"); let calendar = I18n.getCalendar("zh-Hans");
calendar.setFirstDayOfWeek(0); calendar.setFirstDayOfWeek(3);
let firstDayOfWeek = calendar.getFirstDayOfWeek(); // firstDayOfWeek = 3
``` ```
...@@ -927,8 +931,8 @@ Obtains the minimum number of days in the first week of a year. ...@@ -927,8 +931,8 @@ Obtains the minimum number of days in the first week of a year.
**Example** **Example**
```js ```js
var calendar = i18n.getCalendar("zh-Hans"); let calendar = I18n.getCalendar("zh-Hans");
calendar.getMinimalDaysInFirstWeek(); let minimalDaysInFirstWeek = calendar.getMinimalDaysInFirstWeek(); // minimalDaysInFirstWeek = 1
``` ```
...@@ -948,8 +952,9 @@ Sets the minimum number of days in the first week of a year. ...@@ -948,8 +952,9 @@ Sets the minimum number of days in the first week of a year.
**Example** **Example**
```js ```js
var calendar = i18n.getCalendar("zh-Hans"); let calendar = I18n.getCalendar("zh-Hans");
calendar.setMinimalDaysInFirstWeek(3); calendar.setMinimalDaysInFirstWeek(3);
let minimalDaysInFirstWeek = calendar.getMinimalDaysInFirstWeek(); // minimalDaysInFirstWeek = 3
``` ```
...@@ -975,9 +980,9 @@ Obtains the value of the specified field in the **Calendar** object. ...@@ -975,9 +980,9 @@ Obtains the value of the specified field in the **Calendar** object.
**Example** **Example**
```js ```js
var calendar = i18n.getCalendar("zh-Hans"); let calendar = I18n.getCalendar("zh-Hans");
calendar.set(2021, 10, 1, 8, 0, 0); // set time to 2021.10.1 08:00:00 calendar.set(2021, 10, 1, 8, 0, 0); // set time to 2021.10.1 08:00:00
calendar.get("hour_of_day"); // 8 let hourOfDay = calendar.get("hour_of_day"); // hourOfDay = 8
``` ```
...@@ -1003,8 +1008,8 @@ Obtains the **Calendar** object name displayed for the specified locale. ...@@ -1003,8 +1008,8 @@ Obtains the **Calendar** object name displayed for the specified locale.
**Example** **Example**
```js ```js
var calendar = i18n.getCalendar("en-US", "buddhist"); let calendar = I18n.getCalendar("en-US", "buddhist");
calendar.getDisplayName("zh"); // Obtain the name of the Buddhist calendar in zh. let calendarName = calendar.getDisplayName("zh"); // calendarName = "Buddhist Calendar"
``` ```
...@@ -1030,10 +1035,10 @@ Checks whether the specified date in this **Calendar** object is a weekend. ...@@ -1030,10 +1035,10 @@ Checks whether the specified date in this **Calendar** object is a weekend.
**Example** **Example**
```js ```js
var calendar = i18n.getCalendar("zh-Hans"); let calendar = I18n.getCalendar("zh-Hans");
calendar.set(2021, 11, 11, 8, 0, 0); // set time to 2021.11.11 08:00:00 calendar.set(2021, 11, 11, 8, 0, 0); // set time to 2021.11.11 08:00:00
calendar.isWeekend(); // false calendar.isWeekend(); // false
var date = new Date(2011, 11, 6, 9, 0, 0); let date = new Date(2011, 11, 6, 9, 0, 0);
calendar.isWeekend(date); // true calendar.isWeekend(date); // true
``` ```
...@@ -1054,11 +1059,11 @@ Creates a **PhoneNumberFormat** object. ...@@ -1054,11 +1059,11 @@ Creates a **PhoneNumberFormat** object.
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | ---------------------------------------- | ---- | ---------------- | | ------- | ---------------------------------------- | ---- | ---------------- |
| country | string | Yes | Country or region to which the phone number to be formatted belongs.| | country | string | Yes | Country or region to which the phone number to be formatted belongs.|
| options | [PhoneNumberFormatOptions](#phonenumberformatoptions9) | No | Options of the **PhoneNumberFormat** object. | | options | [PhoneNumberFormatOptions](#phonenumberformatoptions8) | No | Options of the **PhoneNumberFormat** object. |
**Example** **Example**
```js ```js
var phoneNumberFormat= new i18n.PhoneNumberFormat("CN", {"type": "E164"}); let phoneNumberFormat= new I18n.PhoneNumberFormat("CN", {"type": "E164"});
``` ```
...@@ -1084,8 +1089,8 @@ Checks whether the format of the specified phone number is valid. ...@@ -1084,8 +1089,8 @@ Checks whether the format of the specified phone number is valid.
**Example** **Example**
```js ```js
var phonenumberfmt = new i18n.PhoneNumberFormat("CN"); let phonenumberfmt = new I18n.PhoneNumberFormat("CN");
phonenumberfmt.isValidNumber("15812312312"); let isValidNumber = phonenumberfmt.isValidNumber("15812312312"); // isValidNumber = true
``` ```
...@@ -1111,8 +1116,8 @@ Formats a phone number. ...@@ -1111,8 +1116,8 @@ Formats a phone number.
**Example** **Example**
```js ```js
var phonenumberfmt = new i18n.PhoneNumberFormat("CN"); let phonenumberfmt = new I18n.PhoneNumberFormat("CN");
phonenumberfmt.format("15812312312"); let formattedPhoneNumber = phonenumberfmt.format("15812312312"); // formattedPhoneNumber = "158 1231 2312"
``` ```
...@@ -1139,12 +1144,12 @@ Obtains the home location of a phone number. ...@@ -1139,12 +1144,12 @@ Obtains the home location of a phone number.
**Example** **Example**
```js ```js
var phonenumberfmt = new i18n.PhoneNumberFormat("CN"); let phonenumberfmt = new I18n.PhoneNumberFormat("CN");
phonenumberfmt.getLocationName("15812312345", "zh-CN"); let locationName = phonenumberfmt.getLocationName("15812312345", "zh-CN"); // locationName = "Zhanjiang, Guangdong Province"
``` ```
## PhoneNumberFormatOptions<sup>9+</sup> ## PhoneNumberFormatOptions<sup>8+</sup>
Defines the options for this PhoneNumberFormat object. Defines the options for this PhoneNumberFormat object.
...@@ -1189,7 +1194,7 @@ Creates an **IndexUtil** object. ...@@ -1189,7 +1194,7 @@ Creates an **IndexUtil** object.
**Example** **Example**
```js ```js
var indexUtil= i18n.getInstance("zh-CN"); let indexUtil= I18n.getInstance("zh-CN");
``` ```
...@@ -1212,8 +1217,10 @@ Obtains the index list for this **locale** object. ...@@ -1212,8 +1217,10 @@ Obtains the index list for this **locale** object.
**Example** **Example**
```js ```js
var indexUtil = i18n.getInstance("zh-CN"); let indexUtil = I18n.getInstance("zh-CN");
var indexList = indexUtil.getIndexList(); // indexList = [ "...", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
// "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "..." ]
let indexList = indexUtil.getIndexList();
``` ```
...@@ -1233,7 +1240,7 @@ Adds the index of the new **locale** object to the index list. ...@@ -1233,7 +1240,7 @@ Adds the index of the new **locale** object to the index list.
**Example** **Example**
```js ```js
var indexUtil = i18n.getInstance("zh-CN"); let indexUtil = I18n.getInstance("zh-CN");
indexUtil.addLocale("en-US"); indexUtil.addLocale("en-US");
``` ```
...@@ -1260,12 +1267,12 @@ Obtains the index of a text object. ...@@ -1260,12 +1267,12 @@ Obtains the index of a text object.
**Example** **Example**
```js ```js
var indexUtil= i18n.getInstance("zh-CN"); let indexUtil= I18n.getInstance("zh-CN");
indexUtil.getIndex("hi"); // Return hi. let index = indexUtil.getIndex("hi"); // index = "H"
``` ```
## i18n.getLineInstance<sup>8+</sup> ## I18n.getLineInstance<sup>8+</sup>
getLineInstance(locale: string): BreakIterator getLineInstance(locale: string): BreakIterator
...@@ -1287,7 +1294,7 @@ Obtains a [BreakIterator](#breakiterator8) object for text segmentation. ...@@ -1287,7 +1294,7 @@ Obtains a [BreakIterator](#breakiterator8) object for text segmentation.
**Example** **Example**
```js ```js
var iterator = i18n.getLineInstance("en"); let iterator = I18n.getLineInstance("en");
``` ```
...@@ -1310,8 +1317,8 @@ Sets the text to be processed by the [BreakIterator](#breakiterator8) object. ...@@ -1310,8 +1317,8 @@ Sets the text to be processed by the [BreakIterator](#breakiterator8) object.
**Example** **Example**
```js ```js
var iterator = i18n.getLineInstance("en"); let iterator = I18n.getLineInstance("en");
iterator.setLineBreakText("Apple is my favorite fruit."); iterator.setLineBreakText("Apple is my favorite fruit ."); // Set a short sentence as the text to be processed by the BreakIterator object.
``` ```
...@@ -1331,9 +1338,9 @@ Obtains the text being processed by the [BreakIterator](#breakiterator8) object. ...@@ -1331,9 +1338,9 @@ Obtains the text being processed by the [BreakIterator](#breakiterator8) object.
**Example** **Example**
```js ```js
var iterator = i18n.getLineInstance("en"); let iterator = I18n.getLineInstance("en");
iterator.setLineBreakText("Apple is my favorite fruit."); iterator.setLineBreakText("Apple is my favorite fruit.");
iterator.getLineBreakText(); // Apple is my favorite fruit. let breakText = iterator.getLineBreakText(); // breakText = "Apple is my favorite fruit."
``` ```
...@@ -1353,9 +1360,9 @@ Obtains the position of the [BreakIterator](#breakiterator8) object in the text ...@@ -1353,9 +1360,9 @@ Obtains the position of the [BreakIterator](#breakiterator8) object in the text
**Example** **Example**
```js ```js
var iterator = i18n.getLineInstance("en"); let iterator = I18n.getLineInstance("en");
iterator.setLineBreakText("Apple is my favorite fruit."); iterator.setLineBreakText("Apple is my favorite fruit.");
iterator.current(); // 0 let currentPos = iterator.current(); // currentPos = 0
``` ```
...@@ -1375,9 +1382,9 @@ Puts the [BreakIterator](#breakiterator8) object to the first text boundary, whi ...@@ -1375,9 +1382,9 @@ Puts the [BreakIterator](#breakiterator8) object to the first text boundary, whi
**Example** **Example**
```js ```js
var iterator = i18n.getLineInstance("en"); let iterator = i18n.getLineInstance("en");
iterator.setLineBreakText("Apple is my favorite fruit."); iterator.setLineBreakText("Apple is my favorite fruit.");
iterator.first(); // 0 let firstPos = iterator.first(); // firstPos = 0
``` ```
...@@ -1397,9 +1404,9 @@ Puts the [BreakIterator](#breakiterator8) object to the last text boundary, whic ...@@ -1397,9 +1404,9 @@ Puts the [BreakIterator](#breakiterator8) object to the last text boundary, whic
**Example** **Example**
```js ```js
var iterator = i18n.getLineInstance("en"); let iterator = I18n.getLineInstance("en");
iterator.setLineBreakText("Apple is my favorite fruit."); iterator.setLineBreakText("Apple is my favorite fruit.");
iterator.last(); // 27 let lastPos = iterator.last(); // lastPos = 27
``` ```
...@@ -1425,11 +1432,11 @@ Moves the [BreakIterator](#breakiterator8) object backward by the specified numb ...@@ -1425,11 +1432,11 @@ Moves the [BreakIterator](#breakiterator8) object backward by the specified numb
**Example** **Example**
```js ```js
var iterator = i18n.getLineInstance("en"); let iterator = I18n.getLineInstance("en");
iterator.setLineBreakText("Apple is my favorite fruit."); iterator.setLineBreakText("Apple is my favorite fruit.");
iterator.first(); // 0 let pos = iterator.first(); // pos = 0
iterator.next(); // 6 pos = iterator.next(); // pos = 6
iterator.next(10); // -1 pos = iterator.next(10); // pos = -1
``` ```
...@@ -1449,11 +1456,11 @@ Moves the [BreakIterator](#breakiterator8) object to the previous text boundary. ...@@ -1449,11 +1456,11 @@ Moves the [BreakIterator](#breakiterator8) object to the previous text boundary.
**Example** **Example**
```js ```js
var iterator = i18n.getLineInstance("en"); let iterator = I18n.getLineInstance("en");
iterator.setLineBreakText("Apple is my favorite fruit."); iterator.setLineBreakText("Apple is my favorite fruit.");
iterator.first(); // 0 let pos = iterator.first(); // pos = 0
iterator.next(3); // 12 pos = iterator.next(3); // pos = 12
iterator.previous(); // 9 pos = iterator.previous(); // pos = 9
``` ```
...@@ -1479,11 +1486,11 @@ Moves the [BreakIterator](#breakiterator8) object to the text boundary after the ...@@ -1479,11 +1486,11 @@ Moves the [BreakIterator](#breakiterator8) object to the text boundary after the
**Example** **Example**
```js ```js
var iterator = i18n.getLineInstance("en"); let iterator = I18n.getLineInstance("en");
iterator.setLineBreakText("Apple is my favorite fruit."); iterator.setLineBreakText("Apple is my favorite fruit.");
iterator.following(0); // 6 let pos = iterator.following(0); // pos = 6
iterator.following(100); // -1 pos = iterator.following(100); // pos = -1
iterator.current(); // 27 pos = iterator.current(); // pos = 27
``` ```
...@@ -1509,14 +1516,14 @@ Checks whether the position specified by the offset is a text boundary. If **tru ...@@ -1509,14 +1516,14 @@ Checks whether the position specified by the offset is a text boundary. If **tru
**Example** **Example**
```js ```js
var iterator = i18n.getLineInstance("en"); let iterator = I18n.getLineInstance("en");
iterator.setLineBreakText("Apple is my favorite fruit."); iterator.setLineBreakText("Apple is my favorite fruit.");
iterator.isBoundary(0); // true; let isBoundary = iterator.isBoundary(0); // isBoundary = true;
iterator.isBoundary(5); // false; isBoundary = iterator.isBoundary(5); // isBoundary = false;
``` ```
## i18n.getTimeZone<sup>7+</sup> ## I18n.getTimeZone<sup>7+</sup>
getTimeZone(zoneID?: string): TimeZone getTimeZone(zoneID?: string): TimeZone
...@@ -1538,7 +1545,7 @@ Obtains the **TimeZone** object corresponding to the specified time zone ID. ...@@ -1538,7 +1545,7 @@ Obtains the **TimeZone** object corresponding to the specified time zone ID.
**Example** **Example**
```js ```js
var timezone = i18n.getTimeZone(); let timezone = I18n.getTimeZone();
``` ```
...@@ -1561,8 +1568,8 @@ Obtains the ID of the specified **TimeZone** object. ...@@ -1561,8 +1568,8 @@ Obtains the ID of the specified **TimeZone** object.
**Example** **Example**
```js ```js
var timezone = i18n.getTimeZone(); let timezone = I18n.getTimeZone();
timezone.getID(); let timezoneID = timezone.getID(); // timezoneID = "Asia/Shanghai"
``` ```
...@@ -1570,7 +1577,7 @@ Obtains the ID of the specified **TimeZone** object. ...@@ -1570,7 +1577,7 @@ Obtains the ID of the specified **TimeZone** object.
getDisplayName(locale?: string, isDST?: boolean): string getDisplayName(locale?: string, isDST?: boolean): string
Obtains the representation of a **TimeZone** object in the specified locale. Obtains the localized representation of the time zone.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
...@@ -1589,8 +1596,8 @@ Obtains the representation of a **TimeZone** object in the specified locale. ...@@ -1589,8 +1596,8 @@ Obtains the representation of a **TimeZone** object in the specified locale.
**Example** **Example**
```js ```js
var timezone = i18n.getTimeZone(); let timezone = I18n.getTimeZone();
timezone.getDisplayName("zh-CN", false); let timezoneName = timezone.getDisplayName("zh-CN", false); // timezoneName = "China Standard Time"
``` ```
...@@ -1610,8 +1617,8 @@ Obtains the offset between the time zone represented by a **TimeZone** object an ...@@ -1610,8 +1617,8 @@ Obtains the offset between the time zone represented by a **TimeZone** object an
**Example** **Example**
```js ```js
var timezone = i18n.getTimeZone(); let timezone = I18n.getTimeZone();
timezone.getRawOffset(); let offset = timezone.getRawOffset(); // offset = 28800000
``` ```
...@@ -1631,8 +1638,8 @@ Obtains the offset between the time zone represented by a **TimeZone** object an ...@@ -1631,8 +1638,8 @@ Obtains the offset between the time zone represented by a **TimeZone** object an
**Example** **Example**
```js ```js
var timezone = i18n.getTimeZone(); let timezone = I18n.getTimeZone();
timezone.getOffset(1234567890); let offset = timezone.getOffset(1234567890); // offset = 28800000
``` ```
...@@ -1652,7 +1659,8 @@ Obtains the list of time zone IDs supported by the system. ...@@ -1652,7 +1659,8 @@ Obtains the list of time zone IDs supported by the system.
**Example** **Example**
```ts ```ts
var ids = i18n.TimeZone.getAvailableIDs(); // ids = ["America/Adak", "America/Anchorage", "America/Bogota", "America/Denver", "America/Los_Angeles", "America/Montevideo", "America/Santiago", "America/Sao_Paulo", "Asia/Ashgabat", "Asia/Hovd", "Asia/Jerusalem", "Asia/Magadan", "Asia/Omsk", "Asia/Shanghai", "Asia/Tokyo", "Asia/Yerevan", "Atlantic/Cape_Verde", "Australia/Lord_Howe", "Europe/Dublin", "Europe/London", "Europe/Moscow", "Pacific/Auckland", "Pacific/Easter", "Pacific/Pago-Pago"], 24 time zones supported in total
let ids = I18n.TimeZone.getAvailableIDs();
``` ```
...@@ -1672,7 +1680,8 @@ Obtains the list of time zone city IDs supported by the system. ...@@ -1672,7 +1680,8 @@ Obtains the list of time zone city IDs supported by the system.
**Example** **Example**
```ts ```ts
var cityIDs = i18n.TimeZone.getAvailableZoneCityIDs(); // cityIDs = ["Auckland", "Magadan", "Lord Howe Island", "Tokyo", "Shanghai", "Hovd", "Omsk", "Ashgabat", "Yerevan", "Moscow", "Tel Aviv", "Dublin", "London", "Praia", "Montevideo", "Brasília", "Santiago", "Bogotá", "Easter Island", "Salt Lake City", "Los Angeles", "Anchorage", "Adak", "Pago Pago"], 24 time zone cities supported in total
let cityIDs = I18n.TimeZone.getAvailableZoneCityIDs();
``` ```
...@@ -1680,7 +1689,7 @@ Obtains the list of time zone city IDs supported by the system. ...@@ -1680,7 +1689,7 @@ Obtains the list of time zone city IDs supported by the system.
static getCityDisplayName(cityID: string, locale: string): string static getCityDisplayName(cityID: string, locale: string): string
Obtains the localized display of a time zone city in the specified locale. Obtains the localized representation of a time zone city in the specified locale.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
...@@ -1699,7 +1708,7 @@ Obtains the localized display of a time zone city in the specified locale. ...@@ -1699,7 +1708,7 @@ Obtains the localized display of a time zone city in the specified locale.
**Example** **Example**
```ts ```ts
var displayName = i18n.TimeZone.getCityDisplayName("Shanghai", "zh-CN"); let displayName = I18n.TimeZone.getCityDisplayName("Shanghai", "zh-CN"); // displayName = "Shanghai (China)"
``` ```
...@@ -1725,7 +1734,7 @@ Obtains the **TimeZone** object corresponding to the specified time zone city ID ...@@ -1725,7 +1734,7 @@ Obtains the **TimeZone** object corresponding to the specified time zone city ID
**Example** **Example**
```ts ```ts
var timezone = i18n.TimeZone.getTimezoneFromCity("Shanghai"); let timezone = I18n.TimeZone.getTimezoneFromCity("Shanghai");
``` ```
...@@ -1748,7 +1757,9 @@ Obtains a list of IDs supported by the **Transliterator** object. ...@@ -1748,7 +1757,9 @@ Obtains a list of IDs supported by the **Transliterator** object.
**Example** **Example**
```ts ```ts
i18n.Transliterator.getAvailableIDs(); // ids = ["ASCII-Latin", "Accents-Any", "Amharic-Latin/BGN", ...], 671 IDs supported in total
// Each ID consists of two parts separated by a hyphen (-). The format is source-destination.
let ids = I18n.Transliterator.getAvailableIDs();
``` ```
...@@ -1774,7 +1785,7 @@ Creates a **Transliterator** object. ...@@ -1774,7 +1785,7 @@ Creates a **Transliterator** object.
**Example** **Example**
```ts ```ts
var transliterator = i18n.Transliterator.getInstance("Any-Latn"); let transliterator = I18n.Transliterator.getInstance("Any-Latn");
``` ```
...@@ -1800,8 +1811,8 @@ Converts the input string from the source format to the target format. ...@@ -1800,8 +1811,8 @@ Converts the input string from the source format to the target format.
**Example** **Example**
```ts ```ts
var transliterator = i18n.Transliterator.getInstance("Any-Latn"); let transliterator = I18n.Transliterator.getInstance("Any-Latn");
transliterator.transform ("China"); let res = transliterator.transform("China"); // res = "zhōng guó"
``` ```
...@@ -1830,7 +1841,7 @@ Checks whether the input character string is composed of digits. ...@@ -1830,7 +1841,7 @@ Checks whether the input character string is composed of digits.
**Example** **Example**
```js ```js
var isdigit = i18n.Unicode.isDigit("1"); // Return true. let isdigit = I18n.Unicode.isDigit("1"); // isdigit = true
``` ```
...@@ -1856,7 +1867,7 @@ Checks whether the input character is a space. ...@@ -1856,7 +1867,7 @@ Checks whether the input character is a space.
**Example** **Example**
```js ```js
var isspacechar = i18n.Unicode.isSpaceChar("a"); // Return false. let isspacechar = I18n.Unicode.isSpaceChar("a"); // isspacechar = false
``` ```
...@@ -1882,7 +1893,7 @@ Checks whether the input character is a white space. ...@@ -1882,7 +1893,7 @@ Checks whether the input character is a white space.
**Example** **Example**
```js ```js
var iswhitespace = i18n.Unicode.isWhitespace("a"); // Return false. let iswhitespace = I18n.Unicode.isWhitespace("a"); // iswhitespace = false
``` ```
...@@ -1908,7 +1919,7 @@ Checks whether the input character is of the right to left (RTL) language. ...@@ -1908,7 +1919,7 @@ Checks whether the input character is of the right to left (RTL) language.
**Example** **Example**
```js ```js
var isrtl = i18n.Unicode.isRTL("a"); // Return false. let isrtl = I18n.Unicode.isRTL("a"); // isrtl = false
``` ```
...@@ -1934,7 +1945,7 @@ Checks whether the input character is an ideographic character. ...@@ -1934,7 +1945,7 @@ Checks whether the input character is an ideographic character.
**Example** **Example**
```js ```js
var isideograph = i18n.Unicode.isIdeograph("a"); // Return false. let isideograph = I18n.Unicode.isIdeograph("a"); // isideograph = false
``` ```
...@@ -1960,7 +1971,7 @@ Checks whether the input character is a letter. ...@@ -1960,7 +1971,7 @@ Checks whether the input character is a letter.
**Example** **Example**
```js ```js
var isletter = i18n.Unicode.isLetter("a"); // Return true. let isletter = I18n.Unicode.isLetter("a"); // isletter = true
``` ```
...@@ -1986,7 +1997,7 @@ Checks whether the input character is a lowercase letter. ...@@ -1986,7 +1997,7 @@ Checks whether the input character is a lowercase letter.
**Example** **Example**
```js ```js
var islowercase = i18n.Unicode.isLowerCase("a"); // Return true. let islowercase = I18n.Unicode.isLowerCase("a"); // islowercase = true
``` ```
...@@ -2012,7 +2023,7 @@ Checks whether the input character is an uppercase letter. ...@@ -2012,7 +2023,7 @@ Checks whether the input character is an uppercase letter.
**Example** **Example**
```js ```js
var isuppercase = i18n.Unicode.isUpperCase("a"); // Return false. let isuppercase = I18n.Unicode.isUpperCase("a"); // isuppercase = false
``` ```
...@@ -2038,7 +2049,7 @@ Obtains the type of the input character string. ...@@ -2038,7 +2049,7 @@ Obtains the type of the input character string.
**Example** **Example**
```js ```js
var type = i18n.Unicode.getType("a"); let type = I18n.Unicode.getType("a"); // type = "U_LOWERCASE_LETTER"
``` ```
...@@ -2071,7 +2082,7 @@ Converts one measurement unit into another and formats the unit based on the spe ...@@ -2071,7 +2082,7 @@ Converts one measurement unit into another and formats the unit based on the spe
**Example** **Example**
```js ```js
i18n.I18NUtil.unitConvert({unit: "cup", measureSystem: "US"}, {unit: "liter", measureSystem: "SI"}, 1000, "en-US", "long"); let res = I18n.I18NUtil.unitConvert({unit: "cup", measureSystem: "US"}, {unit: "liter", measureSystem: "SI"}, 1000, "en-US", "long"); // res = 236.588 liters
``` ```
...@@ -2097,11 +2108,11 @@ Obtains the sequence of the year, month, and day in the specified locale. ...@@ -2097,11 +2108,11 @@ Obtains the sequence of the year, month, and day in the specified locale.
**Example** **Example**
```js ```js
i18n.I18NUtil.getDateOrder("zh-CN"); let order = I18n.I18NUtil.getDateOrder("zh-CN"); // order = "y-L-d"
``` ```
## i18n.getDisplayCountry<sup>(deprecated)</sup> ## I18n.getDisplayCountry<sup>(deprecated)</sup>
getDisplayCountry(country: string, locale: string, sentenceCase?: boolean): string getDisplayCountry(country: string, locale: string, sentenceCase?: boolean): string
...@@ -2127,12 +2138,12 @@ This API is deprecated since API version 9. You are advised to use [System.getDi ...@@ -2127,12 +2138,12 @@ This API is deprecated since API version 9. You are advised to use [System.getDi
**Example** **Example**
```js ```js
i18n.getDisplayCountry("zh-CN", "en-GB", true); let countryName = I18n.getDisplayCountry("zh-CN", "en-GB", true); // countryName = true
i18n.getDisplayCountry("zh-CN", "en-GB"); countryName = I18n.getDisplayCountry("zh-CN", "en-GB"); // countryName = true
``` ```
## i18n.getDisplayLanguage<sup>(deprecated)</sup> ## I18n.getDisplayLanguage<sup>(deprecated)</sup>
getDisplayLanguage(language: string, locale: string, sentenceCase?: boolean): string getDisplayLanguage(language: string, locale: string, sentenceCase?: boolean): string
...@@ -2158,12 +2169,12 @@ This API is deprecated since API version 9. You are advised to use [System.getDi ...@@ -2158,12 +2169,12 @@ This API is deprecated since API version 9. You are advised to use [System.getDi
**Example** **Example**
```js ```js
i18n.getDisplayLanguage("zh", "en-GB", true); let languageName = I18n.getDisplayLanguage("zh", "en-GB", true); // languageName = "Chinese"
i18n.getDisplayLanguage("zh", "en-GB"); languageName = I18n.getDisplayLanguage("zh", "en-GB"); // languageName = "Chinese"
``` ```
## i18n.getSystemLanguage<sup>(deprecated)</sup> ## I18n.getSystemLanguage<sup>(deprecated)</sup>
getSystemLanguage(): string getSystemLanguage(): string
...@@ -2181,11 +2192,11 @@ This API is deprecated since API version 9. You are advised to use [System.getSy ...@@ -2181,11 +2192,11 @@ This API is deprecated since API version 9. You are advised to use [System.getSy
**Example** **Example**
```js ```js
i18n.getSystemLanguage(); let systemLanguage = I18n.getSystemLanguage(); // Obtain the current system language.
``` ```
## i18n.getSystemRegion<sup>(deprecated)</sup> ## I18n.getSystemRegion<sup>(deprecated)</sup>
getSystemRegion(): string getSystemRegion(): string
...@@ -2203,11 +2214,11 @@ This API is deprecated since API version 9. You are advised to use [System.getSy ...@@ -2203,11 +2214,11 @@ This API is deprecated since API version 9. You are advised to use [System.getSy
**Example** **Example**
```js ```js
i18n.getSystemRegion(); let region = I18n.getSystemRegion(); // Obtain the current system region.
``` ```
## i18n.getSystemLocale<sup>(deprecated)</sup> ## I18n.getSystemLocale<sup>(deprecated)</sup>
getSystemLocale(): string getSystemLocale(): string
...@@ -2225,11 +2236,11 @@ This API is deprecated since API version 9. You are advised to use [System.getSy ...@@ -2225,11 +2236,11 @@ This API is deprecated since API version 9. You are advised to use [System.getSy
**Example** **Example**
```js ```js
i18n.getSystemLocale(); let locale = I18n.getSystemLocale (); // Obtain the system locale.
``` ```
## i18n.is24HourClock<sup>(deprecated)</sup> ## I18n.is24HourClock<sup>(deprecated)</sup>
is24HourClock(): boolean is24HourClock(): boolean
...@@ -2247,11 +2258,11 @@ This API is deprecated since API version 9. You are advised to use [System.is24H ...@@ -2247,11 +2258,11 @@ This API is deprecated since API version 9. You are advised to use [System.is24H
**Example** **Example**
```js ```js
var is24HourClock = i18n.is24HourClock(); let is24HourClock = I18n.is24HourClock();
``` ```
## i18n.set24HourClock<sup>(deprecated)</sup> ## I18n.set24HourClock<sup>(deprecated)</sup>
set24HourClock(option: boolean): boolean set24HourClock(option: boolean): boolean
...@@ -2278,11 +2289,11 @@ This API is deprecated since API version 9. You are advised to use [System.set24 ...@@ -2278,11 +2289,11 @@ This API is deprecated since API version 9. You are advised to use [System.set24
**Example** **Example**
```js ```js
// Set the system time to the 24-hour clock. // Set the system time to the 24-hour clock.
var success = i18n.set24HourClock(true); let success = I18n.set24HourClock(true);
``` ```
## i18n.addPreferredLanguage<sup>(deprecated)</sup> ## I18n.addPreferredLanguage<sup>(deprecated)</sup>
addPreferredLanguage(language: string, index?: number): boolean addPreferredLanguage(language: string, index?: number): boolean
...@@ -2310,13 +2321,13 @@ This API is supported since API version 8 and is deprecated since API version 9. ...@@ -2310,13 +2321,13 @@ This API is supported since API version 8 and is deprecated since API version 9.
**Example** **Example**
```js ```js
// Add zh-CN to the preferred language list. // Add zh-CN to the preferred language list.
var language = 'zh-CN'; let language = 'zh-CN';
var index = 0; let index = 0;
var success = i18n.addPreferredLanguage(language, index); let success = I18n.addPreferredLanguage(language, index);
``` ```
## i18n.removePreferredLanguage<sup>(deprecated)</sup> ## I18n.removePreferredLanguage<sup>(deprecated)</sup>
removePreferredLanguage(index: number): boolean removePreferredLanguage(index: number): boolean
...@@ -2343,16 +2354,16 @@ This API is supported since API version 8 and is deprecated since API version 9. ...@@ -2343,16 +2354,16 @@ This API is supported since API version 8 and is deprecated since API version 9.
**Example** **Example**
```js ```js
// Delete the first preferred language from the preferred language list. // Delete the first preferred language from the preferred language list.
var index = 0; let index = 0;
var success = i18n.removePreferredLanguage(index); let success = I18n.removePreferredLanguage(index);
``` ```
## i18n.getPreferredLanguageList<sup>(deprecated)</sup> ## I18n.getPreferredLanguageList<sup>(deprecated)</sup>
getPreferredLanguageList(): Array&lt;string&gt; getPreferredLanguageList(): Array&lt;string&gt;
Obtains the list of preferred languages. Obtains the preferred language list.
This API is supported since API version 8 and is deprecated since API version 9. You are advised to use [System.getPreferredLanguageList](#getpreferredlanguagelist9) instead. This API is supported since API version 8 and is deprecated since API version 9. You are advised to use [System.getPreferredLanguageList](#getpreferredlanguagelist9) instead.
...@@ -2362,15 +2373,15 @@ This API is supported since API version 8 and is deprecated since API version 9. ...@@ -2362,15 +2373,15 @@ This API is supported since API version 8 and is deprecated since API version 9.
| Type | Description | | Type | Description |
| ------------------- | --------- | | ------------------- | --------- |
| Array&lt;string&gt; | List of preferred languages.| | Array&lt;string&gt; | Preferred language list.|
**Example** **Example**
```js ```js
var preferredLanguageList = i18n.getPreferredLanguageList(); let preferredLanguageList = I18n.getPreferredLanguageList(); // Obtain the preferred language list.
``` ```
## i18n.getFirstPreferredLanguage<sup>(deprecated)</sup> ## I18n.getFirstPreferredLanguage<sup>(deprecated)</sup>
getFirstPreferredLanguage(): string getFirstPreferredLanguage(): string
...@@ -2388,7 +2399,7 @@ This API is supported since API version 8 and is deprecated since API version 9. ...@@ -2388,7 +2399,7 @@ This API is supported since API version 8 and is deprecated since API version 9.
**Example** **Example**
```js ```js
var firstPreferredLanguage = i18n.getFirstPreferredLanguage(); let firstPreferredLanguage = I18n.getFirstPreferredLanguage();
``` ```
......
# Internationalization – Intl # @ohos.intl (Internationalization)
The Intl module provides basic I18N capabilities, such as time and date formatting, number formatting, and string sorting, through the standard I18N APIs defined in ECMA 402. The **intl** module provides basic i18n capabilities, such as time and date formatting, number formatting, and string sorting, through the standard i18n APIs defined in ECMA 402.
The [i18n](i18n-guidelines.md) module provides enhanced i18n capabilities through supplementary interfaces that are not defined in ECMA 402. It works with the intl module to provide a complete suite of i18n capabilities.
The [I18N](js-apis-i18n.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** > **NOTE**
> - 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 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. > - This module provides basic i18n capabilities, such as time and date formatting, number formatting, and string sorting, through the standard i18n interfaces defined in ECMA 402. For details about the enhanced i18n capabilities, see [i18n](js-apis-i18n.md).
## Modules to Import ## Modules to Import
``` ```js
import Intl from '@ohos.intl'; import Intl from '@ohos.intl';
``` ```
Importing an incorrect bundle can lead to unexpected API behavior.
## Locale ## Locale
...@@ -30,9 +30,9 @@ import Intl from '@ohos.intl'; ...@@ -30,9 +30,9 @@ import Intl from '@ohos.intl';
| region | string | Yes | No | Region associated with the locale, for example, **CN**. | | region | string | Yes | No | Region associated with the locale, for example, **CN**. |
| baseName | string | Yes | No | Basic key information about the locale, which consists of the language, script, and region, for example, **zh-Hans-CN**. | | baseName | string | Yes | No | Basic key information about the locale, which consists of the language, script, and region, for example, **zh-Hans-CN**. |
| caseFirst | string | Yes | No | Whether case is taken into account for the locale's collation rules. The value can be **upper**, **lower**, or **false**.| | caseFirst | string | Yes | No | Whether case is taken into account for the locale's collation rules. The value can be **upper**, **lower**, or **false**.|
| calendar | string | Yes | No | Calendar for the locale. The value can be any of the following: **buddhist**, **chinese**, **coptic**, **dangi**, **ethioaa**, **ethiopic**, **gregory**, **hebrew**, **indian**, **islamic**, **islamic-umalqura**, **islamic-tbla**, **islamic-civil**, **islamic-rgsa**, **iso8601**, **japanese**, **persian**, **roc**, **islamicc**.| | calendar | string | Yes | No | Calendar for the locale. The value can be any of the following: **buddhist**, **chinese**, **coptic**, **dangi**, **ethioaa**, **ethiopic**, **gregory**, **hebrew**, **indian**, **islamic**, **islamic-umalqura**, **islamic-tbla**, **islamic-civil**, **islamic-rgsa**, **iso8601**, **japanese**, **persian**, **roc**, or **islamicc**.|
| collation | string | Yes | No | Rule for sorting regions. The value can be any of the following: **big5han**, **compat**, **dict**, **direct**, **ducet**, **eor**, **gb2312**, **phonebk**, **phonetic**, **pinyin**, **reformed**, **searchjl**, **stroke**, **trad**, **unihan**, **zhuyin**.| | collation | string | Yes | No | Rule for sorting regions. The value can be any of the following: **big5han**, **compat**, **dict**, **direct**, **ducet**, **eor**, **gb2312**, **phonebk**, **phonetic**, **pinyin**, **reformed**, **searchjl**, **stroke**, **trad**, **unihan**, **zhuyin**.|
| hourCycle | string | Yes | No | Time system for the locale. The value can be any of the following: **h12**, **h23**, **h11**, **h24**.| | hourCycle | string | Yes | No | Time system for the locale. The value can be any of the following: **h12**, **h23**, **h11**, or **h24**.|
| numberingSystem | string | Yes | No | Numbering system for the locale. The value can be any of the following: **adlm**, **ahom**, **arab**, **arabext**, **bali**, **beng**, **bhks**, **brah**, **cakm**, **cham**, **deva**, **diak**, **fullwide**, **gong**, **gonm**, **gujr**, **guru**, **hanidec**, **hmng**, **hmnp**, **java**, **kali**, **khmr**, **knda**, **lana**, **lanatham**, **laoo**, **latn**, **lepc**, **limb**, **mathbold**, **mathdbl**, **mathmono**, **mathsanb**, **mathsans**, **mlym**, **modi**, **mong**, **mroo**, **mtei**, **mymr**, **mymrshan**, **mymrtlng**, **newa**, **nkoo**, **olck**, **orya**, **osma**, **rohg**, **saur**, **segment**, **shrd**, **sind**, **sinh**, **sora**, **sund**, **takr**, **talu**, **tamldec**, **telu**, **thai**, **tibt**, **tirh**, **vaii**, **wara**, **wcho**.| | numberingSystem | string | Yes | No | Numbering system for the locale. The value can be any of the following: **adlm**, **ahom**, **arab**, **arabext**, **bali**, **beng**, **bhks**, **brah**, **cakm**, **cham**, **deva**, **diak**, **fullwide**, **gong**, **gonm**, **gujr**, **guru**, **hanidec**, **hmng**, **hmnp**, **java**, **kali**, **khmr**, **knda**, **lana**, **lanatham**, **laoo**, **latn**, **lepc**, **limb**, **mathbold**, **mathdbl**, **mathmono**, **mathsanb**, **mathsans**, **mlym**, **modi**, **mong**, **mroo**, **mtei**, **mymr**, **mymrshan**, **mymrtlng**, **newa**, **nkoo**, **olck**, **orya**, **osma**, **rohg**, **saur**, **segment**, **shrd**, **sind**, **sinh**, **sora**, **sund**, **takr**, **talu**, **tamldec**, **telu**, **thai**, **tibt**, **tirh**, **vaii**, **wara**, **wcho**.|
| numeric | boolean | Yes | No | Whether to apply special collation rules for numeric characters. | | numeric | boolean | Yes | No | Whether to apply special collation rules for numeric characters. |
...@@ -41,13 +41,16 @@ import Intl from '@ohos.intl'; ...@@ -41,13 +41,16 @@ import Intl from '@ohos.intl';
constructor() constructor()
Creates a Locale object. Creates a **Locale** object.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Example** **Example**
``` ```js
var locale = new Intl.Locale(); // The default constructor uses the current system locale to create a Locale object.
let locale = new Intl.Locale()
// Return the current system locale.
let localeID = locale.toString()
``` ```
...@@ -55,20 +58,22 @@ Creates a Locale object. ...@@ -55,20 +58,22 @@ Creates a Locale object.
constructor(locale: string, options?: LocaleOptions) constructor(locale: string, options?: LocaleOptions)
Creates a Locale object. Creates a **Locale** object.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | ------------- | ---- | ---------------------------- | | -------------------- | -------------------------------- | ---- | ---------------------------- |
| locale | string | Yes | A string containing locale information, including the language, optional script, and region.| | locale | string | Yes | A string containing locale information, including the language, optional script, and region. For details about the international standards and combination modes for the language, script, and country or region, see [intl Development](../../internationalization/intl-guidelines.md#setting-locale-information).|
| options<sup>9+</sup> | [LocaleOptions](#localeoptions9) | No | Options for creating the **Locale** object. | | options<sup>9+</sup> | [LocaleOptions](#localeoptions6) | No | Options for creating the **Locale** object. |
**Example** **Example**
``` ```js
var locale = new Intl.Locale("zh-CN"); // Create a Locale object named zh-CN.
let locale = new Intl.Locale("zh-CN")
let localeID = locale.toString() // localeID = "zh-CN"
``` ```
...@@ -76,20 +81,21 @@ Creates a Locale object. ...@@ -76,20 +81,21 @@ Creates a Locale object.
toString(): string toString(): string
Converts locale information to a string. Obtains the string representation of a **Locale** object.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ------ | ----------- | | ------ | ----------- |
| string | String containing locale information.| | string | String representation of the **Locale** object.|
**Example** **Example**
``` ```js
var locale = new Intl.Locale("zh-CN"); // Create a Locale object named en-GB.
locale.toString(); let locale = new Intl.Locale("en-GB");
let localeID = locale.toString(); // localeID = "en-GB"
``` ```
...@@ -101,16 +107,25 @@ Maximizes information of the **Locale** object. If the script and locale informa ...@@ -101,16 +107,25 @@ Maximizes information of the **Locale** object. If the script and locale informa
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ----------------- | ---------- | | ----------------- | ---------- |
| [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"); // Create a Locale object named zh.
locale.maximize(); let locale = new Intl.Locale("zh");
// Complete the script and region of the Locale object.
let maximizedLocale = locale.maximize();
let localeID = maximizedLocale.toString(); // localeID = "zh-Hans-CN"
// Create a Locale object named en-US.
locale = new Intl.Locale("en-US");
// Complete the script of the Locale object.
maximizedLocale = locale.maximize();
localeID = maximizedLocale.toString(); // localeID = "en-Latn-US"
``` ```
...@@ -122,20 +137,29 @@ Minimizes information of the **Locale** object. If the script and locale informa ...@@ -122,20 +137,29 @@ Minimizes information of the **Locale** object. If the script and locale informa
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ----------------- | ---------- | | ----------------- | ---------- |
| [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"); // Create a Locale object named zh-Hans-CN.
locale.minimize(); let locale = new Intl.Locale("zh-Hans-CN");
// Remove the script and region of the Locale object.
let minimizedLocale = locale.minimize();
let localeID = minimizedLocale.toString(); // localeID = "zh"
// Create a Locale object named en-US.
locale = new Intl.Locale("en-US");
// Remove the region of the Locale object.
minimizedLocale = locale.minimize();
localeID = minimizedLocale.toString(); // localeID = "en"
``` ```
## LocaleOptions<sup>9+</sup> ## LocaleOptions<sup>6+</sup>
Represents the locale options. Represents the locale options.
...@@ -143,9 +167,9 @@ Represents the locale options. ...@@ -143,9 +167,9 @@ Represents the locale options.
| Name | Type | Readable | Writable | Description | | Name | Type | Readable | Writable | Description |
| --------------- | ------- | ---- | ---- | ---------------------------------------- | | --------------- | ------- | ---- | ---- | ---------------------------------------- |
| calendar | string | Yes | Yes | Calendar for the locale. The value can be any of the following: **buddhist**, **chinese**, **coptic**, **dangi**, **ethioaa**, **ethiopic**, **gregory**, **hebrew**, **indian**, **islamic**, **islamic-umalqura**, **islamic-tbla**, **islamic-civil**, **islamic-rgsa**, **iso8601**, **japanese**, **persian**, **roc**, **islamicc**.| | calendar | string | Yes | Yes | Calendar for the locale. The value can be any of the following: **buddhist**, **chinese**, **coptic**, **dangi**, **ethioaa**, **ethiopic**, **gregory**, **hebrew**, **indian**, **islamic**, **islamic-umalqura**, **islamic-tbla**, **islamic-civil**, **islamic-rgsa**, **iso8601**, **japanese**, **persian**, **roc**, or **islamicc**.|
| collation | string | Yes | Yes | Collation rule. The value can be any of the following: **big5han**, **compat**, **dict**, **direct**, **ducet**, **emoji**, **eor**, **gb2312**, **phonebk**, **phonetic**, **pinyin**, **reformed**,**search**, **searchjl**, **standard**, **stroke**, **trad**, **unihan**, **zhuyin**.| | collation | string | Yes | Yes | Collation rule. The value can be any of the following: **big5han**, **compat**, **dict**, **direct**, **ducet**, **emoji**, **eor**, **gb2312**, **phonebk**, **phonetic**, **pinyin**, **reformed**, **search**, **searchjl**, **standard**, **stroke**, **trad**, **unihan**, **zhuyin**.|
| hourCycle | string | Yes | Yes | Time system for the locale. The value can be any of the following: **h11**, **h12**, **h23**, **h24**.| | hourCycle | string | Yes | Yes | Time system for the locale. The value can be any of the following: **h11**, **h12**, **h23**, or **h24**.|
| numberingSystem | string | Yes | Yes | Numbering system for the locale. The value can be any of the following: **adlm**, **ahom**, **arab**, **arabext**, **bali**, **beng**, **bhks**, **brah**, **cakm**, **cham**, **deva**, **diak**, **fullwide**, **gong**, **gonm**, **gujr**, **guru**, **hanidec**, **hmng**, **hmnp**, **java**, **kali**, **khmr**, **knda**, **lana**, **lanatham**, **laoo**, **latn**, **lepc**, **limb**, **mathbold**, **mathdbl**, **mathmono**, **mathsanb**, **mathsans**, **mlym**, **modi**, **mong**, **mroo**, **mtei**, **mymr**, **mymrshan**, **mymrtlng**, **newa**, **nkoo**, **olck**, **orya**, **osma**, **rohg**, **saur**, **segment**, **shrd**, **sind**, **sinh**, **sora**, **sund**, **takr**, **talu**, **tamldec**, **telu**, **thai**, **tibt**, **tirh**, **vaii**, **wara**, **wcho**.| | numberingSystem | string | Yes | Yes | Numbering system for the locale. The value can be any of the following: **adlm**, **ahom**, **arab**, **arabext**, **bali**, **beng**, **bhks**, **brah**, **cakm**, **cham**, **deva**, **diak**, **fullwide**, **gong**, **gonm**, **gujr**, **guru**, **hanidec**, **hmng**, **hmnp**, **java**, **kali**, **khmr**, **knda**, **lana**, **lanatham**, **laoo**, **latn**, **lepc**, **limb**, **mathbold**, **mathdbl**, **mathmono**, **mathsanb**, **mathsans**, **mlym**, **modi**, **mong**, **mroo**, **mtei**, **mymr**, **mymrshan**, **mymrtlng**, **newa**, **nkoo**, **olck**, **orya**, **osma**, **rohg**, **saur**, **segment**, **shrd**, **sind**, **sinh**, **sora**, **sund**, **takr**, **talu**, **tamldec**, **telu**, **thai**, **tibt**, **tirh**, **vaii**, **wara**, **wcho**.|
| numeric | boolean | Yes | Yes | Whether to use the 12-hour clock. | | numeric | boolean | Yes | Yes | Whether to use the 12-hour clock. |
| caseFirst | string | Yes | Yes | Whether upper case or lower case is sorted first. The value can be **upper**, **lower**, or **false**.| | caseFirst | string | Yes | Yes | Whether upper case or lower case is sorted first. The value can be **upper**, **lower**, or **false**.|
...@@ -163,8 +187,9 @@ Creates a **DateTimeOptions** object for the specified locale. ...@@ -163,8 +187,9 @@ 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(); // Use the current system locale to create a DateTimeFormat object.
let datefmt= new Intl.DateTimeFormat();
``` ```
...@@ -179,19 +204,21 @@ Creates a **DateTimeOptions** object for the specified locale. ...@@ -179,19 +204,21 @@ Creates a **DateTimeOptions** object for the specified locale.
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | ----------------------------------- | ---- | ---------------------------- | | -------------------- | ------------------------------------ | ---- | ---------------------------- |
| locale | string \| Array&lt;string&gt; | Yes | A string containing locale information, including the language, optional script, and region.| | locale | string \| Array&lt;string&gt; | Yes | A string containing locale information, including the language, optional script, and region.|
| options<sup>9+</sup> | [DateTimeOptions](#datetimeoptions9) | No | Options for creating a **DateTimeFormat** object. | | options<sup>9+</sup> | [DateTimeOptions](#datetimeoptions6) | No | Options for creating a **DateTimeFormat** object. |
**Example** **Example**
``` ```js
var datefmt= new Intl.DateTimeFormat("zh-CN", { dateStyle: 'full', timeStyle: 'medium' }); // Use locale zh-CN to create a DateTimeFormat object. Set dateStyle to full and timeStyle to medium.
let datefmt= new Intl.DateTimeFormat("zh-CN", { dateStyle: 'full', timeStyle: 'medium' });
``` ```
**Example** **Example**
``` ```js
var datefmt= new Intl.DateTimeFormat(["ban", "zh"], { dateStyle: 'full', timeStyle: 'medium' }); // Use the locale list ["ban", "zh"] to create a DateTimeFormat object. Because ban is an invalid locale ID, locale zh is used to create the DateTimeFormat object.
let datefmt= new Intl.DateTimeFormat(["ban", "zh"], { dateStyle: 'full', timeStyle: 'medium' });
``` ```
...@@ -209,17 +236,22 @@ Formats the specified date and time. ...@@ -209,17 +236,22 @@ Formats the specified date and time.
| ---- | ---- | ---- | ------- | | ---- | ---- | ---- | ------- |
| date | Date | Yes | Date and time to be formatted.| | date | Date | Yes | Date and time to be formatted.|
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ------ | ------------ | | ------ | ------------ |
| 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); let date = new Date(2021, 11, 17, 3, 24, 0);
var datefmt = new Intl.DateTimeFormat("en-GB"); // Use locale en-GB to create a DateTimeFormat object.
datefmt.format(date); let datefmt = new Intl.DateTimeFormat("en-GB");
let formattedDate = datefmt.format(date); // formattedDate "17/12/2021"
// Use locale en-GB to create a DateTimeFormat object. Set dateStyle to full and timeStyle to medium.
datefmt = new Intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' });
formattedDate = datefmt.format(date); // formattedDate "Friday, 17 December 2021 at 03:24:00"
``` ```
...@@ -238,18 +270,19 @@ Formats the specified date range. ...@@ -238,18 +270,19 @@ Formats the specified date range.
| startDate | Date | Yes | Start date and time to be formatted.| | startDate | Date | Yes | Start date and time to be formatted.|
| endDate | Date | Yes | End date and time to be formatted.| | endDate | Date | Yes | End date and time to be formatted.|
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ------ | -------------- | | ------ | -------------- |
| 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); let startDate = new Date(2021, 11, 17, 3, 24, 0);
var endDate = new Date(2021, 11, 18, 3, 24, 0); let endDate = new Date(2021, 11, 18, 3, 24, 0);
var datefmt = new Intl.DateTimeFormat("en-GB"); // Use locale en-GB to create a DateTimeFormat object.
datefmt.formatRange(startDate, endDate); let datefmt = new Intl.DateTimeFormat("en-GB");
let formattedDateRange = datefmt.formatRange(startDate, endDate); // formattedDateRange = "17/12/2021-18/12/2021"
``` ```
...@@ -261,20 +294,23 @@ Obtains the formatting options for **DateTimeFormat** object. ...@@ -261,20 +294,23 @@ Obtains the formatting options for **DateTimeFormat** object.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ----------------------------------- | ----------------------------- | | ------------------------------------ | ----------------------------- |
| [DateTimeOptions](#datetimeoptions9) | Formatting options for **DateTimeFormat** objects.| | [DateTimeOptions](#datetimeoptions6) | Formatting options for **DateTimeFormat** objects.|
**Example** **Example**
``` ```js
var datefmt = new Intl.DateTimeFormat("en-GB"); let datefmt = new Intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' });
datefmt.resolvedOptions(); // Obtain the options of the DateTimeFormat object.
let options = datefmt.resolvedOptions();
let dateStyle = options.dateStyle; // dateStyle = "full"
let timeStyle = options.timeStyle; // timeStyle = "medium"
``` ```
## DateTimeOptions<sup>9+</sup> ## DateTimeOptions<sup>6+</sup>
Provides the options for the **DateTimeFormat** object. Provides the options for the **DateTimeFormat** object.
...@@ -285,7 +321,7 @@ Provides the options for the **DateTimeFormat** object. ...@@ -285,7 +321,7 @@ Provides the options for the **DateTimeFormat** object.
| locale | string | Yes | No | Locale, for example, **zh-Hans-CN**. | | locale | string | Yes | No | Locale, for example, **zh-Hans-CN**. |
| dateStyle | string | Yes | Yes | Date display format. The value can be **long**, **short**, **medium**, or **full**.| | dateStyle | string | Yes | Yes | Date display format. The value can be **long**, **short**, **medium**, or **full**.|
| timeStyle | string | Yes | Yes | Time display format. The value can be **long**, **short**, **medium**, or **full**.| | timeStyle | string | Yes | Yes | Time display format. The value can be **long**, **short**, **medium**, or **full**.|
| hourCycle | string | Yes | Yes | Time system for the locale. The value can be any of the following: **h11**, **h12**, **h23**, **h24**.| | hourCycle | string | Yes | Yes | Time system for the locale. The value can be any of the following: **h11**, **h12**, **h23**, or **h24**.|
| timeZone | string | Yes | Yes | Time zone represented by a valid IANA time zone ID. | | timeZone | string | Yes | Yes | Time zone represented by a valid IANA time zone ID. |
| numberingSystem | string | Yes | Yes | Numbering system for the locale. The value can be any of the following: **adlm**, **ahom**, **arab**, **arabext**, **bali**, **beng**, **bhks**, **brah**, **cakm**, **cham**, **deva**, **diak**, **fullwide**, **gong**, **gonm**, **gujr**, **guru**, **hanidec**, **hmng**, **hmnp**, **java**, **kali**, **khmr**, **knda**, **lana**, **lanatham**, **laoo**, **latn**, **lepc**, **limb**, **mathbold**, **mathdbl**, **mathmono**, **mathsanb**, **mathsans**, **mlym**, **modi**, **mong**, **mroo**, **mtei**, **mymr**, **mymrshan**, **mymrtlng**, **newa**, **nkoo**, **olck**, **orya**, **osma**, **rohg**, **saur**, **segment**, **shrd**, **sind**, **sinh**, **sora**, **sund**, **takr**, **talu**, **tamldec**, **telu**, **thai**, **tibt**, **tirh**, **vaii**, **wara**, **wcho**.| | numberingSystem | string | Yes | Yes | Numbering system for the locale. The value can be any of the following: **adlm**, **ahom**, **arab**, **arabext**, **bali**, **beng**, **bhks**, **brah**, **cakm**, **cham**, **deva**, **diak**, **fullwide**, **gong**, **gonm**, **gujr**, **guru**, **hanidec**, **hmng**, **hmnp**, **java**, **kali**, **khmr**, **knda**, **lana**, **lanatham**, **laoo**, **latn**, **lepc**, **limb**, **mathbold**, **mathdbl**, **mathmono**, **mathsanb**, **mathsans**, **mlym**, **modi**, **mong**, **mroo**, **mtei**, **mymr**, **mymrshan**, **mymrtlng**, **newa**, **nkoo**, **olck**, **orya**, **osma**, **rohg**, **saur**, **segment**, **shrd**, **sind**, **sinh**, **sora**, **sund**, **takr**, **talu**, **tamldec**, **telu**, **thai**, **tibt**, **tirh**, **vaii**, **wara**, **wcho**.|
| hour12 | boolean | Yes | Yes | Whether to use the 12-hour clock. | | hour12 | boolean | Yes | Yes | Whether to use the 12-hour clock. |
...@@ -315,8 +351,9 @@ Creates a **NumberFormat** object for the specified locale. ...@@ -315,8 +351,9 @@ 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(); // Use the current system locale to create a NumberFormat object.
let numfmt = new Intl.NumberFormat();
``` ```
...@@ -328,15 +365,17 @@ Creates a **NumberFormat** object for the specified locale. ...@@ -328,15 +365,17 @@ Creates a **NumberFormat** object for the specified locale.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
Parameters **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | ------------------------------- | ---- | ---------------------------- | | -------------------- | -------------------------------- | ---- | ---------------------------- |
| locale | string \| Array&lt;string&gt; | Yes | A string containing locale information, including the language, optional script, and region.| | locale | string \| Array&lt;string&gt; | Yes | A string containing locale information, including the language, optional script, and region.|
| options<sup>9+</sup> | [NumberOptions](#numberoptions9) | No | Options for creating a **NumberFormat** object. | | options<sup>9+</sup> | [NumberOptions](#numberoptions6) | No | Options for creating a **NumberFormat** object. |
**Example** **Example**
``` ```js
var numfmt = new Intl.NumberFormat("en-GB", {style:'decimal', notation:"scientific"}); // Use locale en-GB to create a NumberFormat object. Set style to decimal and notation to scientific.
let numfmt = new Intl.NumberFormat("en-GB", {style:'decimal', notation:"scientific"});
``` ```
...@@ -354,7 +393,7 @@ Formats a number. ...@@ -354,7 +393,7 @@ Formats a number.
| ------ | ------ | ---- | ---- | | ------ | ------ | ---- | ---- |
| number | number | Yes | Number to be formatted.| | number | number | Yes | Number to be formatted.|
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ------ | ---------- | | ------ | ---------- |
...@@ -362,9 +401,10 @@ Formats a number. ...@@ -362,9 +401,10 @@ Formats a number.
**Example** **Example**
``` ```js
var numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"}); // Use locale list ["en-GB", "zh"] to create a NumberFormat object. Because en-GB is a valid locale ID, it is used to create the NumberFormat object.
numfmt.format(1223); let numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
let formattedNumber = numfmt.format(1223); // formattedNumber = 1.223E3
``` ```
...@@ -376,23 +416,26 @@ Obtains the options of the **NumberFormat** object. ...@@ -376,23 +416,26 @@ Obtains the options of the **NumberFormat** object.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ------------------------------- | --------------------------- | | -------------------------------- | --------------------------- |
| [NumberOptions](#numberoptions9) | Formatting options for **NumberFormat** objects.| | [NumberOptions](#numberoptions6) | Formatting options for **NumberFormat** objects.|
**Example** **Example**
``` ```js
var numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"}); let numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
numfmt.resolvedOptions(); // Obtain the options of the NumberFormat object.
let options = numfmt.resolvedOptions();
let style = options.style; // style = decimal
let notation = options.notation // notation = scientific
``` ```
## NumberOptions<sup>9+</sup> ## NumberOptions<sup>6+</sup>
Provides the device capability. Defines the device capability.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
...@@ -426,13 +469,14 @@ Provides the device capability. ...@@ -426,13 +469,14 @@ Provides the device capability.
constructor() constructor()
Creates a Collator object. Creates a **Collator** object.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Example** **Example**
``` ```js
var collator = new Intl.Collator(); // Use the system locale to create a Collator object.
let collator = new Intl.Collator();
``` ```
...@@ -440,20 +484,21 @@ Creates a Collator object. ...@@ -440,20 +484,21 @@ Creates a Collator object.
constructor(locale: string | Array&lt;string&gt;, options?: CollatorOptions) constructor(locale: string | Array&lt;string&gt;, options?: CollatorOptions)
Creates a Collator object. Creates a **Collator** object.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | ----------------------------------- | ---- | ---------------------------- | | -------------------- | ------------------------------------ | ---- | ---------------------------- |
| locale | string \| Array&lt;string&gt; | Yes | A string containing locale information, including the language, optional script, and region.| | locale | string \| Array&lt;string&gt; | Yes | A string containing locale information, including the language, optional script, and region.|
| options<sup>9+</sup> | [CollatorOptions](#collatoroptions9) | No | Options for creating a **Collator** object. | | options<sup>9+</sup> | [CollatorOptions](#collatoroptions8) | No | Options for creating a **Collator** object. |
**Example** **Example**
``` ```js
var collator = new Intl.Collator("zh-CN", {localeMatcher: "lookup", usage: "sort"}); // Use locale zh-CN to create a Collator object. Set localeMatcher to lookup and usage to sort.
let collator = new Intl.Collator("zh-CN", {localeMatcher: "lookup", usage: "sort"});
``` ```
...@@ -472,16 +517,18 @@ Compares two strings based on the sorting policy of the **Collator** object. ...@@ -472,16 +517,18 @@ Compares two strings based on the sorting policy of the **Collator** object.
| first | string | Yes | First string to compare. | | first | string | Yes | First string to compare. |
| second | string | Yes | Second string to compare.| | second | string | Yes | Second string to compare.|
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ------ | ---------------------------------------- | | ------ | ---------------------------------------- |
| 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"); // Use locale en-GB to create a Collator object.
collator.compare("first", "second"); let collator = new Intl.Collator("en-GB");
// Compare the sequence of the first and second strings.
let compareResult = collator.compare("first", "second"); // compareResult = -1
``` ```
...@@ -493,21 +540,23 @@ Returns properties reflecting the locale and collation options of a **Collator** ...@@ -493,21 +540,23 @@ Returns properties reflecting the locale and collation options of a **Collator**
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ----------------------------------- | ----------------- | | ------------------------------------ | ----------------- |
| [CollatorOptions](#collatoroptions9) | Properties of the **Collator** object.| | [CollatorOptions](#collatoroptions8) | Properties of the **Collator** object.|
**Example** **Example**
```js
``` let collator = new Intl.Collator("zh-Hans", { usage: 'sort', ignorePunctuation: true });
var collator = new Intl.Collator("zh-Hans"); // Obtain the options of the Collator object.
var options = collator.resolvedOptions(); let options = collator.resolvedOptions();
let usage = options.usage; // usage = "sort"
let ignorePunctuation = options.ignorePunctuation // ignorePunctuation = true
``` ```
## CollatorOptions<sup>9+</sup> ## CollatorOptions<sup>8+</sup>
Represents the properties of a **Collator** object. Represents the properties of a **Collator** object.
...@@ -517,7 +566,7 @@ Represents the properties of a **Collator** object. ...@@ -517,7 +566,7 @@ Represents the properties of a **Collator** object.
| ----------------- | ------- | ---- | ---- | ---------------------------------------- | | ----------------- | ------- | ---- | ---- | ---------------------------------------- |
| localeMatcher | string | Yes | Yes | Locale matching algorithm. The value can be **lookup** or **best fit**.| | localeMatcher | string | Yes | Yes | Locale matching algorithm. The value can be **lookup** or **best fit**.|
| usage | string | Yes | Yes | Whether the comparison is for sorting or for searching. The value can be **sort** or **search**. | | usage | string | Yes | Yes | Whether the comparison is for sorting or for searching. The value can be **sort** or **search**. |
| sensitivity | string | Yes | Yes | Differences in the strings that lead to non-zero return values. The value can be **base**, **accent**, **case**, or **variant**.| | sensitivity | string | Yes | Yes | Differences in the strings that lead to non-zero return values. The value can be **base**, **accent**, **case**, or **letiant**.|
| ignorePunctuation | boolean | Yes | Yes | Whether punctuation is ignored. The value can be **true** or **false**. | | ignorePunctuation | boolean | Yes | Yes | Whether punctuation is ignored. The value can be **true** or **false**. |
| collation | string | Yes | Yes | Rule for sorting regions. The value can be any of the following: **big5han**, **compat**, **dict**, **direct**, **ducet**, **eor**, **gb2312**, **phonebk**, **phonetic**, **pinyin**, **reformed**, **searchjl**, **stroke**, **trad**, **unihan**, **zhuyin**.| | collation | string | Yes | Yes | Rule for sorting regions. The value can be any of the following: **big5han**, **compat**, **dict**, **direct**, **ducet**, **eor**, **gb2312**, **phonebk**, **phonetic**, **pinyin**, **reformed**, **searchjl**, **stroke**, **trad**, **unihan**, **zhuyin**.|
| numeric | boolean | Yes | Yes | Whether numeric collation is used. The value can be **true** or **false**. | | numeric | boolean | Yes | Yes | Whether numeric collation is used. The value can be **true** or **false**. |
...@@ -531,13 +580,14 @@ Represents the properties of a **Collator** object. ...@@ -531,13 +580,14 @@ Represents the properties of a **Collator** object.
constructor() constructor()
Create a **PluralRules** object. Creates a **PluralRules** object to obtain the singular-plural type of numbers.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Example** **Example**
``` ```js
var pluralRules = new Intl.PluralRules(); // Use the system locale to create a PluralRules object.
let pluralRules = new Intl.PluralRules();
``` ```
...@@ -545,19 +595,21 @@ Create a **PluralRules** object. ...@@ -545,19 +595,21 @@ Create a **PluralRules** object.
constructor(locale: string | Array&lt;string&gt;, options?: PluralRulesOptions) constructor(locale: string | Array&lt;string&gt;, options?: PluralRulesOptions)
Create a **PluralRules** object. Creates a **PluralRules** object to obtain the singular-plural type of numbers.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
Parameters **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | ---------------------------------------- | ---- | ---------------------------- | | -------------------- | ---------------------------------------- | ---- | ---------------------------- |
| locale | string \| Array&lt;string&gt; | Yes | A string containing locale information, including the language, optional script, and region.| | locale | string \| Array&lt;string&gt; | Yes | A string containing locale information, including the language, optional script, and region.|
| options<sup>9+</sup> | [PluralRulesOptions](#pluralrulesoptions9) | No | Options for creating a **PluralRules** object. | | options<sup>9+</sup> | [PluralRulesOptions](#pluralrulesoptions8) | No | Options for creating a **PluralRules** object. |
**Example** **Example**
``` ```js
var pluralRules= new Intl.PluralRules("zh-CN", {"localeMatcher": "lookup", "type": "cardinal"}); // Use locale zh-CN to create a PluralRules object. Set localeMatcher to lookup and type to cardinal.
let pluralRules= new Intl.PluralRules("zh-CN", {"localeMatcher": "lookup", "type": "cardinal"});
``` ```
...@@ -575,20 +627,27 @@ Obtains a string that represents the singular-plural type of the specified numbe ...@@ -575,20 +627,27 @@ Obtains a string that represents the singular-plural type of the specified numbe
| ---- | ------ | ---- | ------------ | | ---- | ------ | ---- | ------------ |
| n | number | Yes | Number for which the singular-plural type is to be obtained.| | n | number | Yes | Number for which the singular-plural type is to be obtained.|
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ------ | ---------------------------------------- | | ------ | ---------------------------------------- |
| 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"); // Use locale zh-Hans to create a PluralRules object.
pluralRules.select(1); let zhPluralRules = new Intl.PluralRules("zh-Hans");
// Determine the singular-plural type corresponding to number 1 in locale zh-Hans.
let plural = zhPluralRules.select(1); // plural = other
// Use locale en-US to create a PluralRules object.
let enPluralRules = new Intl.PluralRules("en-US");
// Determine the singular-plural type corresponding to number 1 in locale en-US.
plural = enPluralRules.select(1); // plural = one
``` ```
## PluralRulesOptions<sup>9+</sup> ## PluralRulesOptions<sup>8+</sup>
Represents the properties of a **PluralRules** object. Represents the properties of a **PluralRules** object.
...@@ -617,8 +676,9 @@ Creates a **RelativeTimeFormat** object. ...@@ -617,8 +676,9 @@ Creates a **RelativeTimeFormat** object.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Example** **Example**
``` ```js
var relativetimefmt = new Intl.RelativeTimeFormat(); // Use the system locale to create a RelativeTimeFormat object.
let relativetimefmt = new Intl.RelativeTimeFormat();
``` ```
...@@ -633,13 +693,14 @@ Creates a **RelativeTimeFormat** object. ...@@ -633,13 +693,14 @@ Creates a **RelativeTimeFormat** object.
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | ---------------------------------------- | ---- | ---------------------------- | | -------------------- | ---------------------------------------- | ---- | ---------------------------- |
| locale | string \| Array&lt;string&gt; | Yes | A string containing locale information, including the language, optional script, and region.| | locale | string \| Array&lt;string&gt; | Yes | A string containing locale information, including the language, optional script, and region.|
| options<sup>9+</sup> | [RelativeTimeFormatInputOptions](#relativetimeformatinputoptions9) | No | Options for creating a **RelativeTimeFormat** object. | | options<sup>9+</sup> | [RelativeTimeFormatInputOptions](#relativetimeformatinputoptions8) | No | Options for creating a **RelativeTimeFormat** object. |
**Example** **Example**
``` ```js
var relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {"localeMatcher": "lookup", "numeric": "always", "style": "long"}); // Use locale zh-CN to create a RelativeTimeFormat object. Set localeMatcher to lookup, numeric to always, and style to long.
let relativeTimeFormat = new Intl.RelativeTimeFormat("zh-CN", {"localeMatcher": "lookup", "numeric": "always", "style": "long"});
``` ```
...@@ -658,16 +719,18 @@ Formats the value and unit based on the specified locale and formatting options. ...@@ -658,16 +719,18 @@ Formats the value and unit based on the specified locale and formatting options.
| value | number | Yes | Value to format. | | value | number | Yes | Value to format. |
| unit | string | Yes | Unit to format. The value can be any of the following: **year**, **quarter**, **month**, **week**, **day**, **hour**, **minute**, **second**.| | unit | string | Yes | Unit to format. The value can be any of the following: **year**, **quarter**, **month**, **week**, **day**, **hour**, **minute**, **second**.|
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ------ | ---------- | | ------ | ---------- |
| string | Relative time after formatting.| | string | Relative time after formatting.|
**Example** **Example**
``` ```js
var relativetimefmt = new Intl.RelativeTimeFormat("zh-CN"); // Use locale zh-CN to create a RelativeTimeFormat object.
relativetimefmt.format(3, "quarter") let relativetimefmt = new Intl.RelativeTimeFormat("zh-CN");
// Obtain the localized representation (in unit of quarter) of number 3 in locale zh-CN.
let formatResult = relativetimefmt.format(3, "quarter"); // formatResult = "3 quarters later"
``` ```
...@@ -675,7 +738,7 @@ Formats the value and unit based on the specified locale and formatting options. ...@@ -675,7 +738,7 @@ Formats the value and unit based on the specified locale and formatting options.
formatToParts(value: number, unit: string): Array&lt;object&gt; formatToParts(value: number, unit: string): Array&lt;object&gt;
Returns an array of RelativeTimeFormat objects in parts for locale-aware formatting. Obtains an array of RelativeTimeFormat objects in parts for locale-aware formatting.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
...@@ -686,16 +749,17 @@ Returns an array of RelativeTimeFormat objects in parts for locale-aware formatt ...@@ -686,16 +749,17 @@ Returns an array of RelativeTimeFormat objects in parts for locale-aware formatt
| value | number | Yes | Value to format. | | value | number | Yes | Value to format. |
| unit | string | Yes | Unit to format. The value can be any of the following: **year**, **quarter**, **month**, **week**, **day**, **hour**, **minute**, **second**.| | unit | string | Yes | Unit to format. The value can be any of the following: **year**, **quarter**, **month**, **week**, **day**, **hour**, **minute**, **second**.|
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ------------------- | --------------------------- | | ------------------- | --------------------------- |
| 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"}); // Use locale en to create a RelativeTimeFormat object. Set numeric to auto.
var parts = relativetimefmt.format(10, "seconds"); let relativetimefmt = new Intl.RelativeTimeFormat("en", {"numeric": "auto"});
let parts = relativetimefmt.formatToParts(10, "seconds"); // parts = [ {type: "literal", value: "in"}, {type: "integer", value: 10, unit: "second"}, {type: "literal", value: "seconds"} ]
``` ```
...@@ -707,20 +771,23 @@ Obtains the formatting options for **RelativeTimeFormat** objects. ...@@ -707,20 +771,23 @@ Obtains the formatting options for **RelativeTimeFormat** objects.
**System capability**: SystemCapability.Global.I18n **System capability**: SystemCapability.Global.I18n
**Return Value** **Return value**
| Type | Description | | Type | Description |
| ---------------------------------------- | --------------------------------- | | ---------------------------------------- | --------------------------------- |
| [RelativeTimeFormatResolvedOptions](#relativetimeformatresolvedoptions8) | Formatting options for **RelativeTimeFormat** objects.| | [RelativeTimeFormatResolvedOptions](#relativetimeformatresolvedoptions8) | Formatting options for **RelativeTimeFormat** objects.|
**Example** **Example**
``` ```js
var relativetimefmt= new Intl.RelativeTimeFormat("en-GB"); // Use locale en-GB to create a RelativeTimeFormat object.
relativetimefmt.resolvedOptions(); let relativetimefmt= new Intl.RelativeTimeFormat("en-GB", { style: "short" });
// Obtain the options of the RelativeTimeFormat object.
let options = relativetimefmt.resolvedOptions();
let style = options.style; // style = "short"
``` ```
## RelativeTimeFormatInputOptions<sup>9+</sup> ## RelativeTimeFormatInputOptions<sup>8+</sup>
Represents the properties of a **RelativeTimeFormat** object. Represents the properties of a **RelativeTimeFormat** object.
...@@ -733,7 +800,7 @@ Represents the properties of a **RelativeTimeFormat** object. ...@@ -733,7 +800,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.
......
# I18N Error Codes # i18n Error Codes
## 890001 Incorrect Parameter Type ## 890001 Incorrect Parameter Type
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册