Mad Hatter Mad Hatter - 1 year ago 98
Java Question

Convert da-DK to Danish (Denmark) etc

How do I convert locale ids into display languages and countries in Java. For instance, I have a list of locales in this format: "da-DK" and I want them all to show as "Danish (Denmark)".

Here is my code so far:

public void getAvailableLocales() {
systemLanguages = Resources.getSystem().getAssets().getLocales();
for (int i = 0; i < systemLanguages.length; i++) {
String sL = systemLanguages[i];
Locale loc = new Locale(sL);
String locDisplayResults = loc.getDisplayName();
languagesList.add(new Languages(locDisplayResults));

Unfortunately, getDisplayName() is only working for cases where there is just two letters for the language, such as "ar" for "Arabic". It leaves "ar-EG" untouched.

Answer Source

Instead of using new Locale(sL), use Locale.forLanguageTag(sL).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download