Class Locales

java.lang.Object
ch.tocco.nice2.textresources.api.i18n.Locales

public final class Locales extends Object
Utilities for dealing with locales.
  • 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 using match(). 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

      @Nullable public static @Nullable Locale lookup(Locale preferred, Collection<Locale> available)
      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

      @Nullable public static <T> T lookup(List<Locale> preferred, Map<Locale,? extends T> map)
      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

      @Nullable public static <T> T lookup(Locale preferred, Map<Locale,? extends T> map)
      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

      public static int match(Locale candidate, Locale match)
      Matches a candidate locale against another locale. The match is scored using the scores defined in Locales.Score.
      Parameters:
      candidate - A candidate locale
      match - A locale to match the candidate against.
      Returns:
      A score for the match, 0 the two locales are completely distinct.
    • weight

      public static int weight(Locale locale)
      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

      public static boolean isSimplifiable(Locale locale)
    • simplify

      public static Locale simplify(Locale locale)
      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

      public static Locale.Builder simplifiedBuilder(Locale template)
    • builder

      public static Locale.Builder builder(Locale template)
    • stringToLocaleFunction

      public static Function<String,Locale> stringToLocaleFunction()
    • stringToSimplifiedLocaleFunction

      public static Function<String,Locale> stringToSimplifiedLocaleFunction()
    • localeToStringFunction

      public static Function<Locale,String> localeToStringFunction()
    • simplifiedLocaleToStringFunction

      public static Function<Locale,String> simplifiedLocaleToStringFunction()
    • simplifyFunction

      public static Function<Locale,Locale> simplifyFunction()