Class LocaleHelper


  • @Immutable
    public final class LocaleHelper
    extends Object
    Misc locale utility methods.
    Author:
    Philip Helger
    • Field Detail

      • LOCALE_SEPARATOR

        public static final char LOCALE_SEPARATOR
        Separates language and country in a locale string representation.
        See Also:
        Constant Field Values
      • LOCALE_ALL

        public static final Locale LOCALE_ALL
        the default locale which means "all locales".
      • LOCALE_INDEPENDENT

        public static final Locale LOCALE_INDEPENDENT
        the default locale which means "locale independent".
    • Method Detail

      • getLocaleDisplayName

        @Nonnull
        public static String getLocaleDisplayName​(@Nullable
                                                  Locale aLocale,
                                                  @Nonnull
                                                  Locale aContentLocale)
        Get the display name of the passed language in the currently selected UI language.
        Parameters:
        aLocale - The locale from which the display name is required. May be null.
        aContentLocale - The locale in which the name of the locale is required. If aLocale is "de" and display locale is "de" the result would be "Deutsch" whereas if display locale is "en" the result would be "German".
        Returns:
        the display name of the language or a fixed text if the passed Locale is null, "all" or "independent"
        See Also:
        LOCALE_ALL, LOCALE_INDEPENDENT
      • getLocaleNativeDisplayName

        @Nonnull
        public static String getLocaleNativeDisplayName​(@Nonnull
                                                        Locale aLocale)
        Get the display name of the passed locale in the passed locale.
        Parameters:
        aLocale - The locale to use. May not be null.
        Returns:
        The native display name of the passed locale.
      • getAllLocaleDisplayNames

        @Nonnull
        @ReturnsMutableCopy
        public static ICommonsMap<Locale,​String> getAllLocaleDisplayNames​(@Nonnull
                                                                                Locale aContentLocale)
        Get all possible locale names in the passed locale
        Parameters:
        aContentLocale - the locale ID in which the language list is required
        Returns:
        The mapping from the input locale to the display text. The result map is not ordered.
      • getCalculatedLocaleListForResolving

        @Nonnull
        @ReturnsImmutableObject
        @CodingStyleguideUnaware
        public static List<Locale> getCalculatedLocaleListForResolving​(@Nonnull
                                                                       Locale aLocale)
        Get a list with all valid locale permutations of the passed locale. If the passed locale has no language, always an empty list is returned.
        Examples:
        • "de_AT" => ["de_AT", "de"]
        • "en_US" => ["en_US", "en"]
        • "de" => ["de"]
        • "de_DE_Variant" => ["de_DE_Variant", "de_DE", "de"]
        • "" => []
        • "_AT" => []
        • "_AT_Variant" => []
        Parameters:
        aLocale - The locale to get the permutation from. May not be null .
        Returns:
        A non-null unmodifiable list of all permutations of the locale, with the most specific locale being first. The returned list has never more than three entries. The returned list may have no entries, if the passed locale has no language.
      • getLocaleFromString

        @Nonnull
        public static Locale getLocaleFromString​(@Nullable
                                                 String sLocaleAsString)
        Convert a String in the form "language-country-variant" to a Locale object. Language needs to have exactly 2 characters. Country is optional but if present needs to have exactly 2 characters. Variant is optional.
        Parameters:
        sLocaleAsString - The string representation to be converted to a Locale.
        Returns:
        Never null. If the passed parameter is null or empty, SystemHelper.getSystemLocale() is returned.
      • isSpecialLocale

        public static boolean isSpecialLocale​(@Nullable
                                              Locale aLocale)
        Check if the passed locale is one of the special locales "all" or "independent"
        Parameters:
        aLocale - The locale to check. May be null.
        Returns:
        if the passed locale is not null and a special locale.
        See Also:
        LOCALE_ALL, LOCALE_INDEPENDENT
      • isSpecialLocaleCode

        public static boolean isSpecialLocaleCode​(@Nullable
                                                  String sLocale)
        Check if the passed locale is one of the special locales "all" or "independent"
        Parameters:
        sLocale - The locale to check. May be null.
        Returns:
        if the passed locale is not null and a special locale.
        See Also:
        LOCALE_ALL, LOCALE_INDEPENDENT
      • getValidCountryCode

        @Nullable
        public static String getValidCountryCode​(@Nullable
                                                 String sCode)
        Ensure a country code is valid by converting it to a 2-character uppercase code. This method also maps 3 letter codes to 2 letter codes.
        Parameters:
        sCode - The country code to check. May be null.
        Returns:
        null if the source code is not a support country code.