Class Locales
java.lang.Object
ch.tocco.nice2.textresources.api.i18n.Locales
Utilities for dealing with locales.
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Locale.Builder
static boolean
isSimplifiable
(Locale locale) static @Nullable Locale
lookup
(List<Locale> preferred, Collection<Locale> available) Lookup a matching available locale from a list of preferred locales.static <T> T
Lookup the best matching value from a map mapping locales to values.static @Nullable Locale
lookup
(Locale preferred, Collection<Locale> available) Lookup a matching locale for a single locale.static <T> T
Lookup the best matching value from a map mapping locales to values.static int
Matches a candidate locale against another locale.static Locale.Builder
simplifiedBuilder
(Locale template) static Locale
Returns a locale without any extensions, if present.static int
Returns the "weight" of a locale.
-
Method Details
-
lookup
@Nullable public static @Nullable Locale lookup(List<Locale> preferred, Collection<Locale> available) Lookup a matching available locale from a list of preferred locales. This uses a simple scoring mechanism to determine the best match. First, the available locales are ordered by their weight, the least specific locale first (e.g., de-CH is more specific than just de, has therefore more weight). Then, it applies the following algorithm for each entry in the list of preferred locales: 1. Set the score to 0 2. Determine a match score for an available locale usingmatch()
. 3. If the language didn't match, don't consider the locale a viable locale. 4. If the score is higher than the previous score, remember this match as the current viable candidate. 5. Continue with 2 until there are no more available locales. 6. Return the current viable locale if any, or consider the next preferred locale.- Parameters:
preferred
- A list of preferred locales.available
- A collection of available locales.- Returns:
- The best matching available locale or `null`, if none matches.
-
lookup
Lookup a matching locale for a single locale.- Parameters:
preferred
- The preferred locale.available
- A collection of available locales.- Returns:
- The best matching locale.
- See Also:
-
lookup
Lookup the best matching value from a map mapping locales to values.- Parameters:
preferred
- A list of preferred locales.map
- A map mapping locales to values.- Returns:
- The best matching value or `null`.
- See Also:
-
lookup
Lookup the best matching value from a map mapping locales to values.- Parameters:
preferred
- The preferred locale.map
- A map mapping locales to values.- Returns:
- The best matching value or `null`.
- See Also:
-
match
Matches a candidate locale against another locale. The match is scored using the scores defined inLocales.Score
.- Parameters:
candidate
- A candidate localematch
- A locale to match the candidate against.- Returns:
- A score for the match, 0 the two locales are completely distinct.
-
weight
Returns the "weight" of a locale. A locale is considered the "heavier", the more specific it is. E.g., "de-CH" is more specific than "de", thus, "de-CH" will result in a higher return value.- Parameters:
locale
- The locale to determine the weight of.- Returns:
- A weight of the locale.
-
isSimplifiable
-
simplify
Returns a locale without any extensions, if present. If the locale doesn't contain any extensions, the locale is returned unchanged. Unicode types will be copied if present.- Parameters:
locale
- The source locale- Returns:
- The simplified locale.
-
simplifiedBuilder
-
builder
-
stringToLocaleFunction
-
stringToSimplifiedLocaleFunction
-
localeToStringFunction
-
simplifiedLocaleToStringFunction
-
simplifyFunction
-