java java或java库中是否有语言代码的常量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7223788/
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
Are there constants for language codes in java or in a java library?
提问by Martin Schlagnitweit
Are there any constants for language codes like "en"
or "de"
in java or in a java library? (Or is using the strings OK?)
是否有像语言代码的任何常量"en"
或"de"
Java或在一个Java库?(或者使用字符串可以吗?)
I know that something like
我知道像
Locale.COUNTRY-NAME.getLanguage()
would work, but I am searching for something more streamlined like
会工作,但我正在寻找更精简的东西,比如
Locale.LANGUAGE-NAME
采纳答案by Itay Maman
I am afraid there aren't constants for all languages.
You do have several predefined Locales such as Locale.UK
Locale.US
, etc. Each locale has a language code which can be obtained via the getLanguage()
method.
恐怕不是所有语言都有常数。您确实有几个预定义的区域设置,例如Locale.UK
Locale.US
等。每个区域设置都有一个语言代码,可以通过该getLanguage()
方法获取。
To get all language code supported by the underlying JVM use getISOLanguages()
要获取底层 JVM 支持的所有语言代码,请使用 getISOLanguages()
for(String lang : Locale.getISOLanguages()) {
System.out.println(lang);
}
More details: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Locale.html
更多详情:http: //download.oracle.com/javase/1.4.2/docs/api/java/util/Locale.html
回答by MaDa
The 2-letter language codes are defined by ISO 639-1 standard. The java.util.Locale
class does not contain them all, but only Locales supported by the VM (as per Locale.getAvailableLocales()
).
2 个字母的语言代码由 ISO 639-1 标准定义。本java.util.Locale
类不包含所有这些,但只有语言环境由VM支持(按照Locale.getAvailableLocales()
)。
The easiest way to access all ISO 639-1 language codes in Java is to rely on the International Components for Unicodeproject (ICU4J) — an extensive, widely used and actively developed i18n library from IBM. You can get the list of all languages from the ULocale class: com.ibm.icu.util.ULocale.getISOLanguages().
在 Java 中访问所有 ISO 639-1 语言代码的最简单方法是依赖International Components for Unicode项目 (ICU4J)——IBM 提供的一个广泛、广泛使用和积极开发的 i18n 库。您可以从 ULocale 类中获取所有语言的列表:com.ibm.icu.util.ULocale.getISOLanguages()。
回答by Varun Achar
Yup.. Use Locale.COUNTRY-NAME
是的..使用 Locale.COUNTRY-NAME