# Internationalization (i18n) >![](../../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 contains enhanced i18n APIs, which are not defined in ECMA 402. ## Modules to Import ``` import i18n from '@ohos.i18n'; ``` ## Required Permissions None ## i18n.getDisplayLanguage getDisplayLanguage\(language: string, locale: string, sentenceCase?: boolean\): string Obtains the localized script for the specified language. - Parameters

Name

Type

Mandatory

Description

language

string

Yes

Specified language.

locale

string

Yes

Locale ID.

sentenceCase

boolean

No

Whether to use sentence case for the localized script.

- Return values

Type

Description

string

Localized script for the specified language.

- Example ``` i18n.getDisplayLanguage("zh", "en-GB", true); i18n.getDisplayLanguage("zh", "en-GB"); ``` ## i18n.getDisplayCountry getDisplayCountry\(country: string, locale: string, sentenceCase?: boolean\): string 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 values

Type

Description

string

Localized script for the specified country.

- Example ``` i18n.getDisplayCountry("zh-CN", "en-GB", true); i18n.getDisplayCountry("zh-CN", "en-GB"); ``` ## i18n.isRTL8+ isRTL\(locale: string\): boolean Checks whether the localized script for the specified language is displayed from right to left. - Parameters

Name

Type

Description

locale

string

Locale ID.

- Return values

Type

Description

boolean

The value true indicates that the localized script is displayed from right to left, and value false indicates the opposite.

- Example ``` i18n.isRTL("zh-CN");// Since Chinese is not written from right to left, false is returned. i18n.isRTL("ar-EG");// Since Arabic is written from right to left, true is returned. ``` ## i18n.getSystemLanguage getSystemLanguage\(\): string Obtains the system language. - Return values

Type

Description

string

System language ID.

- Example ``` i18n.getSystemLanguage(); ``` ## i18n.getSystemRegion getSystemRegion\(\): string Obtains the system region. - Return values

Type

Description

string

System region ID.

- Example ``` i18n.getSystemRegion(); ``` ## i18n.getSystemLocale getSystemLocale\(\): string Obtains the system locale. - Return values

Type

Description

string

System locale ID.

- Example ``` i18n.getSystemLocale(); ``` ## i18n.getCalendar8+ getCalendar\(locale: string, type? : string\): Calendar Obtains a **Calendar** object. - Parameters

Name

Type

Mandatory

Description

locale

string

Yes

Valid locale value, for example, zh-Hans-CN.

type

string

No

Valid calendar type. Currently, the valid types are as follows: buddhist, chinese, coptic, ethiopic, hebrew, gregory, indian, islamic_civil, islamic_tbla, islamic_umalqura, japanese, and persian. If this parameter is left unspecified, the default calendar type of the specified locale is used.

- Return values

Type

Description

Calendar

Calendar object.

- Example ``` i18n.getCalendar("zh-Hans", "gregory"); ``` ## Calendar8+ ### setTime8+ setTime\(date: Date\): void Sets the date for this **Calendar** object. - Parameters

Name

Type

Mandatory

Description

date

Date

Yes

Date to be set for the Calendar object.

- Example ``` var calendar = I18n.getCalendar("en-US", "gregory"); var date = new Date(2021, 10, 7, 8, 0, 0, 0); calendar.setTime(date); ``` ### setTime8+ setTime\(time: number\): void Sets the date and time for this **Calendar** object. The value is represented by the number of milliseconds that have elapsed since the Unix epoch \(00:00:00 UTC on January 1, 1970\). - Parameters

Name

Type

Mandatory

Description

time

number

Yes

Number of milliseconds that have elapsed since the Unix epoch.

- Example ``` var calendar = I18n.getCalendar("en-US", "gregory"); calendar.setTime(10540800000); ``` ### set8+ set\(year: number, month: number, date:number, hour?: number, minute?: number, second?: number\): void Sets the year, month, day, hour, minute, and second for this **Calendar** object. - Parameters

Name

Type

Mandatory

Description

year

number

Yes

Year to set.

month

number

Yes

Month to set.

date

number

Yes

Day to set.

hour

number

No

Hour to set.

minute

number

No

Minute to set.

second

number

No

Second to set.

- Example ``` var calendar = i18n.getCalendar("zh-Hans"); calendar.setTime(2021, 10, 1, 8, 0, 0); // set time to 2021.10.1 08:00:00 ``` ### setTimeZone8+ setTimeZone\(timezone: string\): void Sets the time zone of this **Calendar** object. - Parameters

