languagesByCountry
is a 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.
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> languagesByCountry(String countryCode)
String countryCode
: The two letter country code.
The method returns the languages supported by the given country.
import org.apache.commons.lang3.LocaleUtils;public class Main{public static void main(String[] args){// Example 1String 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 2countryCode = "OL";System.out.println("The list of languages supported by the country code " + countryCode + " are as follows:");System.out.println(LocaleUtils.languagesByCountry(countryCode));// Example 3countryCode = null;System.out.println("The list of languages supported by the country code " + countryCode + " are as follows:");System.out.println(LocaleUtils.languagesByCountry(countryCode));}}
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.
country code = OL
The method returns []
because there is no country with the country code OL.
country code = null
The method returns []
because the country code is null
.
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