Package de.aristaflow.adept2.util
Class LocaleTools
- java.lang.Object
-
- de.aristaflow.adept2.util.LocaleTools
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description 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.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.static Locale[]
getCommonLocales()
Returns a list of commonly useful locales, e.g. to offer a set of default locales in a combo box.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.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.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.static String
getLocaleLabel(Locale locale)
Returns a string representation of the locale, which is presentable to the user.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.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.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.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.static Locale
parseLocaleString(String localeString)
Deprecated.UseLocale.forLanguageTag(String)
instead.
-
-
-
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.UseLocale.forLanguageTag(String)
instead. Make sure the locale has the right format (IETF BCP 47), which is different from the usualLocale
format. For instance, the localede_DE
has the language tagde-DE
!Parse a locale from the specified locale string as returned byLocale.toString()
.- Parameters:
localeString
- the locale string to be parsed- Returns:
- The locale,
null
if the string wasnull
. An empty or blank string will be parsed toLocale.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 isnull
or an empty array or an array containing only onenull
element, an empty list will be returned. This must not contain severalnull
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 arenull
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 isnull
or an empty array or an array containing only onenull
element, an empty list will be returned. This must not contain severalnull
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 arenull
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 isnull
, an empty list will be returned. This must not containnull
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 arenull
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 benull
.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 benull
.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 benull
.localisations
- A map containing localised strings for various locales. This may benull
.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 isnull
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 usingString.format(Locale, String, Object...)
.- Returns:
- The designated map containing the requested localisations. This is the designated map
unless
null
is provided. Ifnull
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 localeavailableLocales
- the available locales- Returns:
- The best matching locale or
null
if not even a language matches.
-
-