3

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.

Amit Kumar
  • 377
  • 4
  • 17

3 Answers3

5

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]
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
  • Suggestion: HashSet languageSet = countryLanguageMap.getOrDefault(country, null); if (languageSet == null) { countryLanguageMap.put(country, new HashSet<>(Collections.singletonList(language))); } else { languageSet.add(language); } – F. Müller Dec 31 '20 at 14:11
  • Nice trick! Although it gets you countries like 419 … – tquadrat Dec 31 '20 at 14:11
  • How does this address the Question which asks for a default language per country (which does not exist in many countries)? – Basil Bourque Dec 31 '20 at 22:10
3

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.

Kristof Neirynck
  • 3,934
  • 1
  • 33
  • 47
  • I'm not quite sure that having a language difference because of a word meaning breakfast in one and lunch in another matters much. Think of the US's four meals: breakfast, lunch, supper, and dinner. – NomadMaker Dec 31 '20 at 15:47
  • True, my example is flawed. The uk and us share a language, but write their dates differently. 1/2/2021 can be February 1st or January 2nd. The first floor is one floor lower in the us. 1,234 is a larger number in the us than it is in Europe because the decimal separator is different. The word “pants” is interpreted differently in the us and the uk. Then there is color and colour. Or lift and elevator. These small things add up. – Kristof Neirynck Dec 31 '20 at 17:12
  • Yes, two countries divided by a common language. -- B. Shaw. – NomadMaker Dec 31 '20 at 17:14
0

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.

tquadrat
  • 3,033
  • 1
  • 16
  • 29
  • 1
    Regarding the statement "most countries have just one (official) language", I count over 60 countries with multiple official languages, and over 25 of the remaining single-official-language countries have secondary regional languages commonly in use. This according to Wikipedia page for [*List of official languages by country and territory*](https://en.wikipedia.org/wiki/List_of_official_languages_by_country_and_territory). So trying the define a default language will be a problem in many places. – Basil Bourque Dec 31 '20 at 22:21
  • @BasilBourque – I thought I said that … although I am surprised how narrow the margin for „most“ countries in fact is. – tquadrat Jan 01 '21 at 12:01