harmony 鸿蒙@ohos.intl (Internationalization)

2022-08-09 浏览 (845)

@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 i18n 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.

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 interfaces defined in ECMA 402. For details about the enhanced i18n capabilities, see i18n.

Modules to Import

import Intl from '@ohos.intl';

Locale

Attributes

System capability: SystemCapability.Global.I18n

NameTypeReadableWritableDescription
languagestringYesNoLanguage associated with the locale, for example, zh.
scriptstringYesNoScript type of the language, for example, Hans.
regionstringYesNoRegion associated with the locale, for example, CN.
baseNamestringYesNoBasic key information about the locale, which consists of the language, script, and region, for example, zh-Hans-CN.
caseFirststringYesNoWhether case is taken into account for the locale's collation rules. The value can be upper, lower, or false.
calendarstringYesNoCalendar 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.
collationstringYesNoRule 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.
hourCyclestringYesNoTime system for the locale. The value can be any of the following: h12, h23, h11, or h24.
numberingSystemstringYesNoNumbering 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.
numericbooleanYesNoWhether to apply special collation rules for numeric characters. The default value is false.

constructor8+

constructor()

Creates a Locale object.

System capability: SystemCapability.Global.I18n

Example

// 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();

constructor

constructor(locale: string, options?: LocaleOptions)

Creates a Locale object.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
localestringYesA 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.
optionsLocaleOptionsNoOptions for creating the Locale object.

Example

// Create a Locale object named zh-CN.
let locale = new Intl.Locale("zh-CN");
let localeID = locale.toString(); // localeID = "zh-CN"

toString

toString(): string

Obtains the string representation of a Locale object.

System capability: SystemCapability.Global.I18n

Return value

TypeDescription
stringString representation of the Locale object.

Example

// Create a Locale object named en-GB.
let locale = new Intl.Locale("en-GB");
let localeID = locale.toString(); // localeID = "en-GB"

maximize

maximize(): Locale

Maximizes information of the Locale object. If the script and locale information is missing, add the information.

System capability: SystemCapability.Global.I18n

Return value

TypeDescription
LocaleLocale object with the maximized information.

Example

// Create a Locale object named zh.
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"

minimize

minimize(): Locale

Minimizes information of the Locale object. If the script and locale information is present, delete the information.

System capability: SystemCapability.Global.I18n

Return value

TypeDescription
LocaleLocale object with the minimized information.

Example

// Create a Locale object named zh-Hans-CN.
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"

LocaleOptions6+

Represents the locale options. Since API version 9, the attributes in LocaleOptions are optional.

System capability: SystemCapability.Global.I18n

NameTypeReadableWritableDescription
calendarstringYesYesCalendar 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.
collationstringYesYesCollation 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.
hourCyclestringYesYesTime system for the locale. The value can be any of the following: h11, h12, h23, or h24.
numberingSystemstringYesYesNumbering 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.
numericbooleanYesYesWhether to use the 12-hour clock. The default value is false.
caseFirststringYesYesWhether upper case or lower case is sorted first. The value can be upper, lower, or false.

DateTimeFormat

constructor8+

constructor()

Creates a DateTimeOptions object for the specified locale.

System capability: SystemCapability.Global.I18n

Example

// Use the current system locale to create a DateTimeFormat object.
let datefmt= new Intl.DateTimeFormat();

constructor

constructor(locale: string|Array<string>, options?: DateTimeOptions)

Creates a DateTimeOptions object for the specified locale.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
localestring |Array<string>YesA string containing locale information, including the language, optional script, and region.
optionsDateTimeOptionsNoOptions for creating a DateTimeFormat object. If no options are set, the default values of year, month, and day are numeric.

Example

// 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

// 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' });

format

format(date: Date): string

Formats the specified date and time.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
dateDateYesDate and time to be formatted.

Return value

TypeDescription
stringA string containing the formatted date and time.

Example

let date = new Date(2021, 11, 17, 3, 24, 0);
// Use locale en-GB to create a DateTimeFormat object.
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"

formatRange

formatRange(startDate: Date, endDate: Date): string

Formats the specified date range.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
startDateDateYesStart date and time to be formatted.
endDateDateYesEnd date and time to be formatted.

Return value

TypeDescription
stringA string containing the formatted date and time range.

Example

let startDate = new Date(2021, 11, 17, 3, 24, 0);
let endDate = new Date(2021, 11, 18, 3, 24, 0);
// Use locale en-GB to create a DateTimeFormat object.
let datefmt = new Intl.DateTimeFormat("en-GB");
let formattedDateRange = datefmt.formatRange(startDate, endDate); // formattedDateRange = "17/12/2021-18/12/2021"

