Class LocaleUtility


  • public class LocaleUtility
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      LocaleUtility()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Locale fmtToMappedLocale​(java.lang.String zhLocale)  
      static java.util.Locale fmtToMappedLocale​(java.util.Locale zhLocale)  
      static java.util.Locale getDefaultLocale()  
      static java.util.List<java.util.Locale> getFallbackLocales()  
      static java.util.Locale getL2Locale()
      get the locale from ThreadLocal
      static java.util.Locale getLocale()
      get the locale from ThreadLocal
      static java.util.Locale getSourceLocale()  
      static boolean isDefaultLocale​(java.lang.String languageTag)  
      static boolean isDefaultLocale​(java.util.Locale locale)  
      static boolean isLanguageTag​(java.lang.String languageTag)
      validate that an argument is a well-formed BCP 47 tag
      static java.util.Set<java.util.Locale> langTagtoLocaleSet​(java.util.Set<java.lang.String> languageTags)  
      static java.lang.String normalizeToLanguageTag​(java.lang.String localeStr)
      normalize a locale string(e.g.
      static java.util.Locale pickupLocaleFromList​(java.util.Set<java.util.Locale> locales, java.util.Locale preferredLocale)
      Iterates over the set of locales to find a locale that best matches the preferredLocale.
      static void setDefaultLocale​(java.util.Locale defaultLocale)  
      static void setFallbackLocales​(java.util.List<java.util.Locale> fallbackLocales)  
      static void setL2Locale​(java.util.Locale locale)
      Set the locale to ThreadLocal
      static void setLocale​(java.util.Locale locale)
      Set the locale to ThreadLocal
      static void setSourceLocale​(java.util.Locale sourceLocale)  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LocaleUtility

        public LocaleUtility()
    • Method Detail

      • setLocale

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

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

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

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

        public static boolean isDefaultLocale​(java.util.Locale locale)
      • isDefaultLocale

        public static boolean isDefaultLocale​(java.lang.String languageTag)
      • fmtToMappedLocale

        public static java.util.Locale fmtToMappedLocale​(java.lang.String zhLocale)
      • fmtToMappedLocale

        public static java.util.Locale fmtToMappedLocale​(java.util.Locale zhLocale)
      • pickupLocaleFromList

        public static java.util.Locale pickupLocaleFromList​(java.util.Set<java.util.Locale> locales,
                                                            java.util.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 java.lang.String normalizeToLanguageTag​(java.lang.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​(java.lang.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 java.util.Locale getDefaultLocale()
      • setDefaultLocale

        public static void setDefaultLocale​(java.util.Locale defaultLocale)
      • getSourceLocale

        public static java.util.Locale getSourceLocale()
      • setSourceLocale

        public static void setSourceLocale​(java.util.Locale sourceLocale)
      • getFallbackLocales

        public static java.util.List<java.util.Locale> getFallbackLocales()
      • setFallbackLocales

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

        public static java.util.Set<java.util.Locale> langTagtoLocaleSet​(java.util.Set<java.lang.String> languageTags)