Class LocaleUtility


  • public class LocaleUtility
    extends Object
    • Constructor Detail

      • LocaleUtility

        public LocaleUtility()
    • Method Detail

      • setLocale

        public static void setLocale​(Locale locale)
        Set the locale to ThreadLocal
        Parameters:
        locale -
      • getLocale

        public static Locale getLocale()
        get the locale from ThreadLocal
      • setL2Locale

        public static void setL2Locale​(Locale locale)
        Set the locale to ThreadLocal
        Parameters:
        locale -
      • getL2Locale

        public static Locale getL2Locale()
        get the locale from ThreadLocal
      • isDefaultLocale

        public static boolean isDefaultLocale​(Locale locale)
      • isDefaultLocale

        public static boolean isDefaultLocale​(String languageTag)
      • fmtToMappedLocale

        public static Locale fmtToMappedLocale​(String zhLocale)
      • fmtToMappedLocale

        public static Locale fmtToMappedLocale​(Locale zhLocale)
      • pickupLocaleFromList

        public static Locale pickupLocaleFromList​(Set<Locale> locales,
                                                  Locale preferredLocale)
        Iterates over the set of locales to find a locale that best matches the preferredLocale.

        A "best match" is defined to be the locale that has the longest common language tag with the preferredLocale. For example, the supported locale 'de' will be returned for a non-supported preferredLocale 'de-DE'.

        To meet the custom usage of Chinese language, locale "zh" is not considered as a "match" for any non-supported Chinese locale (zh-*). That is, even if "zh" locale is supported, null will be returned for Chinese locale 'zh-HK' that is not supported.

        Parameters:
        locales - the set of locales to find the best match from.
        preferredLocale - the locale being matched.
        Returns:
        the best match, if any; null otherwise.
      • normalizeToLanguageTag

        public static String normalizeToLanguageTag​(String localeStr)
        normalize a locale string(e.g. 'zh__#Hans', 'zh_CN_#Hans') to language tag(e.g. 'zh-Hans', 'zh-Hans-CN').
      • isLanguageTag

        public static boolean isLanguageTag​(String languageTag)
        validate that an argument is a well-formed BCP 47 tag
        Parameters:
        languageTag -
        Returns:
        true if the format is fine
      • getDefaultLocale

        public static Locale getDefaultLocale()
      • setDefaultLocale

        public static void setDefaultLocale​(Locale defaultLocale)
      • getSourceLocale

        public static Locale getSourceLocale()
      • setSourceLocale

        public static void setSourceLocale​(Locale sourceLocale)
      • getFallbackLocales

        public static List<Locale> getFallbackLocales()
      • setFallbackLocales

        public static void setFallbackLocales​(List<Locale> fallbackLocales)
      • langTagtoLocaleSet

        public static Set<Locale> langTagtoLocaleSet​(Set<String> languageTags)