resolvedOptions

resolvedOptions(): DateTimeOptions

Obtains the formatting options for DateTimeFormat object.

System capability: SystemCapability.Global.I18n

Return value

TypeDescription
DateTimeOptionsFormatting options for DateTimeFormat objects.

Example

let datefmt = new Intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' });
// Obtain the options of the DateTimeFormat object.
let options = datefmt.resolvedOptions();
let dateStyle = options.dateStyle; // dateStyle = "full"
let timeStyle = options.timeStyle; // timeStyle = "medium"

DateTimeOptions6+

Provides the options for the DateTimeFormat object. Since API version 9, the attributes in DateTimeOptions are optional.

System capability: SystemCapability.Global.I18n

NameTypeReadableWritableDescription
localestringYesNoLocale, for example, zh-Hans-CN.
dateStylestringYesYesDate display format. The value can be long, short, medium, or full.
timeStylestringYesYesTime display format. The value can be long, short, medium, or full.
hourCyclestringYesYesTime system for the locale. The value can be any of the following: h11, h12, h23, or h24.
timeZonestringYesYesTime zone represented by a valid IANA time zone ID.
numberingSystemstringYesYesNumbering 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.
hour12booleanYesYesWhether to use the 12-hour clock. If hour12 and hourCycle are not set and the 24-hour clock is turned on, the default value of hour12 is false.
weekdaystringYesYesWorkday display format. The value can be long, short, or narrow.
erastringYesYesEra display format. The value can be long, short, or narrow.
yearstringYesYesYear display format. The value can be numeric or 2-digit.
monthstringYesYesMonth display format. The value can be any of the following: numeric, 2-digit, long, short, narrow.
daystringYesYesDay display format. The value can be numeric or 2-digit.
hourstringYesYesHour display format. The value can be numeric or 2-digit.
minutestringYesYesMinute display format. The value can be numeric or 2-digit.
secondstringYesYesSeconds display format. The value can be numeric or 2-digit.
timeZoneNamestringYesYesLocalized representation of a time zone name.
dayPeriodstringYesYesTime period display format. The value can be long, short, or narrow.
localeMatcherstringYesYesLocale matching algorithm. The value can be lookup or best fit.
formatMatcherstringYesYesFormat matching algorithm. The value can be basic or best fit.

NumberFormat

constructor8+

constructor()

Creates a NumberFormat object for the specified locale.

System capability: SystemCapability.Global.I18n

Example

// Use the current system locale to create a NumberFormat object.
let numfmt = new Intl.NumberFormat();

constructor

constructor(locale: string|Array<string>, options?: NumberOptions)

Creates a NumberFormat object for the specified locale.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
localestring |Array<string>YesA string containing locale information, including the language, optional script, and region.
optionsNumberOptionsNoOptions for creating a NumberFormat object.

Example

// 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"});

format

format(number: number): string;

Formats a number.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
numbernumberYesNumber to be formatted.

Return value

TypeDescription
stringFormatted number.

Example

// 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.
let numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
let formattedNumber = numfmt.format(1223); // formattedNumber = 1.223E3

resolvedOptions

resolvedOptions(): NumberOptions

Obtains the options of the NumberFormat object.

System capability: SystemCapability.Global.I18n

Return value

TypeDescription
NumberOptionsFormatting options for NumberFormat objects.

Example

let numfmt = new Intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
// Obtain the options of the NumberFormat object.
let options = numfmt.resolvedOptions();
let style = options.style; // style = decimal
let notation = options.notation; // notation = scientific

NumberOptions6+

Defines the device capability. Since API version 9, the attributes in NumberOptions are optional.

System capability: SystemCapability.Global.I18n

