Package com.helger.commons.locale
Class LocaleHelper
java.lang.Object
com.helger.commons.locale.LocaleHelper
Misc locale utility methods.
- Author:
- Philip Helger
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Localethe default locale which means "all locales".static final Localethe default locale which means "locale independent".static final charSeparates language and country in a locale string representation.static final StringThe language string representing the "all" locale.static final StringThe language string representing the "independent" locale. -
Method Summary
Modifier and TypeMethodDescriptionstatic EChangeClear all stored locale listsstatic ICommonsMap<Locale, String> getAllLocaleDisplayNames(Locale aContentLocale) Get all possible locale names in the passed localeGet 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 Details
-
LOCALE_SEPARATOR
public static final char LOCALE_SEPARATORSeparates language and country in a locale string representation.- See Also:
-
STR_ALL
The language string representing the "all" locale. SeeLOCALE_ALL.- See Also:
-
LOCALE_ALL
the default locale which means "all locales". -
STR_INDEPENDENT
The language string representing the "independent" locale. SeeLOCALE_INDEPENDENT.- See Also:
-
LOCALE_INDEPENDENT
the default locale which means "locale independent".
-
-
Method Details
-
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:
-
getLocaleNativeDisplayName
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
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
-
getLocaleToUseOrFallback
-
isSpecialLocale
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:
-
isSpecialLocaleCode
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:
-
getValidLanguageCode
-
getValidCountryCode
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.
-
clearCache
Clear all stored locale lists- Returns:
EChange.
-