Class LocaleMatcher
Example:
LocaleMatcher matcher = LocaleMatcher.builder().setSupportedLocales("fr, en-GB, en").build(); Locale bestSupported = matcher.getBestLocale(Locale.US); // "en"
A matcher takes into account when languages are close to one another, such as Danish and Norwegian, and when regional variants are close, like en-GB and en-AU as opposed to en-US.
If there are multiple supported locales with the same (language, script, region) likely subtags, then the current implementation returns the first of those locales. It ignores variant subtags (except for pseudolocale variants) and extensions. This may change in future versions.
For example, the current implementation does not distinguish between de, de-DE, de-Latn, de-1901, de-u-co-phonebk.
If you prefer one equivalent locale over another, then provide only the preferred one, or place it earlier in the list of supported locales.
Otherwise, the order of supported locales may have no effect on the best-match results. The current implementation compares each desired locale with supported locales in the following order: 1. Default locale, if supported; 2. CLDR "paradigm locales" like en-GB and es-419; 3. other supported locales. This may change in future versions.
Often a product will just need one matcher instance, built with the languages that it supports. However, it may want multiple instances with different default languages based on additional information, such as the domain.
This class is not intended for public subclassing.
- Author:
- markdavis@google.com
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
LocaleMatcher Builder.static enum
Builder option for whether all desired locales are treated equally or earlier ones are preferred.static enum
Builder option for whether to include or ignore one-way (fallback) match data.static enum
Builder option for whether the language subtag or the script subtag is most important.static final class
Data for the best-matching pair of a desired and a supported locale. -
Constructor Summary
ConstructorsConstructorDescriptionLocaleMatcher
(LocalePriorityList supportedLocales) Copies the supported locales, preserving iteration order, and constructs a LocaleMatcher.LocaleMatcher
(String supportedLocales) Parses the string likeLocalePriorityList
does and constructs a LocaleMatcher for the supported locales parsed from the string. -
Method Summary
Modifier and TypeMethodDescriptionstatic LocaleMatcher.Builder
builder()
Returns a builder used in chaining parameters for building a LocaleMatcher.canonicalize
(ULocale locale) Partially canonicalizes a locale (language).getBestLocale
(Iterable<Locale> desiredLocales) Returns the supported locale which best matches one of the desired locales.getBestLocale
(Locale desiredLocale) Returns the supported locale which best matches the desired locale.getBestLocaleResult
(Iterable<Locale> desiredLocales) Returns the best match between the desired and supported locales.getBestLocaleResult
(Locale desiredLocale) Returns the best match between the desired locale and the supported locales.getBestMatch
(ULocale desiredLocale) Returns the supported locale which best matches the desired locale.getBestMatch
(Iterable<ULocale> desiredLocales) Returns the supported locale which best matches one of the desired locales.getBestMatch
(String desiredLocaleList) Parses the string likeLocalePriorityList
does and returns the supported locale which best matches one of the desired locales.getBestMatchResult
(ULocale desiredLocale) Returns the best match between the desired locale and the supported locales.getBestMatchResult
(Iterable<ULocale> desiredLocales) Returns the best match between the desired and supported locales.boolean
Returns true if the pair of locales matches acceptably.boolean
Returns true if the pair of locales matches acceptably.double
Deprecated.ICU 65 Build and use a matcher rather than comparing pairs of locales.toString()
-
Constructor Details
-
LocaleMatcher
Copies the supported locales, preserving iteration order, and constructs a LocaleMatcher. The first locale is used as the default locale for when there is no good match.- Parameters:
supportedLocales
- list of locales
-
LocaleMatcher
Parses the string likeLocalePriorityList
does and constructs a LocaleMatcher for the supported locales parsed from the string. The first one (in LocalePriorityList iteration order) is used as the default locale for when there is no good match.- Parameters:
supportedLocales
- the string of locales to set, to be parsed like LocalePriorityList does
-
-
Method Details
-
builder
Returns a builder used in chaining parameters for building a LocaleMatcher.- Returns:
- a new Builder object
-
getBestMatch
Returns the supported locale which best matches the desired locale.- Parameters:
desiredLocale
- Typically a user's language.- Returns:
- the best-matching supported locale.
-
getBestMatch
Returns the supported locale which best matches one of the desired locales.- Parameters:
desiredLocales
- Typically a user's languages, in order of preference (descending). (In ICU 4.4..63 this parameter had type LocalePriorityList.)- Returns:
- the best-matching supported locale.
-
getBestMatch
Parses the string likeLocalePriorityList
does and returns the supported locale which best matches one of the desired locales.- Parameters:
desiredLocaleList
- Typically a user's languages, as a string which is to be parsed like LocalePriorityList does.- Returns:
- the best-matching supported locale.
-
getBestLocale
Returns the supported locale which best matches the desired locale.- Parameters:
desiredLocale
- Typically a user's language.- Returns:
- the best-matching supported locale.
-
getBestLocale
Returns the supported locale which best matches one of the desired locales.- Parameters:
desiredLocales
- Typically a user's languages, in order of preference (descending).- Returns:
- the best-matching supported locale.
-
getBestMatchResult
Returns the best match between the desired locale and the supported locales.- Parameters:
desiredLocale
- Typically a user's language.- Returns:
- the best-matching pair of the desired and a supported locale.
-
getBestMatchResult
Returns the best match between the desired and supported locales.- Parameters:
desiredLocales
- Typically a user's languages, in order of preference (descending).- Returns:
- the best-matching pair of a desired and a supported locale.
-
getBestLocaleResult
Returns the best match between the desired locale and the supported locales.- Parameters:
desiredLocale
- Typically a user's language.- Returns:
- the best-matching pair of the desired and a supported locale.
-
getBestLocaleResult
Returns the best match between the desired and supported locales.- Parameters:
desiredLocales
- Typically a user's languages, in order of preference (descending).- Returns:
- the best-matching pair of a desired and a supported locale.
-
isMatch
Returns true if the pair of locales matches acceptably. This is influenced by Builder options such as setDirection(), setFavorSubtag(), and setMaxDistance().- Parameters:
desired
- The desired locale.supported
- The supported locale.- Returns:
- true if the pair of locales matches acceptably.
-
isMatch
Returns true if the pair of locales matches acceptably. This is influenced by Builder options such as setDirection(), setFavorSubtag(), and setMaxDistance().- Parameters:
desired
- The desired locale.supported
- The supported locale.- Returns:
- true if the pair of locales matches acceptably.
-
match
@Deprecated public double match(ULocale desired, ULocale desiredMax, ULocale supported, ULocale supportedMax) Deprecated.ICU 65 Build and use a matcher rather than comparing pairs of locales.Returns a fraction between 0 and 1, where 1 means that the languages are a perfect match, and 0 means that they are completely different.This is mostly an implementation detail, and the precise values may change over time. The implementation may use either the maximized forms or the others ones, or both. The implementation may or may not rely on the forms to be consistent with each other.
Callers should construct and use a matcher rather than match pairs of locales directly.
- Parameters:
desired
- Desired locale.desiredMax
- Maximized locale (using likely subtags).supported
- Supported locale.supportedMax
- Maximized locale (using likely subtags).- Returns:
- value between 0 and 1, inclusive.
-
canonicalize
Partially canonicalizes a locale (language). Note that for now, it is canonicalizing according to CLDR conventions (he vs iw, etc), since that is what is needed for likelySubtags.Currently, this is a much simpler canonicalization than what the ULocale class does: The language/script/region subtags are each mapped separately, ignoring the other subtags. If none of these change, then the input locale is returned. Otherwise a new ULocale with only those subtags is returned, removing variants and extensions.
- Parameters:
locale
- language/locale code- Returns:
- ULocale with remapped subtags.
-
toString
-