Java Get Countries by Language Code using Apache Commons Lang

Tags: LocaleUtils Apache Commons Apache Commons Lang Locale

In this Java tutorial we learn how to use the LocaleUtils class of the Apache Commons Lang library to get a list of countries that support a given language.

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.

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.12.0</version>
</dependency>

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 Countries by Language Code in Java

The Apache Commons Lang library provides the method LocaleUtils.countriesByLanguage() to return a list of Locale objects of countries which supported a given language code. The following example code shows you how to get all countries available for English language, “en” code.

GetCountriesByLanguage.java

import org.apache.commons.lang3.LocaleUtils;

import java.util.List;
import java.util.Locale;

public class GetCountriesByLanguage {
    public static void main(String... args) {
        List<Locale> locales = LocaleUtils.countriesByLanguage("en");

        System.out.println("Countries support English:");
        for (Locale locale : locales) {
            System.out.println(locale.getDisplayName());
        }
    }
}
The output is:
Countries support English:
English (United States)
English (Singapore)
English (Malta)
English (Philippines)
English (New Zealand)
English (South Africa)
English (Australia)
English (Ireland)
English (Canada)
English (India)
English (United Kingdom)

Happy Coding 😊