Getting default language from country code in Java

Viewed 1271

I need to find the language based on country code. For country code "US", it should return "en" or "CN" should return "zh". I went through java.util.Locale, it works on LanguageCode rather than countryCode. Also, Locale.getAvailableLocales is not helpful here, as it returns multiple languages for same countryCode. Is there anything else that can be useful. I want to avoid initialising all useful locale in a file and then using it. (as it may require update regularly whenever we have to use a new Locale)

Thanks in advance

Looked into other stackoverflow questions, could not find answers thus posting a new question here.

3 Answers

You can build a HashMap to map the country codes to the set of languages as follows:

Locale[] locales = Locale.getAvailableLocales();
Map<String,HashSet<String>> countryLanguageMap = new HashMap<>();

for (Locale locale : locales) {
     String country = locale.getCountry(), language = locale.getLanguage();
     if(!country.isEmpty() && !language.isEmpty()) {
          if(countryLanguageMap.containsKey(country))
               countryLanguageMap.get(country).add(language);
          else
               countryLanguageMap.put(country, new HashSet<String>(Arrays.asList(language)));
     }
}

Then, get the languages of a country code as follows:

countryLanguageMap.get("US"); // [en, es]

There is no right answer to this question.

Some countries have multiple official languages. Belgium has three national languages: Dutch, French and German. These are spoken in the north, south and east region of the country. Lots of Belgian websites have a homepage that consists of a language choice only. Almost all of them allow you to easily switch languages. Canada has two national languages: English and French. Both are used in New Brunswick, Quebec uses predominantly French, the rest uses predominantly English. Switzerland has four national languages: German, French, Italian and Romansh. I'm not even going to try to explain this one.

Some languages are different between countries. Both Belgium and France speak French. They use completely different words to describe the same thing. The word "déjeuner" means lunch in France and breakfast in Belgium.

If you're trying to write a multilingual java web application I suggest using a ResourceBundle. You'll read the locale information from the Accept-Language http header or simply ask the user.

# Messages.properties
# fallback, usually assumes English, United States.
breakfast = breakfast
lunch = lunch
supper = supper
# Messages_fr.properties
# fallback for French if country isn't France
breakfast = déjeuner
lunch = dȋner
supper = souper
# Messages_fr_FR.properties
# French if country is France
breakfast = petit-déjeuner
lunch = déjeuner
supper = dȋner

You should read the javadoc for java.util.ResourceBundle. Also beware the default encoding for ResourceBundle property files has changed between java 8 and 9.

There is nothing like a default language for a country, although most countries does have just one (official) language. In the reverse, only few languages are spoken in just one single country. But several languages do have a variant that is specific to a single country.

That's the reason for designing java.util.Locale as it was designed. The locale is not a 'country' code, but mainly a language code. But on a first glance, it seems that most country variants for languages are defined in that class.

If you really want to select a language based on the country, you need to set up your own map, perhaps like this:

Map<String,Locale> languagesForCountries = new HashMap<>(); 

But to fill that, you need some manual work:

languagesForCountries.put( "US", Locale.US );
languagesForCountries.put( "DE", Locale.GERMANY );
languagesForCountries.put( "BR", new Locale( "pt", "BR" );
languagesForCountries.put( "AT", new Locale( "de", "AT" );
… // and so on

Perhaps you find an already existing database somewhere that you can use.

Related