Name

Type

Mandatory

Description

timezone

string

Yes

Time zone, for example, Asia/Shanghai.

- Example ``` var calendar = i18n.getCalendar("zh-Hans"); calendar.setTimeZone("Asia/Shanghai"); ``` ### getTimeZone8+ getTimeZone\(\): string Obtains the time zone of this **Calendar** object. - Return values

Type

Description

string

Time zone of the Calendar object.

- Example ``` var calendar = i18n.getCalendar("zh-Hans"); calendar.setTimeZone("Asia/Shanghai"); calendar.getTimeZone(); // Asia/Shanghai" ``` ### getFirstDayOfWeek8+ getFirstDayOfWeek\(\): number Obtains the start day of a week for this **Calendar** object. - Return values

Type

Description

number

Start day of a week. The value 1 indicates Sunday, and value 7 indicates Saturday.

- Example ``` var calendar = I18n.getCalendar("en-US", "gregory"); calendar.getFirstDayOfWeek(); ``` ### setFirstDayOfWeek8+ setFirstDayOfWeek\(value: number\): void Sets the start day of a week for this **Calendar** object. - Parameters

Name

Type

Mandatory

Description

value

number

No

Start day of a week. The value 1 indicates Sunday, and value 7 indicates Saturday.

- Example ``` var calendar = i18n.getCalendar("zh-Hans"); calendar.setFirstDayOfWeek(0); ``` ### getMinimalDaysInFirstWeek8+ getMinimalDaysInFirstWeek\(\): number Obtains the minimum number of days in the first week of a year. - Return values

Type

Description

number

Minimum number of days in the first week of a year.

- Example ``` var calendar = i18n.getCalendar("zh-Hans"); calendar.getMinimalDaysInFirstWeek(); ``` ### setMinimalDaysInFirstWeek8+ setMinimalDaysInFirstWeek\(value: number\): void Sets the minimum number of days in the first week of a year. - Parameters

Name

Type

Mandatory

Description

value

number

No

Minimum number of days in the first week of a year.

- Example ``` var calendar = i18n.getCalendar("zh-Hans"); calendar.setMinimalDaysInFirstWeek(3); ``` ### get8+ get\(field: string\): number Obtains the value of the specified field in the **Calendar** object. - Parameters

Name

Type

Mandatory

Description

field

string

Yes

Value of the specified field in the Calendar object. Currently, the valid fields are as follows: era, year, month, week_of_year, week_of_month, date, day_of_year, day_of_week, day_of_week_in_month, hour, hour_of_day, minute, second, millisecond, zone_offset, dst_offset, year_woy, dow_local, extended_year, julian_day, milliseconds_in_day, and is_leap_month.

- Return values

Type

Description

number

Value of the specified field. For example, if the year in the internal date of this Calendar object is 1990, the get("year") function will return 1990.

- Example ``` var calendar = i18n.getCalendar("zh-Hans"); calendar.setTime(2021, 10, 1, 8, 0, 0); // set time to 2021.10.1 08:00:00 calendar.get("hour_of_day"); // 8 ``` ### getDisplayName8+ getDisplayName\(locale: string\): string Obtains the name of the **Calendar** object displayed for the specified locale. - Parameters

Name

Type

Mandatory

Description

locale

string

Yes

Locale for which the name of the Calendar object is displayed. For example, if locale is en-US, the name of the Buddhist calendar will be Buddhist Calendar.

- Return values

Type

Description

string

Name of the Calendar object displayed for the specified locale.

- Example ``` var calendar = i18n.getCalendar("en-US", "buddhist"); calendar.getDisplayName("zh"); // Obtain the name of the Buddhist calendar in zh. ``` ### isWeekend8+ isWeekend\(date?: Date\): boolean Checks whether the specified date in this **Calendar** object is a weekend. - Parameters

Name

Type

Mandatory

Description

date

Date

No

Specified date in this Calendar object. If this parameter is left unspecified, the system checks whether the current date in the Calendar object is a weekend.

- Return values

Type

Description

boolean

The value true indicates that the date is a weekend, and value false indicates a weekday.

