class LocaleMatcher
Defined at line 186 of file ../../third_party/icu/latest/source/common/unicode/localematcher.h
Immutable class that picks the best match between a user's desired locales and
an application's supported locales.
Movable but not copyable.
Example:
UErrorCode errorCode = U_ZERO_ERROR;
LocaleMatcher matcher = LocaleMatcher::Builder().setSupportedLocales("fr, en-GB, en").build(errorCode);
Locale *bestSupported = matcher.getBestLocale(Locale.US, errorCode); // "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.
ICU 65
Public Methods
void LocaleMatcher (LocaleMatcher && src)
Move copy constructor; might modify the source.
This matcher will have the same settings that the source matcher had.
Parameters
void ~LocaleMatcher ()
Destructor.
ICU 65
LocaleMatcher & operator= (LocaleMatcher && src)
Move assignment operator; might modify the source.
This matcher will have the same settings that the source matcher had.
The behavior is undefined if *this and src are the same object.
Parameters
Returns
*this
ICU 65
const Locale * getBestMatch (const Locale & desiredLocale, UErrorCode & errorCode)
Returns the supported locale which best matches the desired locale.
Parameters
Returns
the best-matching supported locale.
ICU 65
const Locale * getBestMatch (Locale::Iterator & desiredLocales, UErrorCode & errorCode)
Returns the supported locale which best matches one of the desired locales.
Parameters
Returns
the best-matching supported locale.
ICU 65
const Locale * getBestMatchForListString (StringPiece desiredLocaleList, UErrorCode & errorCode)
Parses an Accept-Language string
(
RFC 2616 Section 14.4
),
such as "af, en, fr;q=0.9",
and returns the supported locale which best matches one of the desired locales.
Allows whitespace in more places but does not allow "*".
Parameters
Returns
the best-matching supported locale.
ICU 65
Result getBestMatchResult (const Locale & desiredLocale, UErrorCode & errorCode)
Returns the best match between the desired locale and the supported locales.
If the result's desired locale is not nullptr, then it is the address of the input locale.
It has not been cloned.
Parameters
Returns
the best-matching pair of the desired and a supported locale.
ICU 65
Result getBestMatchResult (Locale::Iterator & desiredLocales, UErrorCode & errorCode)
Returns the best match between the desired and supported locales.
If the result's desired locale is not nullptr, then it is a clone of
the best-matching desired locale. The Result object owns the clone.
Parameters
Returns
the best-matching pair of a desired and a supported locale.
ICU 65
UBool isMatch (const Locale & desired, const Locale & supported, UErrorCode & errorCode)
Returns true if the pair of locales matches acceptably.
This is influenced by Builder options such as setDirection(), setFavorSubtag(),
and setMaxDistance().
Parameters
Returns
true if the pair of locales matches acceptably.
ICU 68
double internalMatch (const Locale & desired, const Locale & supported, UErrorCode & errorCode)
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
Returns
value between 0 and 1, inclusive.