Mad Hatter Mad Hatter - 4 months ago 36
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.


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