Class LocaleUtil

java.lang.Object
ch.tocco.nice2.textresources.api.locale.LocaleUtil

public class LocaleUtil extends Object
Provides utility functions for handling Locales.
  • Method Details

    • getIntersection

      public static Set<Locale> getIntersection(Set<Locale> desired, Set<Locale> available, LocaleUtil.LocaleLevel compareLevel)
      Returns the intersection in the order of the first (desired) param.

      Compares the locales to the level specified.

      Note: For example a comparison to level TO_LANGUAGE will match two locales that have conflicting country information too, not just when one has no country information. This may not be desired.

      Returns:
      Ordered by desired, may be empty. The returned Locales contains all available info, even if it was simplified temporarily for comparison.
    • getIntersection

      public static Set<Locale> getIntersection(Set<Locale> desired, Set<Locale> available)
      Overloaded method where compareLevel = LocaleLevel.TO_VARIANT.
      Returns:
      ordered by desired, may be empty
    • getBest

      public static Locale getBest(Set<Locale> desired, Set<Locale> available, LocaleUtil.LocaleLevel compareLevel, Locale fallback)
      Returns the most desired locale that is available, or fallback.

      Compares the locales to the level specified.

      Note: For example a comparison to level TO_LANGUAGE will match two locales that have conflicting country information too, not just when one has no country information. This may not be desired.

      Returns:
      The returned Locale contains all available info, even if it was simplified temporarily for comparison.
    • getBest

      public static Locale getBest(Set<Locale> desired, Set<Locale> available, Locale fallback)
      Overloaded method where compareLevel = LocaleLevel.TO_VARIANT.
      Parameters:
      fallback - Does not check if fallback is in available. Pass null to get null.
    • simplify

      public static Set<Locale> simplify(Set<Locale> set, LocaleUtil.LocaleLevel level)
      Simplifies a set of locales by stripping off information (if there). Note that obviously the returned set may strink.
      Returns:
      may be empty
    • simplify

      public static Locale simplify(Locale locale, LocaleUtil.LocaleLevel level)
      Simplifies a locale by stripping off information (if there).
    • setCountry

      public static Locale setCountry(Locale locale, String country)
      Returns a new locale with the given country applied.
      Parameters:
      locale - The source
      country - 2 letter iso in upper case, eg "CH", not null, may be empty.
      Returns:
      The new locale with the new country and the existing language and variant.