NameTypeReadableWritableDescription
localestringYesNoLocale, for example, zh-Hans-CN. The default value is the system locale.
currencystringYesYesCurrency unit, for example, EUR, CNY, or USD.
currencySignstringYesYesCurrency unit symbol. The options are "standard" and "accounting". The default value is standard.
currencyDisplaystringYesYesCurrency display mode. The value can be symbol, narrowSymbol, code, or name. The default value is symbol.
unitstringYesYesUnit name, for example, meter, inch, or hectare.
unitDisplaystringYesYesUnit display format. The value can be long, short, or narrow. The default value is short.
unitUsage8+stringYesYesUnit usage scenario. The value can be any of the following: default, area-land-agricult, area-land-commercl, area-land-residntl, length-person, length-person-small, length-rainfall, length-road, length-road-small, length-snowfall, length-vehicle, length-visiblty, length-visiblty-small, length-person-informal, length-person-small-informal, length-road-informal, speed-road-travel, speed-wind, temperature-person, temperature-weather, volume-vehicle-fuel. The default value is default.
signDisplaystringYesYesNumber sign display format. The value can be auto, never, always, or expectZero. The default value is auto.
compactDisplaystringYesYesCompact display format. The value can be long or short. The default value is short.
notationstringYesYesNumber formatting specification. The value can be standard, scientific, engineering, or compact. The default value is standard.
localeMatcherstringYesYesLocale matching algorithm. The value can be lookup or best fit. The default value is best fit.
stylestringYesYesNumber display format. The value can be decimal, currency, percent, or unit. The default value is decimal.
numberingSystemstringYesYesNumbering 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. The default value is the default numbering system of the specified locale.
useGroupingbooleanYesYesWhether to use grouping for display. The default value is auto.
minimumIntegerDigitsnumberYesYesMinimum number of digits allowed in the integer part of a number. The value ranges from 1 to 21. The default value of is 1.
minimumFractionDigitsnumberYesYesMinimum number of digits in the fraction part of a number. The value ranges from 0 to 20. The default value is 0.
maximumFractionDigitsnumberYesYesMaximum number of digits in the fraction part of a number. The value ranges from 1 to 21. The default value is 3.
minimumSignificantDigitsnumberYesYesMinimum number of the least significant digits. The value ranges from 1 to 21. The default value of is 1.
maximumSignificantDigitsnumberYesYesMaximum number of the least significant digits. The value ranges from 1 to 21. The default value is 21.

Collator8+

constructor8+

constructor()

Creates a Collator object.

System capability: SystemCapability.Global.I18n

Example

// Use the system locale to create a Collator object.
let collator = new Intl.Collator();

constructor8+

constructor(locale: string|Array<string>, options?: CollatorOptions)

Creates a Collator object.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
localestring |Array<string>YesA string containing locale information, including the language, optional script, and region.
optionsCollatorOptionsNoOptions for creating a Collator object.

Example

// 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"});

compare8+

compare(first: string, second: string): number

Compares two strings based on the sorting policy of the Collator object.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
firststringYesFirst string to compare.
secondstringYesSecond string to compare.

Return value

TypeDescription
numberComparison 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

// Use locale en-GB to create a Collator object.
let collator = new Intl.Collator("en-GB");
// Compare the sequence of the first and second strings.
let compareResult = collator.compare("first", "second"); // compareResult = -1

resolvedOptions8+

resolvedOptions(): CollatorOptions

Returns properties reflecting the locale and collation options of a Collator object.

System capability: SystemCapability.Global.I18n

Return value

TypeDescription
CollatorOptionsProperties of the Collator object.

Example

let collator = new Intl.Collator("zh-Hans", { usage: 'sort', ignorePunctuation: true });
// Obtain the options of the Collator object.
let options = collator.resolvedOptions();
let usage = options.usage; // usage = "sort"
let ignorePunctuation = options.ignorePunctuation; // ignorePunctuation = true

CollatorOptions8+

Represents the properties of a Collator object. Since API version 9, the attributes in CollatorOptions are optional.

System capability: SystemCapability.Global.I18n

NameTypeReadableWritableDescription
localeMatcherstringYesYesLocale matching algorithm. The value can be lookup or best fit. The default value is best fit.
usagestringYesYesWhether the comparison is for sorting or for searching. The value can be sort or search. The default value is sort.
sensitivitystringYesYesDifferences in the strings that lead to non-zero return values. The value can be base, accent, case, or letiant. The default value is variant.
ignorePunctuationbooleanYesYesWhether punctuation is ignored. The value can be true or false. The default value is false.
collationstringYesYesRule 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. The default value is default.
numericbooleanYesYesWhether numeric collation is used. The value can be true or false. The default value is false.
caseFirststringYesYesWhether upper case or lower case is sorted first. The value can be upper, lower, or false. The default value is false.

PluralRules8+

constructor8+

constructor()

Creates a PluralRules object to obtain the singular-plural type of numbers.

System capability: SystemCapability.Global.I18n

Example

// Use the system locale to create a PluralRules object.
let pluralRules = new Intl.PluralRules();

constructor8+

constructor(locale: string|Array<string>, options?: PluralRulesOptions)

Creates a PluralRules object to obtain the singular-plural type of numbers.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
localestring |Array<string>YesA string containing locale information, including the language, optional script, and region.
optionsPluralRulesOptionsNoOptions for creating a PluralRules object.

