Search code examples
javainternationalizationlocaleconstants

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

Solution

  • 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