How to get the local country from the language?

How to find country from Locale-language ?

eg; if I set the local language to en , then I expect to get US as a country.

 Locale locale = new Locale("en"); locale.getCountry() // returns null 

The problem is that I have a country code, such as: fr,de,en , and now I just want to find the correct country code from these codes.

+4
source share
2 answers

en - English. en_US - English. en_GB , I think British English. Presumably using en_US will do what you want. Here is a list of all supported locales: http://www.oracle.com/technetwork/java/javase/locales-137662.html

You cannot get the country from "en" because, as you can see, several countries can share the language. Which one would you choose?

+4
source

One option is to get all the allowed locales:

 Locale[] locales = Locale.getAvailableLocales(); 

then paste them into the HashMap and define language as key . Now you can search for the country through the language key.

+1
source

Source: https://habr.com/ru/post/1500709/


All Articles