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.
| Modifier and Type | Class and Description |
|---|---|
| public static class | LocaleMatcher.Builder
LocaleMatcher Builder. |
| public static enum | LocaleMatcher.Demotion
Builder option for whether all desired locales are treated equally or earlier ones are preferred. |
| public static enum | LocaleMatcher.Direction
Builder option for whether to include or ignore one-way (fallback) match data. |
| public static enum | LocaleMatcher.FavorSubtag
Builder option for whether the language subtag or the script subtag is most important. |
| private static class | |
| private abstract static class | |
| public static class | LocaleMatcher.Result
Data for the best-matching pair of a desired and a supported locale. |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private final Locale | |
| private final ULocale | |
| private final int | |
| private final LocaleMatcher. | |
| private static final Locale | |
| private final LocaleMatcher. | |
| private final int[] | |
| private final Locale[] | |
| private final LSR[] | |
| private final int | |
| private final Map | |
| private final ULocale[] | |
| private final int | |
| private static final boolean | |
| private static final Locale | |
| private static final LSR | |
| private static final ULocale |
| Access | Constructor and Description |
|---|---|
| public | LocaleMatcher(LocalePriorityList
list of locales supportedLocales)Copies the supported locales, preserving iteration order, and constructs a LocaleMatcher. |
| public | LocaleMatcher(String
the string of locales to set,
to be parsed like LocalePriorityList does supportedLocales)Parses the string like |
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static LocaleMatcher. | Returns: a new Builder objectReturns a builder used in chaining parameters for building a LocaleMatcher. |
| public ULocale | Returns: ULocale with remapped subtags.language/locale code locale)Partially canonicalizes a locale (language). |
| private LocaleMatcher. | |
| public Locale | Returns: the best-matching supported locale.Typically a user's language. desiredLocale)Returns the supported locale which best matches the desired locale. |
| public Locale | Returns: the best-matching supported locale.Typically a user's languages, in order of preference (descending). desiredLocales)Returns the supported locale which best matches one of the desired locales. |
| public LocaleMatcher. | Returns: the best-matching pair of the desired and a supported locale.Typically a user's language. desiredLocale)Returns the best match between the desired locale and the supported locales. |
| public LocaleMatcher. | Returns: the best-matching pair of a desired and a supported locale.Typically a user's languages, in order of preference (descending). desiredLocales)Returns the best match between the desired and supported locales. |
| public ULocale | Returns: the best-matching supported locale.Typically a user's language. desiredLocale)Returns the supported locale which best matches the desired locale. |
| public ULocale | Returns: the best-matching supported locale.Typically a user's languages, in order of preference (descending).
(In ICU 4.4..63 this parameter had type LocalePriorityList.) desiredLocales)Returns the supported locale which best matches one of the desired locales. |
| public ULocale | Returns: the best-matching supported locale.Typically a user's languages,
as a string which is to be parsed like LocalePriorityList does. desiredLocaleList)Parses the string like |
| public LocaleMatcher. | Returns: the best-matching pair of the desired and a supported locale.Typically a user's language. desiredLocale)Returns the best match between the desired locale and the supported locales. |
| public LocaleMatcher. | Returns: the best-matching pair of a desired and a supported locale.Typically a user's languages, in order of preference (descending). desiredLocales)Returns the best match between the desired and supported locales. |
| private int | Returns: the index of the best-matching supported locale, or -1 if there is no good match.The first desired locale's LSR. desiredLSR, LocaleMatcher.Remaining desired LSRs, null or empty if none. remainingIter
|
| private static final LSR | |
| private static final LSR | |
| public boolean | |
| public boolean | |
| private LocaleMatcher. | |
| private LocaleMatcher. | |
| public double | Returns: value between 0 and 1, inclusive.Desired locale. desired, ULocale Maximized locale (using likely subtags). desiredMax, ULocale Supported locale. supported, ULocale Maximized locale (using likely subtags). 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.
|
| private final int | |
| public String |