- Example ``` var calendar = i18n.getCalendar("zh-Hans"); calendar.setTime(2021, 11, 11, 8, 0, 0); // Set the time to 2021.11.11 08:00:00. calendar.isWeekend(); // false var date = new Date(2011, 11, 6, 9, 0, 0); calendar.isWeekend(date); // true ``` ## PhoneNumberFormat8+ ### constructor8+ constructor\(country: string, options?: PhoneNumberFormatOptions\) Creates a **PhoneNumberFormat** object. Parameters

Name

Type

Mandatory

Description

country

string

Yes

Country or region to which the phone number to be formatted belongs.

options

PhoneNumberFormatOptions

No

Options of the PhoneNumberFormat object.

- Example ``` var phoneNumberFormat= new i18n.PhoneNumberFormat("CN", {"type": "E164"}); ``` ### isValidNumber8+ isValidNumber\(number: string\): boolean Checks whether the format of the specified phone number is valid. - Parameters

Name

Type

Mandatory

Description

number

string

Yes

Phone number to be checked.

- Return values

Type

Description

boolean

The value true indicates the phone number format is valid, and value false indicates the opposite.

- Example ``` var phonenumberfmt = new i18n.PhoneNumberFormat("CN"); phonenumberfmt.isValidNumber("15812312312"); ``` ### format8+ format\(number: string\): string Formats a phone number. - Parameters

Name

Type

Mandatory

Description

number

string

Yes

Phone number to be formatted.

- Return values

Type

Description

string

Formatted phone number.

- Example ``` var phonenumberfmt = new i18n.PhoneNumberFormat("CN"); phonenumberfmt.format("15812312312"); ``` ## PhoneNumberFormatOptions8+ Defines the options for this **PhoneNumberFormat** object.

Name

Type

Readable

Writable

Description

type

string

Yes

Yes

Format type of a phone number. The available options are as follows: E164, INTERNATIONAL, NATIONAL, and RFC3966.

## UnitInfo8+ Defines the measurement unit information.

Name

Type

Readable

Writable

Description

unit

string

Yes

Yes

Name of the measurement unit, for example, meter, inch, or cup.

measureSystem

string

Yes

Yes

Measurement system. The value can be SI, US, or UK.

## Util8+ ### unitConvert8+ unitConvert\(fromUnit: UnitInfo, toUnit: UnitInfo, value: number, locale: string, style?: string\): string Converts one measurement unit into another and formats the unit based on the specified locale and style. - Parameters

Name

Type

Mandatory

Description

fromUnit

UnitInfo

Yes

Measurement unit to be converted.

toUnit

UnitInfo

Yes

Measurement unit to be converted to.

value

number

Yes

Value of the measurement unit to be converted.

locale

string

Yes

Locale used for formatting, for example, zh-Hans-CN.

style

string

No

Style used for formatting. The value can be long, short, or medium.

- Return values

Type

Description

string

Character string obtained after formatting based on the measurement unit specified by toUnit.

- Example ``` I18n.Util.unitConvert({unit: "cup", measureSystem: "US"}, {unit: "liter", measureSystem: "SI"}, 1000, "en-US", "long"); ``` ## i18n.getInstance8+ getInstance\(locale?: string\): IndexUtil Creates an **IndexUtil** object. - Parameters

Name

Type

Mandatory

Description

locale

string

No

A string containing locale information, including the language, optional script, and region.

- Return values

Type

Description

IndexUtil

IndexUtil object mapping to the specified locale.

- Example ``` var indexUtil= i18n.getInstance("zh-CN"); ``` ## IndexUtil8+ ### getIndexList8+ getIndexList\(\): Array Obtains the index list for this **locale** object. - Return values

Type

Description

Array<string>

Index list for this locale.

- Example ``` var indexUtil = i18n.getInstance("zh-CN"); var indexList = indexUtil.getIndexList(); ``` ### addLocale8+ addLocale\(locale: string\) Adds the index of the new **locale** object to the index list. - Parameters

Name

Type

Mandatory

Description

locale

string

Yes

A string containing locale information, including the language, optional script, and region.

- Example ``` var indexUtil = i18n.getInstance("zh-CN"); indexUtil.addLocale("en-US"); ``` ### getIndex8+ getIndex\(text: string\): string Obtains the index of a **text** object. - Parameters

Name

Type

Mandatory

Description

text

string

Yes

text object whose index is to be obtained.

- Return values

Type

Description

string

Index of the text object.

- Example ``` var indexUtil= i18n.getInstance("zh-CN"); indexUtil.getIndex("hi"); // Return h. ``` ## Character8+ ### isDigit8+ isDigit\(char: string\): boolean Checks whether the input character string is comprised of digits. - Parameters

