Coder Eye
In this post: Getting countries list in Java I wrote about how to get countries list in Java. Getting languages list is quite similar. In fact by making small changes to the code on the countries post languages list can be easily taken.
In order to get the countries list we used this static method of Locale class: Locale.getISOCountries(). Getting languages list is quite similar and can be taken by using this method: Locale.getISOLanguages(). This method returns String array containing all language codes compatible with ISO 639 standard (2 letters code).
Similar to the countries list, we will use a class named Language to store information of a single language:
private String languageCode;
public Language(String languageCode, String name)
this.languageCode = languageCode;
public String getLanguageCode()
public void setLanguageCode(String languageCode)
this.languageCode = languageCode;
public void setName(String name)
return languageCode + ", " + name;
And the class LanguageUtil is very similar to CountyUtil, but instead of countries it returns languages:
public class LanguageUtil
public static List getLanguages(final Locale inLocale)
String[] languageCodes = Locale.getISOLanguages();
List languages = new ArrayList(languageCodes.length);
for (String languageCode : languageCodes)
languages.add(new Language(languageCode, new Locale(languageCode, " ").getDisplayLanguage(inLocale)));
Collections.sort(languages, new Comparator()
public int compare(Language c1, Language c2)
return c1.getName().compareTo(c2.getName());
public static Map getLanguagesMap(final Locale inLocale)
List countries = getLanguages(inLocale);
Map countriesMap = new LinkedHashMap(countries.size());
for (Language country : countries)
countriesMap.put(country.getLanguageCode(), country.getName());
public static void printLanguages(List languages)
for (Language language : languages)
public static void main(String[] args)
// Get list of countries in US English
System.out.println("---- List of languages in English ----");
List languages = getLanguages(Locale.US);
System.out.println("\n\n---- List of languages in Japanese ----");
// Get list of countries in Japanese
languages = getLanguages(Locale.JAPANESE);
And the output for our small test program, which prints all languages once in English and once in Japanese:
---- List of languages in English -------- List of languages in Japanese ----Java Get Languages by Country Code using Apache Commons Lang
In this Java tutorial we learn how to get the list of languages supported for a specific country using the LocaleUtils class of Apache Commons Lang library.
How to add Apache Commons Lang 3 library to your Java project
To use the Apache Commons Lang 3 library in the Gradle build project, add the following dependency into the build.gradle file.
implementation 'org.apache.commons:commons-lang3:3.12.0'
To use the Apache Commons Lang 3 library in the Maven build project, add the following dependency into the pom.xml file.
org.apache.commons commons-lang3 3.12.0
To have more information about the Apache Commons Lang 3 library you can visit the library home page at commons.apache.org/proper/commons-lang/
How to get Locale Languages by Country Code in Java
The Apache Commons Lang library provides the method LocaleUtils.languagesByCountry() to obtain the list of languages supported for a given country code. For example in the below Java program we get languages supported in the United States (US) and Australia (AU).
import org.apache.commons.lang3.LocaleUtils; import java.util.List; import java.util.Locale; public class GetLanguagesByCountry public static void main(String. args) ListLocale> languagesByUS = LocaleUtils.languagesByCountry("US"); System.out.println("Languages at United States:"); for(Locale locale : languagesByUS) System.out.println(locale.getDisplayLanguage()); > ListLocale> languagesByAU = LocaleUtils.languagesByCountry("AU"); System.out.println("Languages at Australia:"); for(Locale locale : languagesByAU) System.out.println(locale.getDisplayLanguage()); > > >
Languages at United States: English Spanish Languages at Australia: English