Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there constants for language codes in java or in a java library?

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.getLanguage()

would work, but I am searching for something more streamlined like

Locale.LANGUAGE-NAME
like image 439
Martin Schlagnitweit Avatar asked Aug 28 '11 20:08

Martin Schlagnitweit


People also ask

How can I change the language of Java?

setDefault(java. util. Locale. ENGLISH); JFileChooser chooser = new JFileChooser(); chooser.

What is the difference between language and locale?

Setting a thread's locale or changing your system locale will change how numbers, dates, and times are displayed for controls created on that thread or running on your system, respectively. A language, on the other hand, is what we speak, read, and write.

What does locale mean in Java?

A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user.

How do I get the locale in Java?

To get equivalent information in Java, use Locale. getDefault() to get the Locale that Java is using, and use methods on the Locale object such as getCountry() , getLanguage() to get details. The information is available using ISO codes and as human readable/displayable names.


2 Answers

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

like image 166
Itay Maman Avatar answered Oct 27 '22 09:10

Itay Maman


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().

like image 31
MaDa Avatar answered Oct 27 '22 09:10

MaDa