Name

Type

Mandatory

Description

char

string

Yes

Input character.

- Return values

Type

Description

boolean

The value true indicates that the input character is a digit, and value false indicates the opposite.

- Example ``` var isdigit = Character.isDigit("1"); // Return true. ``` ### isSpaceChar8+ isSpaceChar\(char: string\): boolean Checks whether the input character is comprised of space. - Parameters

Name

Type

Mandatory

Description

char

string

Yes

Input character.

- Return values

Type

Description

boolean

The value true indicates that the input character is a space, and value false indicates the opposite.

- Example ``` var isspacechar = Character.isSpaceChar("a"); // Return false. ``` ### isWhitespace8+ isWhitespace\(char: string\): boolean Checks whether the input character is comprised of white space. - Parameters

Name

Type

Mandatory

Description

char

string

Yes

Input character.

- Return values

Type

Description

boolean

The value true indicates that the input character is a white space, and value false indicates the opposite.

- Example ``` var iswhitespace = Character.isWhitespace("a"); // Return false. ``` ### isRTL8+ isRTL\(char: string\): boolean Checks whether the input character string is of the right to left \(RTL\) language. - Parameters

Name

Type

Mandatory

Description

char

string

Yes

Input character.

- Return values

Type

Description

boolean

The value true indicates that the input character is of the RTL language, and value false indicates the opposite.

- Example ``` var isrtl = Character.isRTL("a"); // Return false. ``` ### isIdeograph8+ isIdeograph\(char: string\): boolean Checks whether the input character string is comprised of ideographic characters. - Parameters

Name

Type

Mandatory

Description

char

string

Yes

Input character.

- Return values

Type

Description

boolean

The value true indicates that the input character is an ideographic character, and value false indicates the opposite.

- Example ``` var isideograph = Character.isIdeograph("a"); // Return false. ``` ### isLetter8+ isLetter\(char: string\): boolean Checks whether the input character string is comprised of letters. - Parameters

Name

Type

Mandatory

Description

char

string

Yes

Input character.

- Return values

Type

Description

boolean

The value true indicates that the input character is a letter, and value false indicates the opposite.

- Example ``` var isletter = Character.isLetter("a"); // Return true. ``` ### isLowerCase8+ isLowerCase\(char: string\): boolean Checks whether the input character is comprised of lowercase letters. - Parameters

Name

Type

Mandatory

Description

char

string

Yes

Input character.

- Return values

Type

Description

boolean

The value true indicates that the input character is a lowercase letter, and value false indicates the opposite.

- Example ``` var islowercase = Character.isLowerCase("a"); // Return true. ``` ### isUpperCase8+ isUpperCase\(char: string\): boolean Checks whether the input character is comprised of uppercase letters. - Parameters

Name

Type

Mandatory

Description

char

string

Yes

Input character.

- Return values

Type

Description

boolean

The value true indicates that the input character is an uppercase letter, and value false indicates the opposite.

- Example ``` var isuppercase = Character.isUpperCase("a"); // Return false. ``` ### getType8+ getType\(char: string\): string Obtains the type of the input character string. - Parameters

Name

Type

Mandatory

Description

char

string

Yes

Input character.

- Return values

Type

Description

string

Type of the input character.

- Example ``` var type = Character.getType("a"); ``` ## i18n.getLineInstance8+ getLineInstance\(locale: string\): BreakIterator Obtains a [BreakIterator](#section1312302611613) object for text segmentation. - Parameters

Name

Type

Mandatory

Description

locale

string

Yes

Valid locale value, for example, zh-Hans-CN. The BreakIterator object segments text according to the rules of the specified locale.

- Return values

Type

Description

BreakIterator

Break iterator used for text segmentation.

- Example ``` i18n.getLineInstance("en"); ``` ## BreakIterator8+ ### setLineBreakText8+ setLineBreakText\(text: string\): void Sets the text to be processed by the [BreakIterator](#section1312302611613) object. - Parameters

Name

Type

Mandatory

Description

text

string

Yes

Text to be processed by the BreakIterator object.

- Example ``` iterator = I18n.getLineInstance("en"); iterator.setLineBreakText("Apple is my favorite fruit."); ``` ### getLineBreakText8+ getLineBreakText\(\): string Obtains the text being processed by the [BreakIterator](#section1312302611613) object. - Return values

