Top Description Inners Fields Constructors Methods
org.apache.commons.lang3

public Class LocaleUtils

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Arrays, .Collections, .Comparator, .LinkedHashSet, .List, .Locale, .Set, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, java.util.function.Predicate, java.util.stream.Collectors

Operations to assist when working with a Locale.

This class tries to handle null input gracefully. An exception will not be thrown for a null input. Each method documents its behavior in more detail.

Since
2.2
See Also
Locale

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
LocaleUtils.SyncAvoid

Avoids synchronization, initializes on demand.

Field Summary

Modifier and TypeField and Description
private static final ConcurrentMap<String, List<Locale>>
cCountriesByLanguage

Concurrent map of country locales by language.

private static final ConcurrentMap<String, List<Locale>>
cLanguagesByCountry

Concurrent map of language locales by country.

private static final char
DASH

The dash character ''.

private static final char
UNDERSCORE

The underscore character ''.

private static final String
UNDETERMINED

The undetermined language .</am>

Constructor Summary

AccessConstructor and Description
public
LocaleUtils()
Deprecated TODO Make private in 4.0.

LocaleUtils instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
public static List<Locale>

Returns:

the unmodifiable and sorted list of available locales.
availableLocaleList
()

Obtains an unmodifiable and sorted list of installed locales.

private static List<Locale>
public static Set<Locale>

Returns:

the unmodifiable set of available locales.
availableLocaleSet
()

Obtains an unmodifiable set of installed locales.

public static List<Locale>

Returns:

an unmodifiable List of Locale objects, not null.
countriesByLanguage
(final String
the 2 letter language code, null returns empty.
languageCode
)

Obtains the list of countries supported for a given language.

private static boolean

Returns:

whether the given Locale defines a variant.
hasCountry
(final Locale
The Locale to test.
locale
)

Tests whether the given Locale defines a variant.

private static boolean

Returns:

whether the given Locale defines a country.
hasVariant
(final Locale
The Locale to test.
locale
)

Tests whether the given Locale defines a country.

private static boolean

Returns:

whether the given string is the length of an ISO 3166 alpha-2 country code.
isAlpha2Len
(final String
The string to test.
str
)

Tests whether the given string is the length of an ISO 3166 alpha-2 country code.

private static boolean

Returns:

whether the given string is the length of an ISO 3166 alpha-3 country code.
isAlpha3Len
(final String
The string to test.
str
)

Tests whether the given string is the length of an ISO 3166 alpha-3 country code.

public static boolean

Returns:

true if the locale is a known locale.
isAvailableLocale
(final Locale
the Locale object to check if it is available.
locale
)

Checks if the locale specified is in the set of available locales.

private static boolean

Returns:

true, is the given String is a ISO 3166 compliant country code.
isISO3166CountryCode
(final String
the String to check.
str
)

Tests whether the given String is a ISO 3166 alpha-2 country code.

private static boolean

Returns:

true, if the given String is a ISO 639 compliant language code.
isISO639LanguageCode
(final String
the String to check.
str
)

Tests whether the given String is a ISO 639 compliant language code.

public static boolean

Returns:

whether a Locale's language is undetermined.
isLanguageUndetermined
(final Locale
the locale to test.
locale
)

Tests whether a Locale's language is undetermined.

private static boolean

Returns:

true, is the given String is a UN M.49 numeric area code.
isNumericAreaCode
(final String
the String to check.
str
)

TestsNo whether the given String is a UN M.49 numeric area code.

public static List<Locale>

Returns:

an unmodifiable List of Locale objects, not null.
languagesByCountry
(final String
the 2-letter country code, null returns empty.
countryCode
)

Obtains the list of languages supported for a given country.

public static List<Locale>

Returns:

the unmodifiable list of Locale objects, 0 being locale, not null.
localeLookupList
(final Locale
the locale to start from.
locale
)

Obtains the list of locales to search through when performing a locale search.

public static List<Locale>

Returns:

the unmodifiable list of Locale objects, 0 being locale, not null.
localeLookupList
(final Locale
the locale to start from, null returns empty list.
locale
,
final Locale
the default locale to use if no other is found.
defaultLocale
)

Obtains the list of locales to search through when performing a locale search.

pack-priv static Locale

Returns:

a new new Locale for the given country.
ofCountry
(final String
An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code. See the Locale class description about valid country values.
country
)

Creates new Locale for the given country.

private static Locale

Returns:

a Locale parsed from the given String.
parseLocale
(final String
the String to parse as a Locale.
str
)

Tries to parse a Locale from the given String.

public static Locale

Returns:

the given locale if non-null, otherwise Locale#getDefault().
toLocale
(final Locale
a locale or null.
locale
)

Returns the given locale if non-null, otherwise Locale#getDefault().

public static Locale

