Convert da-DK to Danish (Denmark) etc

≡放荡痞女 提交于 2019-12-12 05:15:12

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/40427876/convert-da-dk-to-danish-denmark-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!