Are there any constants for language codes like "en"
or "de"
in java or in a java library? (Or is using the strings OK?)
I know that something like
Locale.COUNTRY-NAME.getLangu开发者_如何学运维age()
would work, but I am searching for something more streamlined like
Locale.LANGUAGE-NAME
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.
To get all language code supported by the underlying JVM use 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
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()
).
The easiest way to access all ISO 639-1 language codes in Java is to rely on the International Components for Unicode project (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().
Yup.. Use Locale.COUNTRY-NAME
精彩评论