Returns:

a Locale, null if null input.
toLocale
(final String
the locale String to convert, null returns null.
str
)

Converts a String to a Locale.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cCountriesByLanguageback to summary
private static final ConcurrentMap<String, List<Locale>> cCountriesByLanguage

Concurrent map of country locales by language.

cLanguagesByCountryback to summary
private static final ConcurrentMap<String, List<Locale>> cLanguagesByCountry

Concurrent map of language locales by country.

DASHback to summary
private static final char DASH

The dash character ''.

UNDERSCOREback to summary
private static final char UNDERSCORE

The underscore character ''.

UNDETERMINEDback to summary
private static final String UNDETERMINED

The undetermined language .

If a language is empty, or not well-formed (for example "a" or "e2"), Locale#toLanguageTag() will return "und" (Undetermined).

See Also
Locale#toLanguageTag()

Constructor Detail

LocaleUtilsback to summary
public LocaleUtils()

Deprecated

TODO Make private in 4.0.

LocaleUtils instances should NOT be constructed in standard programming. Instead, the class should be used as LocaleUtils.toLocale("en_GB");.

This constructor is public to permit tools that require a JavaBean instance to operate.

Annotations
@Deprecated

Method Detail

availableLocaleListback to summary
public static List<Locale> availableLocaleList()

Obtains an unmodifiable and sorted list of installed locales.

This method is a wrapper around Locale#getAvailableLocales(). It is more efficient, as the JDK method must create a new array each time it is called.

Returns:List<Locale>

the unmodifiable and sorted list of available locales.

availableLocaleListback to summary
private static List<Locale> availableLocaleList(final Predicate<Locale> predicate)
availableLocaleSetback to summary
public static Set<Locale> availableLocaleSet()

Obtains an unmodifiable set of installed locales.

This method is a wrapper around Locale#getAvailableLocales(). It is more efficient, as the JDK method must create a new array each time it is called.

Returns:Set<Locale>

the unmodifiable set of available locales.

countriesByLanguageback to summary
public static List<Locale> countriesByLanguage(final String languageCode)

Obtains the list of countries supported for a given language.

This method takes a language code and searches to find the countries available for that language. Variant locales are removed.

Parameters
languageCode:String

the 2 letter language code, null returns empty.

Returns:List<Locale>

an unmodifiable List of Locale objects, not null.

hasCountryback to summary
private static boolean hasCountry(final Locale locale)

Tests whether the given Locale defines a variant.

Parameters
locale:Locale

The Locale to test.

Returns:boolean

whether the given Locale defines a variant.

hasVariantback to summary
private static boolean hasVariant(final Locale locale)

Tests whether the given Locale defines a country.

Parameters
locale:Locale

The Locale to test.

Returns:boolean

whether the given Locale defines a country.

isAlpha2Lenback to summary
private static boolean isAlpha2Len(final String str)

Tests whether the given string is the length of an ISO 3166 alpha-2 country code.

Parameters
str:String

The string to test.

Returns:boolean

whether the given string is the length of an ISO 3166 alpha-2 country code.

isAlpha3Lenback to summary
private static boolean isAlpha3Len(final String str)

Tests whether the given string is the length of an ISO 3166 alpha-3 country code.

Parameters
str:String

The string to test.

Returns:boolean

whether the given string is the length of an ISO 3166 alpha-3 country code.

isAvailableLocaleback to summary
public static boolean isAvailableLocale(final Locale locale)

Checks if the locale specified is in the set of available locales.

Parameters
locale:Locale

the Locale object to check if it is available.

Returns:boolean

true if the locale is a known locale.

isISO3166CountryCodeback to summary
private static boolean isISO3166CountryCode(final String str)

Tests whether the given String is a ISO 3166 alpha-2 country code.

Parameters
str:String

the String to check.

Returns:boolean

true, is the given String is a ISO 3166 compliant country code.

isISO639LanguageCodeback to summary
private static boolean isISO639LanguageCode(final String str)

Tests whether the given String is a ISO 639 compliant language code.

Parameters
str:String

the String to check.

Returns:boolean

true, if the given String is a ISO 639 compliant language code.

isLanguageUndeterminedback to summary
public static boolean isLanguageUndetermined(final Locale locale)

Tests whether a Locale's language is undetermined.

A Locale's language tag is undetermined if it's value is "und". If a language is empty, or not well-formed (for example, "a" or "e2"), it will be equal to "und".

Parameters
locale:Locale

the locale to test.

Returns:boolean

whether a Locale's language is undetermined.

Since
3.14.0
See Also
Locale#toLanguageTag()
isNumericAreaCodeback to summary
private static boolean isNumericAreaCode(final String str)

TestsNo whether the given String is a UN M.49 numeric area code.

Parameters
str:String