Type

Description

string

Text being processed by the BreakIterator object.

- Example ``` iterator = I18n.getLineInstance("en"); iterator.setLineBreakText("Apple is my favorite fruit."); iterator.getLineBreakText(); // Apple is my favorite fruit. ``` ### current8+ current\(\): number Obtains the position of the [BreakIterator](#section1312302611613) object in the text being processed. - Return values

Type

Description

number

Position of the BreakIterator object in the text being processed.

- Example ``` iterator = I18n.getLineInstance("en"); iterator.setLineBreakText("Apple is my favorite fruit."); breakIter.current(); // 0 ``` ### first8+ first\(\): number Puts the [BreakIterator](#section1312302611613) object to the first text boundary, which is always at the beginning of the processed text. - Return values

Type

Description

number

Offset to the first text boundary of the processed text.

- Example ``` iterator = I18n.getLineInstance("en"); iterator.setLineBreakText("Apple is my favorite fruit."); breakIter.first(); // 0 ``` ### last8+ last\(\): number Puts the [BreakIterator](#section1312302611613) object to the last text boundary, which is always the next position after the end of the processed text. - Return values

Type

Description

number

Offset of the last text boundary of the processed text.

- Example ``` iterator = I18n.getLineInstance("en"); iterator.setLineBreakText("Apple is my favorite fruit."); iterator.last(); // 27 ``` ### next8+ next\(index?: number\): number Moves the [BreakIterator](#section1312302611613) object backward by the specified number of text boundaries if the specified index is a positive number. If the index is a negative number, the [BreakIterator](#section1312302611613) object will be moved forward by the corresponding number of text boundaries. If no index is specified, the index will be treated as **1**. - Parameters

Name

Type

Mandatory

Description

index

number

No

Number of text boundaries by which the BreakIterator object is moved. A positive value indicates that the text boundary is moved backward, and a negative value indicates the opposite. If no index is specified, the index will be treated as 1.

- Return values

Type

Description

number

Position of the BreakIterator object in the text after it is moved by the specified number of text boundaries. The value -1 is returned if the position of the BreakIterator object is outside of the processed text after it is moved by the specified number of text boundaries.

- Example ``` iterator = I18n.getLineInstance("en"); iterator.setLineBreakText("Apple is my favorite fruit."); iterator.first(); // 0 iterator.next(); // 6 iterator.next(10); // -1 ``` ### previous8+ previous\(\): number Moves the [BreakIterator](#section1312302611613) object to the previous text boundary. - Return values

Type

Description

number

Position of the BreakIterator object in the text after it is moved to the previous text boundary. The value -1 is returned if the position of the BreakIterator object is outside of the processed text after it is moved by the specified number of text boundaries.

- Example ``` iterator = I18n.getLineInstance("en"); iterator.setLineBreakText("Apple is my favorite fruit."); iterator.first(); // 0 iterator.next(3); // 12 iterator.previous(); // 9 ``` ### following8+ following\(offset: number\): number Moves the [BreakIterator](#section1312302611613) object to the text boundary after the position specified by the offset. - Parameters

Name

Type

Mandatory

Description

offset

number

Yes

Offset to the position before the text boundary to which the BreakIterator object is moved.

- Return values

Type

Description

number

The value -1 is returned if the text boundary to which the BreakIterator object is moved is outside of the processed text.

- Example ``` iterator = I18n.getLineInstance("en"); iterator.setLineBreakText("Apple is my favorite fruit."); iterator.following(0); // 6 iterator.following(100); // -1 iterator.current(); // 27 ``` ### isBoundary8+ isBoundary\(offset: number\): boolean Checks whether the position specified by the offset is a text boundary. If **true** is returned, the [BreakIterator](#section1312302611613) object is moved to the position specified by the offset. If **false** is returned, the [BreakIterator](#section1312302611613) object is moved to the text boundary after the position specified by the offset, which is equivalent to calling [following\(offset\)](#section1743155314301). - Parameters

Name

Type

Mandatory

Description

offset

number

Yes

Position to check.

- Return values

Type

Description

boolean

The value true indicates that the position specified by the offset is a text boundary, and value false indicates the opposite.

- Example ``` iterator = I18n.getLineInstance("en"); iterator.setLineBreakText("Apple is my favorite fruit."); iterator.isBoundary(0); // true; iterator.isBoundary(5); // false; ```