1.2 Abstract Operations
Where the following abstract operations take an availableLocales argument, it must be an [[AvailableLocales]] List as specified in 1.1.
1.2.1 CanonicalizeLocaleList ( locales )
The abstract operation CanonicalizeLocaleList takes the following steps:
- If locales is undefined, then
- Return a new empty List.
- Let seen be a new empty List.
- If Type(locales) is String or Type(locales) is Object and locales has an [[InitializedLocale]] internal slot, then
- Let O be CreateArrayFromList(« locales »).
- Else,
- Let O be ? ToObject(locales).
- Let len be ? ToLength(? Get(O, "length")).
- Let k be 0.
- Repeat, while k < len,
- Let Pk be ! ToString(k).
- Let kPresent be ? HasProperty(O, Pk).
- If kPresent is true, then
- Let kValue be ? Get(O, Pk).
- If Type(kValue) is not String or Object, throw a TypeError exception.
- If Type(kValue) is Object and kValue has an [[InitializedLocale]] internal slot, then
- Let tag be kValue.[[Locale]].
- Else,
- Let tag be ? ToString(kValue).
- If ! IsStructurallyValidLanguageTag(tag) is false, throw a RangeError exception.
- Let canonicalizedTag be ! CanonicalizeUnicodeLocaleId(tag).
- If canonicalizedTag is not an element of seen, append canonicalizedTag as the last element of seen.
- Increase k by 1.
- Return seen.
Note 1
Non-normative summary: The abstract operation interprets the locales argument as an array and copies its elements into a List, validating the elements as structurally valid language tags and canonicalizing them, and omitting duplicates.
Note 2
Requiring kValue to be a String or Object means that the Number value NaN will not be interpreted as the language tag "nan", which stands for Min Nan Chinese.
1.2.2 BestAvailableLocale ( availableLocales, locale )
The BestAvailableLocale abstract operation compares the provided argument locale, which must be a String value with a structurally valid and canonicalized Unicode BCP 47 locale identifier, against the locales in availableLocales and returns either the longest non-empty prefix of locale that is an element of availableLocales, or undefined if there is no such element. It uses the fallback mechanism of RFC 4647, section 3.4. The following steps are taken:
- Let candidate be locale.
- Repeat,
- If availableLocales contains candidate, return candidate.
- Let pos be the character index of the last occurrence of "-" (U+002D) within candidate. If that character does not occur, return undefined.
- If pos ≥ 2 and the character "-" occurs at index pos - 2 of candidate, decrease pos by 2.
- Let candidate be the substring of candidate from position 0, inclusive, to position pos, exclusive.
1.2.3 LookupMatcher ( availableLocales, requestedLocales )
The LookupMatcher abstract operation compares requestedLocales, which must be a List as returned by CanonicalizeLocaleList, against the locales in availableLocales and determines the best available language to meet the request. The following steps are taken:
- Let result be a new Record.
- For each element locale of requestedLocales, do
- Let noExtensionsLocale be the String value that is locale with any Unicode locale extension sequences removed.
- Let availableLocale be ! BestAvailableLocale(availableLocales, noExtensionsLocale).
- If availableLocale is not undefined, then
- Set result.[[locale]] to availableLocale.
- If locale and noExtensionsLocale are not the same String value, then
- Let extension be the String value consisting of the substring of the Unicode locale extension sequence within locale.
- Set result.[[extension]] to extension.
- Return result.
- Let defLocale be ! DefaultLocale().
- Set result.[[locale]] to defLocale.
- Return result.
Note
The algorithm is based on the Lookup algorithm described in RFC 4647 section 3.4, but options specified through Unicode locale extension sequences are ignored in the lookup. Information about such subsequences is returned separately. The abstract operation returns a record with a [[locale]] field, whose value is the language tag of the selected locale, which must be an element of availableLocales. If the language tag of the request locale that led to the selected locale contained a Unicode locale extension sequence, then the returned record also contains an [[extension]] field whose value is the substring of the Unicode locale extension sequence within the request locale language tag.
1.2.4 BestFitMatcher ( availableLocales, requestedLocales )
The BestFitMatcher abstract operation compares requestedLocales, which must be a List as returned by CanonicalizeLocaleList, against the locales in availableLocales and determines the best available language to meet the request. The algorithm is implementation dependent, but should produce results that a typical user of the requested locales would perceive as at least as good as those produced by the LookupMatcher abstract operation. Options specified through Unicode locale extension sequences must be ignored by the algorithm. Information about such subsequences is returned separately. The abstract operation returns a record with a [[locale]] field, whose value is the language tag of the selected locale, which must be an element of availableLocales. If the language tag of the request locale that led to the selected locale contained a Unicode locale extension sequence, then the returned record also contains an [[extension]] field whose value is the substring of the Unicode locale extension sequence within the request locale language tag.
1.2.5 UnicodeExtensionComponents ( extension )
The UnicodeExtensionComponents abstract operation returns the attributes and keywords from extension, which must be a String value whose contents are a Unicode locale extension sequence. If an attribute or a keyword occurs multiple times in extension, only the first occurence is returned. The following steps are taken:
- Let attributes be a new empty List.
- Let keywords be a new empty List.
- Let keyword be undefined.
- Let size be the length of extension.
- Let k be 3.
- Repeat, while k < size,
- Let e be StringIndexOf(extension, "-", k).
- If e = -1, let len be size - k; else let len be e - k.
- Let subtag be the String value equal to the substring of extension consisting of the code units at indices k (inclusive) through k + len (exclusive).
- If keyword is undefined and len ≠ 2, then
- If subtag is not an element of attributes, then
- Append subtag to attributes.
- Else if len = 2, then
- If keyword is not undefined and keywords does not contain an element whose [[Key]] is the same as keyword.[[Key]], then
- Append keyword to keywords.
- Set keyword to the Record { [[Key]]: subtag, [[Value]]: "" }.
- Else,
- If keyword.[[Value]] is the empty String, then
- Set keyword.[[Value]] to subtag.
- Else,
- Set keyword.[[Value]] to the string-concatenation of keyword.[[Value]], "-", and subtag.
- Let k be k + len + 1.
- If keyword is not undefined and keywords does not contain an element whose [[Key]] is the same as keyword.[[Key]], then
- Append keyword to keywords.
- Return the Record { [[Attributes]]: attributes, [[Keywords]]: keywords }.
1.2.6 InsertUnicodeExtensionAndCanonicalize ( locale, extension )
The InsertUnicodeExtensionAndCanonicalize abstract operation inserts extension, which must be a Unicode locale extension sequence, into locale, which must be a String value with a structurally valid and canonicalized Unicode BCP 47 locale identifier. The following steps are taken:
The following algorithm refers to UTS 35's Unicode Language and Locale Identifiers grammar.
- Assert: locale matches the
unicode_locale_id
production. - Assert: locale does not contain a Unicode locale extension sequence.
- Assert: extension is a Unicode locale extension sequence.
- Let privateIndex be StringIndexOf(locale, "-x-", 0).
- If privateIndex = -1, then
- Let locale be the string-concatenation of locale and extension.
- Else,
- Let preExtension be the substring of locale from position 0, inclusive, to position privateIndex, exclusive.
- Let postExtension be the substring of locale from position privateIndex to the end of the string.
- Let locale be the string-concatenation of preExtension, extension, and postExtension.
- Assert: ! IsStructurallyValidLanguageTag(locale) is true.
- Return ! CanonicalizeUnicodeLocaleId(locale).
1.2.7 ResolveLocale ( availableLocales, requestedLocales, options, relevantExtensionKeys, localeData )
The ResolveLocale abstract operation compares a BCP 47 language priority list requestedLocales against the locales in availableLocales and determines the best available language to meet the request. availableLocales, requestedLocales, and relevantExtensionKeys must be provided as List values, options and localeData as Records.
The following steps are taken:
- Let matcher be options.[[localeMatcher]].
- If matcher is "lookup", then
- Let r be ! LookupMatcher(availableLocales, requestedLocales).
- Else,
- Let r be ! BestFitMatcher(availableLocales, requestedLocales).
- Let foundLocale be r.[[locale]].
- Let result be a new Record.
- Set result.[[dataLocale]] to foundLocale.
- If r has an [[extension]] field, then
- Let components be ! UnicodeExtensionComponents(r.[[extension]]).
- Let keywords be components.[[Keywords]].
- Let supportedExtension be "-u".
- For each element key of relevantExtensionKeys, do
- Let foundLocaleData be localeData.[[<foundLocale>]].
- Assert: Type(foundLocaleData) is Record.
- Let keyLocaleData be foundLocaleData.[[<key>]].
- Assert: Type(keyLocaleData) is List.
- Let value be keyLocaleData[0].
- Assert: Type(value) is either String or Null.
- Let supportedExtensionAddition be "".
- If r has an [[extension]] field, then
- If keywords contains an element whose [[Key]] is the same as key, then
- Let entry be the element of keywords whose [[Key]] is the same as key.
- Let requestedValue be entry.[[Value]].
- If requestedValue is not the empty String, then
- If keyLocaleData contains requestedValue, then
- Let value be requestedValue.
- Let supportedExtensionAddition be the string-concatenation of "-", key, "-", and value.
- Else if keyLocaleData contains "true", then
- Let value be "true".
- Let supportedExtensionAddition be the string-concatenation of "-" and key.
- If options has a field [[<key>]], then
- Let optionsValue be options.[[<key>]].
- Assert: Type(optionsValue) is either String, Undefined, or Null.
- If Type(optionsValue) is String, then
- Let optionsValue be the string optionsValue after performing the algorithm steps to transform Unicode extension values to canonical syntax per Unicode Technical Standard #35 LDML § 3.2.1 Canonical Unicode Locale Identifiers, treating key as
ukey
and optionsValue as uvalue
productions. - Let optionsValue be the string optionsValue after performing the algorithm steps to replace Unicode extension values with their canonical form per Unicode Technical Standard #35 LDML § 3.2.1 Canonical Unicode Locale Identifiers, treating key as
ukey
and optionsValue as uvalue
productions. - If optionsValue is the empty String, then
- Let optionsValue be "true".
- If keyLocaleData contains optionsValue, then
- If SameValue(optionsValue, value) is false, then
- Let value be optionsValue.
- Let supportedExtensionAddition be "".
- Set result.[[<key>]] to value.
- Append supportedExtensionAddition to supportedExtension.
- If the number of elements in supportedExtension is greater than 2, then
- Let foundLocale be InsertUnicodeExtensionAndCanonicalize(foundLocale, supportedExtension).
- Set result.[[locale]] to foundLocale.
- Return result.
Note
Non-normative summary: Two algorithms are available to match the locales: the Lookup algorithm described in RFC 4647 section 3.4, and an implementation dependent best-fit algorithm. Independent of the locale matching algorithm, options specified through Unicode locale extension sequences are negotiated separately, taking the caller's relevant extension keys and locale data as well as client-provided options into consideration. The abstract operation returns a record with a [[locale]] field whose value is the language tag of the selected locale, and fields for each key in relevantExtensionKeys providing the selected value for that key.
1.2.8 LookupSupportedLocales ( availableLocales, requestedLocales )
The LookupSupportedLocales abstract operation returns the subset of the provided BCP 47 language priority list requestedLocales for which availableLocales has a matching locale when using the BCP 47 Lookup algorithm. Locales appear in the same order in the returned list as in requestedLocales. The following steps are taken:
- Let subset be a new empty List.
- For each element locale of requestedLocales, do
- Let noExtensionsLocale be the String value that is locale with any Unicode locale extension sequences removed.
- Let availableLocale be ! BestAvailableLocale(availableLocales, noExtensionsLocale).
- If availableLocale is not undefined, append locale to the end of subset.
- Return subset.
1.2.9 BestFitSupportedLocales ( availableLocales, requestedLocales )
The BestFitSupportedLocales abstract operation returns the subset of the provided BCP 47 language priority list requestedLocales for which availableLocales has a matching locale when using the Best Fit Matcher algorithm. Locales appear in the same order in the returned list as in requestedLocales. The steps taken are implementation dependent.
1.2.10 SupportedLocales ( availableLocales, requestedLocales, options )
The SupportedLocales abstract operation returns the subset of the provided BCP 47 language priority list requestedLocales for which availableLocales has a matching locale. Two algorithms are available to match the locales: the Lookup algorithm described in RFC 4647 section 3.4, and an implementation dependent best-fit algorithm. Locales appear in the same order in the returned list as in requestedLocales. The following steps are taken:
- Set options to ? CoerceOptionsToObject(options).
- Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
- If matcher is "best fit", then
- Let supportedLocales be BestFitSupportedLocales(availableLocales, requestedLocales).
- Else,
- Let supportedLocales be LookupSupportedLocales(availableLocales, requestedLocales).
- Return CreateArrayFromList(supportedLocales).
1.2.11 GetOptionsObject ( options )
The abstract operation GetOptionsObject returns an Object suitable for use with GetOption, either options itself or a default empty Object.
It throws a TypeError if options is not undefined and not an Object.
- If options is undefined, then
- Return OrdinaryObjectCreate(null).
- If Type(options) is Object, then
- Return options.
- Throw a TypeError exception.
1.2.12 CoerceOptionsToObject ( options )
The abstract operation CoerceOptionsToObject coerces options into an Object suitable for use with GetOption, defaulting to an empty Object.
Because it coerces non-null primitive values into objects, its use is discouraged for new functionality in favour of GetOptionsObject.
- If options is undefined, then
- Return OrdinaryObjectCreate(null).
- Return ? ToObject(options).
1.2.13 GetOption ( options, property, type, values, default )
The abstract operation GetOption takes arguments options (an Object), property (a property key), type (boolean, number, or string), values (empty or a List of ECMAScript language values), and default (required or an ECMAScript language value). It extracts the value of the specified property of options, converts it to the required type, checks whether it is allowed by values if values is not empty, and substitutes default if the value is undefined. It performs the following steps when called:
- Let value be ? Get(options, property).
- If value is undefined, then
- If default is required, throw a RangeError exception.
- Return default.
- If type is boolean, then
- Set value to ToBoolean(value).
- Else if type is number, then
- Set value to ? ToNumber(value).
- If value is NaN, throw a RangeError exception.
- Else,
- Assert: type is string.
- Set value to ? ToString(value).
- If values is not empty and values does not contain value, throw a RangeError exception.
- Return value.
1.2.14 GetBooleanOrStringNumberFormatOption ( options, property, stringValues, fallback )
The abstract operation GetBooleanOrStringNumberFormatOption takes arguments options (an Object), property (a property key), stringValues (a List of Strings), and fallback (an ECMAScript language value) and returns either a normal completion containing either a Boolean, String, or fallback, or a throw completion. It extracts the value of the property named property from the provided options object. It returns fallback if that value is undefined, true if that value is true, false if that value coerces to false, and otherwise coerces it to a String and returns the result if it is allowed by stringValues. It performs the following steps when called:
- Let value be ? Get(options, property).
- If value is undefined, return fallback.
- If value is true, return true.
- If ToBoolean(value) is false, return false.
- Let value be ? ToString(value).
- If stringValues does not contain value, throw a RangeError exception.
- Return value.
1.2.15 DefaultNumberOption ( value, minimum, maximum, fallback )
The abstract operation DefaultNumberOption converts value to a Number value, checks whether it is in the allowed range, and fills in a fallback value if necessary.
- If value is undefined, return fallback.
- Set value to ? ToNumber(value).
- If value is NaN or less than minimum or greater than maximum, throw a RangeError exception.
- Return floor(value).
1.2.16 GetNumberOption ( options, property, minimum, maximum, fallback )
The abstract operation GetNumberOption extracts the value of the property named property from the provided options object, converts it to a Number value, checks whether it is in the allowed range, and fills in a fallback value if necessary.
- Assert: Type(options) is Object.
- Let value be ? Get(options, property).
- Return ? DefaultNumberOption(value, minimum, maximum, fallback).
1.2.17 PartitionPattern ( pattern )
The PartitionPattern abstract operation is called with argument pattern.
This abstract operation parses an abstract pattern string into a list of Records with two fields, [[Type]] and [[Value]].
The [[Value]] field will be a String value if [[Type]] is "literal", and undefined otherwise.
The syntax of the abstract pattern strings is an implementation detail and is not exposed to users of ECMA-402.
The following steps are taken:
- Let result be a new empty List.
- Let beginIndex be StringIndexOf(pattern, "{", 0).
- Let endIndex be 0.
- Let nextIndex be 0.
- Let length be the number of code units in pattern.
- Repeat, while beginIndex is an integer index into pattern,
- Set endIndex to StringIndexOf(pattern, "}", beginIndex).
- Assert: endIndex is greater than beginIndex.
- If beginIndex is greater than nextIndex, then
- Let literal be a substring of pattern from position nextIndex, inclusive, to position beginIndex, exclusive.
- Append a new Record { [[Type]]: "literal", [[Value]]: literal } as the last element of the list result.
- Let p be the substring of pattern from position beginIndex, exclusive, to position endIndex, exclusive.
- Append a new Record { [[Type]]: p, [[Value]]: undefined } as the last element of the list result.
- Set nextIndex to endIndex + 1.
- Set beginIndex to StringIndexOf(pattern, "{", nextIndex).
- If nextIndex is less than length, then
- Let literal be the substring of pattern from position nextIndex, inclusive, to position length, exclusive.
- Append a new Record { [[Type]]: "literal", [[Value]]: literal } as the last element of the list result.
- Return result.