the String to check.

Returns:boolean

true, is the given String is a UN M.49 numeric area code.

languagesByCountryback to summary
public static List<Locale> languagesByCountry(final String countryCode)

Obtains the list of languages supported for a given country.

This method takes a country code and searches to find the languages available for that country. Variant locales are removed.

Parameters
countryCode:String

the 2-letter country code, null returns empty.

Returns:List<Locale>

an unmodifiable List of Locale objects, not null.

localeLookupListback to summary
public static List<Locale> localeLookupList(final Locale locale)

Obtains the list of locales to search through when performing a locale search.

localeLookupList(Locale("fr", "CA", "xxx"))
  = [Locale("fr", "CA", "xxx"), Locale("fr", "CA"), Locale("fr")]
Parameters
locale:Locale

the locale to start from.

Returns:List<Locale>

the unmodifiable list of Locale objects, 0 being locale, not null.

localeLookupListback to summary
public static List<Locale> localeLookupList(final Locale locale, final Locale defaultLocale)

Obtains the list of locales to search through when performing a locale search.

localeLookupList(Locale("fr", "CA", "xxx"), Locale("en"))
  = [Locale("fr", "CA", "xxx"), Locale("fr", "CA"), Locale("fr"), Locale("en"]

The result list begins with the most specific locale, then the next more general and so on, finishing with the default locale. The list will never contain the same locale twice.

Parameters
locale:Locale

the locale to start from, null returns empty list.

defaultLocale:Locale

the default locale to use if no other is found.

Returns:List<Locale>

the unmodifiable list of Locale objects, 0 being locale, not null.

ofCountryback to summary
pack-priv static Locale ofCountry(final String country)

Creates new Locale for the given country.

Parameters
country:String

An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code. See the Locale class description about valid country values.

Returns:Locale

a new new Locale for the given country.

Exceptions
NullPointerException:
thrown if either argument is null.
See Also
Locale#Locale(String, String)
parseLocaleback to summary
private static Locale parseLocale(final String str)

Tries to parse a Locale from the given String.

See Locale for the format.

Parameters
str:String

the String to parse as a Locale.

Returns:Locale

a Locale parsed from the given String.

Exceptions
IllegalArgumentException:
if the given String cannot be parsed.
See Also
Locale
toLocaleback to summary
public static Locale toLocale(final Locale locale)

Returns the given locale if non-null, otherwise Locale#getDefault().

Parameters
locale:Locale

a locale or null.

Returns:Locale

the given locale if non-null, otherwise Locale#getDefault().

Since
3.12.0
toLocaleback to summary
public static Locale toLocale(final String str)

Converts a String to a Locale.

This method takes the string format of a locale and creates the locale object from it.

  LocaleUtils.toLocale("")           = new Locale("", "")
  LocaleUtils.toLocale("en")         = new Locale("en", "")
  LocaleUtils.toLocale("en_GB")      = new Locale("en", "GB")
  LocaleUtils.toLocale("en-GB")      = new Locale("en", "GB")
  LocaleUtils.toLocale("en_001")     = new Locale("en", "001")
  LocaleUtils.toLocale("en_GB_xxx")  = new Locale("en", "GB", "xxx")   (#)
  LocaleUtils.toLocale("US")         = new Locale("", "US") // Because "US" is Locale.getISOCountries()

(#) The behavior of the JDK variant constructor changed between JDK1.3 and JDK1.4. In JDK1.3, the constructor upper cases the variant, in JDK1.4, it doesn't. Thus, the result from getVariant() may vary depending on your JDK.

This method validates the input strictly. The language code must be lowercase. The country code must be uppercase. The separator must be an underscore or a dash. The length must be correct.

Parameters
str:String

the locale String to convert, null returns null.

Returns:Locale

a Locale, null if null input.

Exceptions
IllegalArgumentException:
if the string is an invalid format.
See Also
Locale#forLanguageTag(String), Locale#getISOCountries()
org.apache.commons.lang3 back to summary

private final Class LocaleUtils.SyncAvoid

extends Object
Class Inheritance

Avoids synchronization, initializes on demand.

Field Summary

Modifier and TypeField and Description
private static final List<Locale>
AVAILABLE_LOCALE_ULIST

Private unmodifiable and sorted list of available locales.

private static final Set<Locale>
AVAILABLE_LOCALE_USET

Private unmodifiable set of available locales.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

AVAILABLE_LOCALE_ULISTback to summary
private static final List<Locale> AVAILABLE_LOCALE_ULIST

Private unmodifiable and sorted list of available locales.

AVAILABLE_LOCALE_USETback to summary
private static final Set<Locale> AVAILABLE_LOCALE_USET

Private unmodifiable set of available locales.

Constructor Detail

SyncAvoidback to summary
private SyncAvoid()