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:
- locale - the locale instance or locale spec to match
- onLoad - a callback function to call when the locale matcher object is fully loaded. When the onLoad option is given, the locale matcher object will attempt to load any missing locale data using the ilib loader callback. When the constructor is done (even if the data is already preassembled), the onLoad function is called with the current instance as a parameter, so this callback can be used with preassembled or dynamic loading or a mix of the two.
- sync - tell whether to load any missing locale data synchronously or asynchronously. If this option is given as "false", then the "onLoad" callback must be given, as the instance returned from this constructor will not be usable for a while.
- loadParams - an object containing parameters to pass to the loader callback function when locale data is missing. The parameters are not interpretted or modified in any way. They are simply passed along. The object may contain any property/value pairs as long as the calling code is in agreement with the loader callback function as to what those parameters mean.
Defined in: LocaleMatcher.js.
Constructor Attributes | Constructor Name and Description |
---|---|
LocaleMatcher(options)
|
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 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|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:
- language - this accounts for 50% of the match score
- region - accounts for 25% of the match score
- script - accounts for 20% of the match score
- variant - accounts for 5% of the match score
- A large language score is given when the language components of the locales match exactly.
- Higher language scores are given when the languages are linguistically close to each other, such as dialects.
- A small score is given when two languages are in the same linguistic family, but one is not a dialect of the other, such as German and Dutch.
- A large region score is given when two locales share the same region.
- A smaller region score is given when one region is contained within another. For example, Hong Kong is part of China, so a moderate score is given instead of a full score.
- A small score is given if two regions are geographically close to each other or are tied by history. For example, Ireland and Great Britain are both adjacent and tied by history, so they receive a moderate score.
- A high script score is given if the two locales share the same script. The legibility of a common script means that there is some small kinship of the different languages.
- A high variant score is given if the two locales share the same variant. Full score is given when both locales have no variant at all.
- Locale components that are unspecified in both locales are given high scores.
- Locales where a particular locale component is missing in only one locale can still match when the default for that locale component matches the component in the other locale. The default value for the missing component is determined using the likely locales data. (See getLikelyLocale()) For example, "en-US" and "en-Latn-US" receive a high script score because the default script for "en" is "Latn".
- 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