开发者

How to get languages list in Android

Hello i need to get the list of the languages defined on an Android device and would like to populate a Spin开发者_JS百科ner with language code and language name. How can i do that?

Thanks in advance and greetings c.


Use:

Resources.getSystem().getAssets().getLocales();


public static List<String> getLanguages() {
    String[] locales = Resources.getSystem().getAssets().getLocales();
    List<String> list = new ArrayList<>();

    for (Locale locale : Locale.getAvailableLocales()) {
        if (locale.getLanguage().length() == 2) {
            if (!isLanguageInList(list, locale)) {
                list.add(locale.getDisplayLanguage());
            }
        }
    }
    Collections.sort(list);
    return list;
}

private static boolean isLanguageInList(List<String> list, Locale locale) {
    if (list == null) {
        return false;
    }
    for (String item: list) {
        if (item.equalsIgnoreCase(locale.getDisplayLanguage())){
            return true;
        }
    }
    return false;
}


Simply use Locale.getAvailableLocales().


I've faced same issue. I've figured out how get locales defined in

phone settings -> system -> languages and input

kotlin

val locales = Resources.getSystem().configuration.locales

this works since API 24.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