java 从语言代码中获取该语言的语言名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36061116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:55:49  来源:igfitidea点击:

Get language name in that language from language code

javaandroidlocale

提问by Charlie-Blake

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

我有一个语言代码列表(如“en”、“es”...),我需要用这些语言显示,如下所示:

English
Espa?ol
Fran?ais
Deutsch
日本語

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

是否有任何内置 API 可以在 Android 中获取这些内容,还是我应该自己映射它们?

回答by nvi9

The Localeclass have a methodfor this: public String getDisplayLanguage(Locale locale), as the documentation says:

Locale班有一个方法,这个:public String getDisplayLanguage(Locale locale)作为文档说:

Returns the name of this locale's language, localized to?locale. The exact output form depends on whether this locale corresponds to a specific language, script, country and variant.

返回此语言环境的语言名称,本地化为?locale. 确切的输出形式取决于此区域设置是否对应于特定的语言、脚本、国家/地区和变体。

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

//...