Class LocaleUtil


  • public class LocaleUtil
    extends Object
    Class for helping to choose the "best" locale that is available, given a preferred locale (e.g., that is being used by a particular user at a web site) and a default locale (e.g., English). User: Randy Wilson Date: 7/31/2014 Time: 11:44 AM
    • Constructor Detail

      • LocaleUtil

        public LocaleUtil()
    • Method Detail

      • findClosestLocale

        public static TextValue findClosestLocale​(Collection<TextValue> textValues,
                                                  Locale localeToMatch)
        Return the textValue whose language is closest the localeToMatch.
        Parameters:
        textValues - - Collection of TextValues, each with a value and a language.
        localeToMatch - - Preferred locale to match against.
        Returns:
        TextValue in the given collection that is closest to the given locale; or null if there are no values.
      • findClosestLocale

        public static TextValue findClosestLocale​(Collection<TextValue> textValues,
                                                  Locale localeToMatch,
                                                  Locale defaultLocale)
        Return the textValue whose language is closest the localeToMatch.
        Parameters:
        textValues - - Collection of TextValues, each with a value and a language.
        localeToMatch - - Preferred locale to match against.
        defaultLocale - - Default locale to match against, if none are close to the preferred locale.
        Returns:
        TextValue in the given collection that is closest to the given locale; or null if there are no values.
      • findClosestLocale

        public static Locale findClosestLocale​(Set<Locale> locales,
                                               Locale localeToMatch)
      • getSimpleLocale

        public static Locale getSimpleLocale​(String languageString)
        Parse the given languageString (e.g., "en-us", "en-US", "en_us", "en_US") and create a Locale from it. Parse but then ignore script, variants, extensions and private use. Keep only the language and region. This should not be needed in Java 1.7, which has Locale.forLangaugeTag(languageString), which does the same thing only better.
        Parameters:
        languageString - - BCP47 or Java language string ("en", "en-us", "en_US", etc.)
        Returns:
        Locale for that language.