Example

// 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"});

select8+

select(n: number): string

Obtains a string that represents the singular-plural type of the specified number.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
nnumberYesNumber for which the singular-plural type is to be obtained.

Return value

TypeDescription
stringSingular-plural type. The value can be any of the following: zero, one, two, few, many, others.

Example

// Use locale zh-Hans to create a PluralRules object.
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

PluralRulesOptions8+

Represents the properties of a PluralRules object. Since API version 9, the attributes in PluralRulesOptions are optional.

System capability: SystemCapability.Global.I18n

NameTypeReadableWritableDescription
localeMatcherstringYesYesLocale matching algorithm. The value can be lookup or best fit. The default value is best fit.
typestringYesYesSorting type. The value can be cardinal or ordinal. The default value is cardinal.
minimumIntegerDigitsnumberYesYesMinimum number of digits allowed in the integer part of a number. The value ranges from 1 to 21. The default value of is 1.
minimumFractionDigitsnumberYesYesMinimum number of digits in the fraction part of a number. The value ranges from 0 to 20. The default value is 0.
maximumFractionDigitsnumberYesYesMaximum number of digits in the fraction part of a number. The value ranges from 1 to 21. The default value is 3.
minimumSignificantDigitsnumberYesYesMinimum number of the least significant digits. The value ranges from 1 to 21. The default value of is 1.
maximumSignificantDigitsnumberYesYesMaximum number of the least significant digits. The value ranges from 1 to 21. The default value is 21.

RelativeTimeFormat8+

constructor8+

constructor()

Creates a RelativeTimeFormat object.

System capability: SystemCapability.Global.I18n

Example

// Use the system locale to create a RelativeTimeFormat object.
let relativetimefmt = new Intl.RelativeTimeFormat();

constructor8+

constructor(locale: string|Array<string>, options?: RelativeTimeFormatInputOptions)

Creates a RelativeTimeFormat object.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
localestring |Array<string>YesA string containing locale information, including the language, optional script, and region.
optionsRelativeTimeFormatInputOptionsNoOptions for creating a RelativeTimeFormat object.

Example

// 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"});

format8+

format(value: number, unit: string): string

Formats the value and unit based on the specified locale and formatting options.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
valuenumberYesValue to format.
unitstringYesUnit to format. The value can be any of the following: year, quarter, month, week, day, hour, minute, second.

Return value

TypeDescription
stringRelative time after formatting.

Example

// Use locale zh-CN to create a RelativeTimeFormat object.
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"

formatToParts8+

formatToParts(value: number, unit: string): Array<object>

Obtains an array of RelativeTimeFormat objects in parts for locale-aware formatting.

System capability: SystemCapability.Global.I18n

Parameters

NameTypeMandatoryDescription
valuenumberYesValue to format.
unitstringYesUnit to format. The value can be any of the following: year, quarter, month, week, day, hour, minute, second.

Return value

TypeDescription
Array<object>An array of RelativeTimeFormat objects in parts.

Example

// Use locale en to create a RelativeTimeFormat object. Set numeric to auto.
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"} ]

resolvedOptions8+

resolvedOptions(): RelativeTimeFormatResolvedOptions

Obtains the formatting options for RelativeTimeFormat objects.

System capability: SystemCapability.Global.I18n

Return value

TypeDescription
RelativeTimeFormatResolvedOptionsFormatting options for RelativeTimeFormat objects.

Example

// Use locale en-GB to create a RelativeTimeFormat object.
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"

RelativeTimeFormatInputOptions8+

Represents the properties of a RelativeTimeFormat object. Since API version 9, the attributes in RelativeTimeFormatInputOptions are optional.

System capability: SystemCapability.Global.I18n

NameTypeReadableWritableDescription
localeMatcherstringYesYesLocale matching algorithm. The value can be lookup or best fit. The default value is best fit.
numericstringYesYesFormat of the output message. The value can be always or auto. The default value is always.
stylestringYesYesLength of an internationalized message. The value can be long, short, or narrow. The default value is long.

RelativeTimeFormatResolvedOptions8+

Represents the properties of a RelativeTimeFormat object.

System capability: SystemCapability.Global.I18n

NameTypeReadableWritableDescription
localestringYesYesA string containing locale information, including the language, optional script, and region.
numericstringYesYesFormat of the output message. The value can be always or auto.
stylestringYesYesLength of an internationalized message. The value can be long, short, or narrow.
numberingSystemstringYesYesNumbering system.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/1eb3812e63724f18867d7209d457b879