ICU 76.1 76.1
|
Immutable class that picks the best match between a user's desired locales and an application's supported locales. More...
#include <localematcher.h>
Data Structures | |
class | Builder |
LocaleMatcher builder. More... | |
class | Result |
Data for the best-matching pair of a desired and a supported locale. More... | |
Public Member Functions | |
LocaleMatcher (LocaleMatcher &&src) noexcept | |
Move copy constructor; might modify the source. | |
~LocaleMatcher () | |
Destructor. | |
LocaleMatcher & | operator= (LocaleMatcher &&src) noexcept |
Move assignment operator; might modify the source. | |
const Locale * | getBestMatch (const Locale &desiredLocale, UErrorCode &errorCode) const |
Returns the supported locale which best matches the desired locale. | |
const Locale * | getBestMatch (Locale::Iterator &desiredLocales, UErrorCode &errorCode) const |
Returns the supported locale which best matches one of the desired locales. | |
const Locale * | getBestMatchForListString (StringPiece desiredLocaleList, UErrorCode &errorCode) const |
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. | |
Result | getBestMatchResult (const Locale &desiredLocale, UErrorCode &errorCode) const |
Returns the best match between the desired locale and the supported locales. | |
Result | getBestMatchResult (Locale::Iterator &desiredLocales, UErrorCode &errorCode) const |
Returns the best match between the desired and supported locales. | |
UBool | isMatch (const Locale &desired, const Locale &supported, UErrorCode &errorCode) const |
Returns true if the pair of locales matches acceptably. | |
double | internalMatch (const Locale &desired, const Locale &supported, UErrorCode &errorCode) const |
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. | |
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:
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.
Definition at line 186 of file localematcher.h.
|
noexcept |
Move copy constructor; might modify the source.
This matcher will have the same settings that the source matcher had.
src | source matcher |
icu::LocaleMatcher::~LocaleMatcher | ( | ) |
Destructor.
const Locale * icu::LocaleMatcher::getBestMatch | ( | const Locale & | desiredLocale, |
UErrorCode & | errorCode | ||
) | const |
Returns the supported locale which best matches the desired locale.
desiredLocale | Typically a user's language. |
errorCode | ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
const Locale * icu::LocaleMatcher::getBestMatch | ( | Locale::Iterator & | desiredLocales, |
UErrorCode & | errorCode | ||
) | const |
Returns the supported locale which best matches one of the desired locales.
desiredLocales | Typically a user's languages, in order of preference (descending). |
errorCode | ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
const Locale * icu::LocaleMatcher::getBestMatchForListString | ( | StringPiece | desiredLocaleList, |
UErrorCode & | errorCode | ||
) | const |
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 "*".
desiredLocaleList | Typically a user's languages, as an Accept-Language string. |
errorCode | ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
Result icu::LocaleMatcher::getBestMatchResult | ( | const Locale & | desiredLocale, |
UErrorCode & | errorCode | ||
) | const |
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.
desiredLocale | Typically a user's language. |
errorCode | ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
Result icu::LocaleMatcher::getBestMatchResult | ( | Locale::Iterator & | desiredLocales, |
UErrorCode & | errorCode | ||
) | const |
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.
desiredLocales | Typically a user's languages, in order of preference (descending). |
errorCode | ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
double icu::LocaleMatcher::internalMatch | ( | const Locale & | desired, |
const Locale & | supported, | ||
UErrorCode & | errorCode | ||
) | const |
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.
desired | Desired locale. |
supported | Supported locale. |
errorCode | ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
UBool icu::LocaleMatcher::isMatch | ( | const Locale & | desired, |
const Locale & | supported, | ||
UErrorCode & | errorCode | ||
) | const |
Returns true if the pair of locales matches acceptably.
This is influenced by Builder options such as setDirection(), setFavorSubtag(), and setMaxDistance().
desired | The desired locale. |
supported | The supported locale. |
errorCode | ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
|
noexcept |
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.
src | source matcher |