Get language name in that language from language c

2019-01-26 05:31发布

问题:

I have a list of language codes (as in "en", "es"...) I need to display in those language like this:

English
Español
Français
Deutsch
日本語

Is there any built-in API to get these in Android or should I map them myself?

回答1:

The Locale class have a method for this: public String getDisplayLanguage(Locale locale). From the documentation: "Returns the name of this locale's language, localized to locale. If the language name is unknown, the language code is returned.". So you can get language names for locales like this:

String lng = "en";
Locale loc = new Locale(lng);
String name = loc.getDisplayLanguage(loc); // English

lng = "es";
loc = new Locale(lng);
name = loc.getDisplayLanguage(loc); // español

//...