Code Poet
Code Poet

Reputation: 8003

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.

Upvotes: 0

Views: 1674

Answers (1)

Enwired
Enwired

Reputation: 1593

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

Upvotes: 1

Related Questions