How do I get all available Locale 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.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();

        for (Locale locale : locales) {
            System.out.printf("Locale name: %s = %s_%s%n",
                    locale.getDisplayName(), locale.getLanguage(), locale.getCountry());
        }
    }
}

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

...
Locale name: Kikuyu (Latin, Kenya) = ki_KE
Locale name: Spanish (Brazil) = es_BR
Locale name: Koyra Chiini (Mali) = khq_ML
Locale name: English (Solomon Islands) = en_SB
Locale name: Tibetan (Tibetan, China) = bo_CN
Locale name: Cherokee (United States) = chr_US
Locale name: Kinyarwanda (Rwanda) = rw_RW
Locale name: Tachelhit (Tifinagh, Morocco) = shi_MA
Locale name: Arabic (Iraq) = ar_IQ
Locale name: Nyankole = nyn_
...

How do I format date using a locale based format?

The code below demonstrate how to format date information for a specific locale. In the example utilize the java.text.SimpleDateFormat class.

package org.kodejava.text;

import java.util.Locale;
import java.util.Date;
import java.text.SimpleDateFormat;

public class FormatDateLocale {
    public static void main(String[] args) {
        // Defines an array of Locale we are going to use for
        // formatting date information.
        Locale[] locales = new Locale[] {
            Locale.JAPAN,
            Locale.CHINA,
            Locale.KOREA,
            Locale.TAIWAN,
            Locale.ITALY,
            Locale.FRANCE,
            Locale.GERMAN
        };

        // Get an instance of current date time
        Date today = new Date();

        // Iterates the entire Locale defined above and create a long
        // formatted date using the SimpleDateFormat.getDateInstance()
        // with the format, the Locale and the date information.
        for (Locale locale : locales) {
            System.out.printf("Date format in %s = %s%n",
                locale.getDisplayName(), SimpleDateFormat.getDateInstance(
                    SimpleDateFormat.LONG, locale).format(today));
        }
    }
}

The result of our code are:

Date format in Japanese (Japan) = 2021年10月6日
Date format in Chinese (China) = 2021年10月6日
Date format in Korean (South Korea) = 2021년 10월 6일
Date format in Chinese (Taiwan) = 2021年10月6日
Date format in Italian (Italy) = 6 ottobre 2021
Date format in French (France) = 6 octobre 2021
Date format in German = 6. Oktober 2021

How do I get currency symbol?

package org.kodejava.util;

import java.util.Currency;
import java.util.Locale;

public class CurrencySymbol {
    public static void main(String[] args) {
        Currency currency = Currency.getInstance(Locale.JAPAN);
        System.out.println("Japan = " + currency.getSymbol());

        currency = Currency.getInstance(Locale.UK);
        System.out.println("UK = " + currency.getSymbol());

        currency = Currency.getInstance(Locale.US);
        System.out.println("US = " + currency.getSymbol());

        currency = Currency.getInstance(new Locale("in", "ID"));
        System.out.println("Indonesia = " + currency.getSymbol());
    }
}

The result of the code snippet above:

Japan = ¥
UK = £
US = $
Indonesia = IDR

How do I use ResourceBundle for i18n?

Creating an application for users in different regions can be hard in terms of the message format for the local region. Java provide a ResourceBundle class that help internationalize our application.

To create resources for i18n (there are 18 letters between the first i and the final n) we need to create a file for each locale our application supported. The file name must be ended in language_COUNTRY.properties. For instance a resource bundle for Locale.UK will be MessagesBundle_en_GB.properties.

When the bundle has been loaded we can use bundle.getString(key) to read specific message from our resource bundle file.

package org.kodejava.util;

import java.util.Locale;
import java.util.ResourceBundle;

public class InternationalizationDemo {

    public static void main(String[] args) {
        // Load resource bundle for Locale.UK locale. The resource 
        // bundle will load the MessagesBundle_en_GB.properties file.
        ResourceBundle bundle =
                ResourceBundle.getBundle("MessagesBundle", Locale.UK);
        System.out.println("Message in " + Locale.UK + ": " +
                bundle.getString("greeting"));

        // Change the default locale to Indonesian and get the default 
        // resource bundle for the current locale.
        Locale.setDefault(new Locale("in", "ID"));
        bundle = ResourceBundle.getBundle("MessagesBundle");
        System.out.println("Message in " + Locale.getDefault() + ": " +
                bundle.getString("greeting"));
    }
}

Below are some example of our resource bundle files, these files should be located in our application classpath to enable the ResourceBundle class to read it.

MessagesBundle_en_GB.properties

greeting=Hello, how are you?

MessagesBundle_in_ID.properties

greeting=Halo, apa kabar?

How do I set default Locale?

package org.kodejava.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class DefaultLocaleExample {
    public static void main(String[] args) {
        // Use Random class to generate some random number
        Random random = new Random();

        // We use the system default locale to format a number and a date.
        NumberFormat formatter = new DecimalFormat();
        Locale locale = Locale.getDefault();
        System.out.println("Default Locale = " + locale);
        System.out.println("Number         = " + formatter.format(random.nextDouble()));
        System.out.println("Date           = " + new SimpleDateFormat().format(new Date()));

        // We change the default locale to Locale.ITALY by setting it through 
        // the Locale.setDefault() method, and then we format another number
        // and date using a new locale. This change will affect all the class 
        // that aware to the Locale, such as the NumberFormat class.
        Locale.setDefault(Locale.ITALY);
        NumberFormat newFormatter = new DecimalFormat();
        System.out.println("New Locale     = " + Locale.getDefault());
        System.out.println("Number         = " + newFormatter.format(random.nextDouble()));
        System.out.println("Date           = " + new SimpleDateFormat().format(new Date()));
    }
}

The result of the code snippet above are:

Default Locale = en_US
Number         = 0.557
Date           = 9/26/21, 12:05 PM
New Locale     = it_IT
Number         = 0,217
Date           = 26/09/21, 12:05