Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

localeutils
java
communitycreator

What is LocaleUtils.countriesByLanguage() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

countriesByLanguage is a staticthe methods in Java that can be called without creating an object of the class. method of the LocaleUtils class. Locale objects provide information based on geographic locations.

The countriesByLanguage method retrieves the list of countries that support a particular language.

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> countriesByLanguage(String languageCode)

Parameters

languageCode: The language code of the programming language to check which countries support it.

Return value

The method returns an unmodifiable list of countries that support the given language.

Code

import org.apache.commons.lang3.LocaleUtils;
public class Main{
public static void main(String[] args){
// Example 1
String 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 2
languageCode = "OL";
System.out.println("The list of countries that support the language code " + languageCode + " is as follows:");
System.out.println(LocaleUtils.countriesByLanguage(languageCode));
// Example 3
languageCode = null;
System.out.println("The list of countries that support the language code " + languageCode + " is as follows:");
System.out.println(LocaleUtils.countriesByLanguage(languageCode));
}
}

Output

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:
[]

Explanation

Example 1

  • languageCode = "hi"

The method returns [hi_IN], i.e., the countries that support the language Hindi.

Example 2

  • languageCode = "OL"

The method returns [], as there is no language with the language code OL.

Example 3

  • languageCode = null

The method returns [], as the language code is null.

RELATED TAGS

localeutils
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring