What is LocaleUtils.languagesByCountry() in Java?

languagesByCountry is a staticdescribes the methods in Java that can be called without creating an object of the class method of the LocaleUtils class that is used to obtain 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.

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 List<Locale> languagesByCountry(String countryCode)

Parameters

String countryCode: The two letter country code.

Return value

The method returns the languages supported by the given country.

Code

import org.apache.commons.lang3.LocaleUtils;
public class Main{
public static void main(String[] args){
// Example 1
String countryCode = "US";
System.out.println("The list of languages supported by the country code " + countryCode + " are as follows:");
System.out.println(LocaleUtils.languagesByCountry(countryCode));
// Example 2
countryCode = "OL";
System.out.println("The list of languages supported by the country code " + countryCode + " are as follows:");
System.out.println(LocaleUtils.languagesByCountry(countryCode));
// Example 3
countryCode = null;
System.out.println("The list of languages supported by the country code " + countryCode + " are as follows:");
System.out.println(LocaleUtils.languagesByCountry(countryCode));
}
}

Example 1

  • country code = US

The method returns [lkt_US, es_US, en_US, haw_US, chr_US], which are the languages supported by the United States of America.

Example 2

  • country code = OL

The method returns [] because there is no country with the country code OL.

Example 3

  • country code = null

The method returns [] because the country code is null.

Expected output

The expected output of the code is as follows:


The list of languages supported by the country code US are as follows:
[lkt_US, es_US, en_US, haw_US, chr_US]
The list of languages supported by the country code OL are as follows:
[]
The list of languages supported by the country code null are as follows:
[]

Free Resources

Attributions:
  1. undefined by undefined