How do I get all availables Locale’s language codes?

When you want to internationalize your application, you need to know the language code and country code. These codes will be used as the properties file name (the file name must be ended in language_COUNTRY.properties). Here is an example that show how to get all supported language’s code.

package org.kodejava.example.util;

import java.util.Locale;

public class LocaleCountryLanguageCode {
    public static void main(String[] args) {

        //
        // Gets an array of all installed locales. The returned
        // array represents the union of locales supported by the
        // Java runtime environment and by installed
        // LocaleServiceProvider implementations.
        //
        Locale[] locales = Locale.getAvailableLocales();

        StringBuilder sb = new StringBuilder();
        for (Locale locale : locales) {
            sb.append("Locale name: ").append(locale.getDisplayName())
                    .append(" = ")
                    .append(locale.getLanguage()).append("_")
                    .append(locale.getCountry()).append("n");
        }
        System.out.println(sb.toString());
    }
}

Here a some of the results produces by the code above:

...
...
Locale name: Japanese (Japan) = ja_JP
Locale name: Spanish (Peru) = es_PE
Locale name: English (United States) = en_US
Locale name: French (Belgium) = fr_BE
Locale name: Indonesian (Indonesia) = in_ID
Locale name: German (Germany) = de_DE
Locale name: English (Singapore) = en_SG
...
...
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply