Package com.helger.commons.locale
Class LocaleHelper
- java.lang.Object
-
- com.helger.commons.locale.LocaleHelper
-
@Immutable public final class LocaleHelper extends Object
Misc locale utility methods.- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static LocaleLOCALE_ALLthe default locale which means "all locales".static LocaleLOCALE_INDEPENDENTthe default locale which means "locale independent".static charLOCALE_SEPARATORSeparates language and country in a locale string representation.static StringSTR_ALLThe language string representing the "all" locale.static StringSTR_INDEPENDENTThe language string representing the "independent" locale.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static EChangeclearCache()Clear all stored locale listsstatic ICommonsMap<Locale,String>getAllLocaleDisplayNames(Locale aContentLocale)Get all possible locale names in the passed localestatic List<Locale>getCalculatedLocaleListForResolving(Locale aLocale)Get a list with all valid locale permutations of the passed locale.static StringgetLocaleDisplayName(Locale aLocale, Locale aContentLocale)Get the display name of the passed language in the currently selected UI language.static LocalegetLocaleFromString(String sLocaleAsString)Convert a String in the form "language-country-variant" to a Locale object.static StringgetLocaleNativeDisplayName(Locale aLocale)Get the display name of the passed locale in the passed locale.static LocalegetLocaleToUseOrFallback(Locale aRequestLocale, Collection<Locale> aAvailableLocales, Locale aFallback)static LocalegetLocaleToUseOrNull(Locale aRequestLocale, Collection<Locale> aAvailableLocales)static StringgetValidCountryCode(String sCode)Ensure a country code is valid by converting it to a 2-character uppercase code.static StringgetValidLanguageCode(String sCode)static booleanisSpecialLocale(Locale aLocale)Check if the passed locale is one of the special locales "all" or "independent"static booleanisSpecialLocaleCode(String sLocale)Check if the passed locale is one of the special locales "all" or "independent"
-
-
-
Field Detail
-
LOCALE_SEPARATOR
public static final char LOCALE_SEPARATOR
Separates language and country in a locale string representation.- See Also:
- Constant Field Values
-
STR_ALL
public static final String STR_ALL
The language string representing the "all" locale. SeeLOCALE_ALL.- See Also:
- Constant Field Values
-
LOCALE_ALL
public static final Locale LOCALE_ALL
the default locale which means "all locales".
-
STR_INDEPENDENT
public static final String STR_INDEPENDENT
The language string representing the "independent" locale. SeeLOCALE_INDEPENDENT.- See Also:
- Constant Field Values
-
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 benull.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 benull.- 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 benull.- Returns:
- A non-
nullunmodifiable 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 isnullor empty,SystemHelper.getSystemLocale()is returned.
-
getLocaleToUseOrNull
@Nullable public static Locale getLocaleToUseOrNull(@Nonnull Locale aRequestLocale, @Nonnull Collection<Locale> aAvailableLocales)
-
getLocaleToUseOrFallback
@Nullable public static Locale getLocaleToUseOrFallback(@Nonnull Locale aRequestLocale, @Nonnull Collection<Locale> aAvailableLocales, @Nullable Locale aFallback)
-
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 benull.- Returns:
- if the passed locale is not
nulland 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 benull.- Returns:
- if the passed locale is not
nulland 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 benull.- Returns:
nullif the source code is not a support country code.
-
-