Mad Hatter Mad Hatter - 29 days ago 8
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();
Arrays.sort(systemLanguages);
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));
recyclerAdapter.notifyDataSetChanged();
}
}


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

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

Comments