Class LocaleTools


  • public final class LocaleTools
    extends Object
    Provides Locale-specific utility methods.
    • Method Detail

      • getCommonLocales

        public static Locale[] getCommonLocales()
        Returns a list of commonly useful locales, e.g. to offer a set of default locales in a combo box.
        Returns:
        a list of commonly useful locales
      • parseLocaleString

        @Deprecated
        public static Locale parseLocaleString​(String localeString)
        Deprecated.
        Use Locale.forLanguageTag(String) instead. Make sure the locale has the right format (IETF BCP 47), which is different from the usual Locale format. For instance, the locale de_DE has the language tag de-DE!
        Parse a locale from the specified locale string as returned by Locale.toString().
        Parameters:
        localeString - the locale string to be parsed
        Returns:
        The locale, null if the string was null. An empty or blank string will be parsed to Locale.ROOT.
        See Also:
        Locale.toString()
      • getFallbackSequenceStrings

        public static List<String> getFallbackSequenceStrings​(Locale... preferredLocales)
        Gets a sequence of locales (as strings) in the order they should be used until a localisation for a specific resource could be found for the designated preferred locales. The first of these locales should be the most preferred and the last typically being a default locale. The result will not contain duplicates, that is, a more generic locale will not be in the list again. For instance, [en_US, en_GB] will lead to [en_US, en, "quot;, en_GB].
        Parameters:
        preferredLocales - The desired locales in order of their preference. If this is null or an empty array or an array containing only one null element, an empty list will be returned. This must not contain several null elements.
        Returns:
        A sequence of locale fallbacks as strings starting from and including the designated locale. The last locale will be Locale.ROOT. This will be the empty list if the designated locales are null or an empty array.
        See Also:
        getFallbackSequence(Locale...)
      • getFallbackSequence

        public static List<Locale> getFallbackSequence​(Locale... preferredLocales)
        Gets a sequence of locales in the order they should be used until a localisation for a specific resource could be found for the designated preferred locales. The first of these locales should be the most preferred and the last typically being a default locale. The result will not contain duplicates, that is, a more generic locale will not be in the list again. For instance, [en_US, en_GB] will lead to [en_US, en, "quot;, en_GB].
        Parameters:
        preferredLocales - The desired locales in order of their preference. If this is null or an empty array or an array containing only one null element, an empty list will be returned. This must not contain several null elements.
        Returns:
        A sequence of locale fallbacks starting from and including the designated locale. The last locale will be Locale.ROOT. This will be the empty list if the designated locales are null or an empty array.
      • getFallbackSequence

        public static List<Locale> getFallbackSequence​(List<Locale> preferredLocales)
        Gets a sequence of locales in the order they should be used until a localisation for a specific resource could be found for the designated preferred locales. The first of these locales should be the most preferred and the last typically being a default locale. The result will not contain duplicates, that is, a more generic locale will not be in the list again. For instance, [en_US, en_GB] will lead to [en_US, en, "", en_GB].
        Parameters:
        preferredLocales - The desired locales in order of their preference. If this is null, an empty list will be returned. This must not contain null elements.
        Returns:
        A sequence of locale fallbacks starting from and including the designated locale. The last locale will be Locale.ROOT. This will be the empty list if the designated locales are null or the empty list.
      • getLocalisedString

        public static String getLocalisedString​(Locale loc,
                                                Map<Locale,​String> localisations)
        Gets the localised string from the designated map of localisations matching the designated locale or one of its fallbacks. The map should contain the root locale.
        Parameters:
        loc - The locale to retrieve the string for, also a fallback locale may be used.
        localisations - A map containing localised strings for various locales. This map should contain the root locale.
        Returns:
        A localised string from the designated map corresponding to the designated locale or null if there is no appropriate localisation in the map.
      • getLocalisedString

        public static String getLocalisedString​(Collection<Locale> locs,
                                                Map<Locale,​String> localisations)
        Gets the localised string from the designated map of localisations matching one of the designated locales. The map should contain the root locale.
        Parameters:
        locs - The locales to retrieve the string for, a preceding locale has preference over the following locales. This may be null.
        localisations - A map containing localised strings for various locales. This map should contain the root locale.
        Returns:
        A localised string from the designated map corresponding to the designated locale or null if there is no appropriate localisation in the map.
      • getLocalisedString

        public static String getLocalisedString​(Locale loc,
                                                Map<Locale,​String> localisations,
                                                String defString)
        Gets the localised string from the designated map of localisations matching the designated locale or one of its fallbacks. If there is no appropriate localisation in the map, the default string will be used.
        Parameters:
        loc - The locale to retrieve the string for, also a fallback locale may be used.
        localisations - A map containing localised strings for various locales. This may be null.
        defString - The default string to be used if no appropriate localisation is found.
        Returns:
        A localised string from the designated map corresponding to the designated locale or the designated default string if there is no appropriate localisation in the map.
      • getLocalisedString

        public static String getLocalisedString​(Collection<Locale> locs,
                                                Map<Locale,​String> localisations,
                                                String defString)
        Gets the localised string from the designated map of localisations matching one of the designated locales. If there is no appropriate localisation in the map, the default string will be used.
        Parameters:
        locs - The locales to retrieve the string for, a preceding locale has preference over the following locales. This may be null.
        localisations - A map containing localised strings for various locales. This may be null.
        defString - The default string to be used if no appropriate localisation is found.
        Returns:
        A localised string from the designated map corresponding to the designated locale or the designated default string if there is no appropriate localisation in the map.
      • fillLocalisedMessage

        public static Map<Locale,​String> fillLocalisedMessage​(Map<Locale,​String> localisedMessage,
                                                                    String msgKey,
                                                                    List<Locale> locales,
                                                                    Function<Locale,​Messages> provider,
                                                                    Object... args)
        Loads the designated message key from the corresponding messages for the designated locales and stores them in the designated map. That is, the appropriate messages will be retrieved from the designated provider for each of the designated locales. The localisation for the designated message key is then stored in the designated map using the actual locale of the messages. This locale may be a fallback for the requested locale. The root locale will always be included.
        Parameters:
        localisedMessage - The map to store the localised message with the actual locale of the corresponding messages as key. If this is null a new map will be created and returned.
        msgKey - The message key with which to retrieve the localisation from the messages.
        locales - The locales for which to retrieve localisations for the designated message key.
        provider - The provider for the localised messages.
        args - Optional arguments passed to the corresponding localised message using String.format(Locale, String, Object...).
        Returns:
        The designated map containing the requested localisations. This is the designated map unless null is provided. If null is provided a new map will be created.
      • findBestMatch

        public static Locale findBestMatch​(Locale targetLocale,
                                           Collection<Locale> availableLocales)
        Returns a locale from the given collection of available locales that best matches the given target locale or null if either the target locale is null or no locale matches the target locale. The best locale need not be the right one. That is, if a matching language is found, this locale may be the best, ignoring country and variant.
        Parameters:
        targetLocale - the desired locale
        availableLocales - the available locales
        Returns:
        The best matching locale or null if not even a language matches.
      • getLocaleLabel

        public static String getLocaleLabel​(Locale locale)
        Returns a string representation of the locale, which is presentable to the user.
        Parameters:
        locale - The locale to be transformed to a string.
        Returns:
        The label string for the given locale.