Class Index | File Index

Classes


Class LocaleMatcher

Create a new locale matcher instance. This is used to see which locales can be matched with each other in various ways.

The options object may contain any of the following properties:


Defined in: LocaleMatcher.js.

Class Summary
Constructor Attributes Constructor Name and Description
 
LocaleMatcher(options)
Method Summary
Method Attributes Method Name and Description
 
Return an Locale instance that is fully specified based on partial information given to the constructor of this locale matcher instance.
 
Return an Locale instance that is specified based on partial information given to the constructor of this locale matcher instance but which leaves out any part of the locale specifier that is so common that it is understood.
 
Return the locale used to construct this instance.
 
Return the macrolanguage associated with this locale.
 
Return the list of regions that this locale is contained within.
 
match(locale)
Return the degree that the given locale matches the current locale of this matcher.
 
smallestCommonRegion(otherLocale)
Find the smallest region that contains both the current locale and the other locale.
Class Detail
LocaleMatcher(options)
Parameters:
{Object} options
parameters to initialize this matcher
Method Detail
{Locale} getLikelyLocale()
Return an Locale instance that is fully specified based on partial information given to the constructor of this locale matcher instance. For example, if the locale spec given to this locale matcher instance is simply "ru" (for the Russian language), then it will fill in the missing region and script tags and return a locale with the specifier "ru-Cyrl-RU". (ie. Russian language, Cyrillic, Russian Federation). Any one or two of the language, script, or region parts may be left unspecified, and the other one or two parts will be filled in automatically. If this class has no information about the given locale, then the locale of this locale matcher instance is returned unchanged.
Returns:
{Locale} the most likely completion of the partial locale given to the constructor of this locale matcher instance

{Locale} getLikelyLocaleMinimal()
Return an Locale instance that is specified based on partial information given to the constructor of this locale matcher instance but which leaves out any part of the locale specifier that is so common that it is understood. For example, if the locale spec given to this locale matcher instance is simply "ru" (for the Russian language), then it will fill in the missing region and/or script tags and return a locale with the specifier "ru-RU". (ie. Russian language, Russian Federation). Note that the default script "Cyrl" is left out because the vast majority of text written in Russian is written with the Cyrllic script, so that part of the locale is understood and is commonly left out.

Any one or two of the language, script, or region parts may be left unspecified, and the other one or two parts will be filled in automatically. If this class has no information about the given locale, then the locale of this locale matcher instance is returned unchanged.

This method returns the same information as getLikelyLocale but with the very common parts left out.

Returns:
{Locale} the most likely "minimal" completion of the partial locale given to the constructor of this locale matcher instance where the commonly understood parts are left out.

{Locale|undefined} getLocale()
Return the locale used to construct this instance.
Returns:
{Locale|undefined} the locale for this matcher

{string} getMacroLanguage()
Return the macrolanguage associated with this locale. If the locale's language is not part of a macro-language, then the locale's language is returned as-is.
Returns:
{string} the ISO code for the macrolanguage associated with this locale, or language of the locale

{Array.} getRegionContainment()
Return the list of regions that this locale is contained within. Regions are nested, so locales can be in multiple regions. (eg. US is in Northern North America, North America, the Americas, the World.) Most regions are specified using UN.49 region numbers, though some, like "EU", are letters. If the locale is underspecified, this method will use the most likely locale method to get the region first. For example, the locale "ja" (Japanese) is most likely "ja-JP" (Japanese for Japan), and the region containment info for Japan is returned.
Returns:
{Array.} an array of region specifiers that this locale is within

{number} match(locale)
Return the degree that the given locale matches the current locale of this matcher. This method returns an integer from 0 to 100. A value of 100 is a 100% match, meaning that the two locales are exactly equivalent to each other. (eg. "ja-JP" and "ja-JP") A value of 0 means that there 0% match or that the two locales have nothing in common. (eg. "en-US" and "ja-JP")

Locale matching is not the same as equivalence, as the degree of matching is returned. (See Locale.equals for equivalence.)

The match score is calculated based on matching the 4 locale components, weighted by importance:

The score is affected by the following things: The intention of this method is that it can be used to determine compatibility of locales. For example, when a user signs up for an account on a web site, the locales that the web site supports and the locale of the user's browser may differ, and the site needs to pick the best locale to show the user. Let's say the web site supports a selection of European languages such as "it-IT", "fr-FR", "de-DE", and "en-GB". The user's browser may be set to "it-CH". The web site code can then match "it-CH" against each of the supported locales to find the one with the highest score. In this case, the best match would be "it-IT" because it shares a language and script in common with "it-CH" and differs only in the region component. It is not a 100% match, but it is pretty good. The web site may decide if the match scores all fall below a chosen threshold (perhaps 50%?), it should show the user the default language "en-GB", because that is probably a better choice than any other supported locale.

Parameters:
{Locale} locale
the other locale to match against the current one
Returns:
{number} an integer from 0 to 100 that indicates the degree to which these locales match each other

{string} smallestCommonRegion(otherLocale)
Find the smallest region that contains both the current locale and the other locale. If the current or other locales are underspecified, this method will use the most likely locale method to get their regions first. For example, the locale "ja" (Japanese) is most likely "ja-JP" (Japanese for Japan), and the region containment info for Japan is checked against the other locale's region containment info.
Parameters:
{string|Locale} otherLocale
a locale specifier or a Locale instance to compare against
Returns:
{string} the region specifier of the smallest region containing both the current locale and other locale

Documentation generated by JsDoc Toolkit 2.4.0 on Tue Jan 17 2023 15:56:28 GMT-0800 (Pacific Standard Time)