countriesByLanguage
is a LocaleUtils
class. Locale
objects provide information based on geographic locations.
The countriesByLanguage
method retrieves the list of countries that support a particular language.
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 List<Locale> countriesByLanguage(String languageCode)
languageCode
: The language code of the programming language to check which countries support it.
The method returns an unmodifiable list of countries that support the given language.
import org.apache.commons.lang3.LocaleUtils;public class Main{public static void main(String[] args){// Example 1String languageCode = "hi";System.out.println("The list of countries that support the language code " + languageCode + " is as follows:");System.out.println(LocaleUtils.countriesByLanguage(languageCode));// Example 2languageCode = "OL";System.out.println("The list of countries that support the language code " + languageCode + " is as follows:");System.out.println(LocaleUtils.countriesByLanguage(languageCode));// Example 3languageCode = null;System.out.println("The list of countries that support the language code " + languageCode + " is as follows:");System.out.println(LocaleUtils.countriesByLanguage(languageCode));}}
The list of countries that support the language code hi are as follows:
[hi_IN]
The list of countries that support the language code OL is as follows:
[]
The list of countries that support the language code null are as follows:
[]
languageCode = "hi"
The method returns [hi_IN]
, i.e., the countries that support the language Hindi.
languageCode = "OL"
The method returns []
, as there is no language with the language code OL
.
languageCode = null
The method returns []
, as the language code is null
.
Free Resources