What is LocaleUtils.isAvailableLocale() in Java?

Overview

isAvailableLocale is a staticthe methods in Java that can be called without creating an object of the class. method of the 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.

How to import 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;

Syntax


public static boolean isAvailableLocale(final Locale locale)

Parameters

final Locale locale: The locale object to check.

Return value

The method returns true if the specified locale is in the list of available locales. Otherwise, it returns false.

Code

import org.apache.commons.lang3.LocaleUtils;
import java.util.Locale;
public class Main{
public static void main(String[] args){
// Example 1
Locale localeToTest = Locale.US;
System.out.println("The locale 'en_US' is " + (LocaleUtils.isAvailableLocale(localeToTest)?"available":"not available "));
// Example 2
localeToTest = Locale.forLanguageTag("ol");
System.out.println("The locale with 'ol' as language tag is " + (LocaleUtils.isAvailableLocale(localeToTest)?"available":"not available "));
// Example 3
localeToTest = null;
System.out.println("The null locale is " + (LocaleUtils.isAvailableLocale(localeToTest)?"available":"not available "));
}
}

Example 1

  • locale = en_US

The method returns true when passed the locale above, as it is available in the list of locales.

Example 2

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.

Example 3

In the third example, we pass a null locale to the method. The method returns false.

Output

The locale 'en_US' is available
The locale with 'ol' as language tag is not available 
The null locale is not available 

Free Resources

Attributions:
  1. undefined by undefined