提交 b8c7a4fe 编写于 作者: S shawn_he

update doc

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