isAvailableLocale
is a LocaleUtils
class that is used to check if the specified locale is in the list of available locales. The method returns true
if the specified locale is in the list of available locales; Otherwise, it returns false
.
LocaleUtils
The definition of LocaleUtils
can be found in the Apache Commons Lang
package, which we can add to the Maven project by adding the following dependency to the pom.xml
file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the
commons-lang
package, refer to the Maven Repository.
You can import the LocaleUtils
class as follows:
import org.apache.commons.lang3.LocaleUtils;
public static boolean isAvailableLocale(final Locale locale)
final Locale locale
: The locale object to check.
The method returns true
if the specified locale is in the list of available locales. Otherwise, it returns false
.
import org.apache.commons.lang3.LocaleUtils;import java.util.Locale;public class Main{public static void main(String[] args){// Example 1Locale localeToTest = Locale.US;System.out.println("The locale 'en_US' is " + (LocaleUtils.isAvailableLocale(localeToTest)?"available":"not available "));// Example 2localeToTest = Locale.forLanguageTag("ol");System.out.println("The locale with 'ol' as language tag is " + (LocaleUtils.isAvailableLocale(localeToTest)?"available":"not available "));// Example 3localeToTest = null;System.out.println("The null locale is " + (LocaleUtils.isAvailableLocale(localeToTest)?"available":"not available "));}}
locale = en_US
The method returns true
when passed the locale above, as it is available in the list of locales.
In the second example, we try to get the locale that has the language tag as ol
with the help of forLanguageTag()
method of the Locale
class. The method returns false
when passed the locale as it is not available in the list of locales.
In the third example, we pass a null locale to the method. The method returns false
.
The locale 'en_US' is available
The locale with 'ol' as language tag is not available
The null locale is not available
Free Resources