How to Use Locale for Internationalization

Internationalization (i18n) involves designing applications so that they can be adapted to different languages, regions, and cultures. In Java, the Locale class is a fundamental part of i18n. It represents a specific geographical, political, or cultural region and is used in conjunction with various APIs to format dates, numbers, and text according to a specific locale.

Here are the basic steps to use Locale for internationalization:


1. Creating a Locale

You can create a Locale object in a few different ways:

package org.kodejava.util;

import java.util.Locale;

public class LocaleExample {
    public static void main(String[] args) {
        // Using predefined constants
        Locale defaultLocale = Locale.getDefault();  // System default locale
        Locale usLocale = Locale.US;                 // United States

        // Using constructors
        Locale customLocale = new Locale("fr", "FR");  // French (France)

        // Using Locale.Builder (for more control)
        Locale builderLocale = new Locale.Builder()
                .setLanguage("de")  // German
                .setRegion("DE")    // Germany
                .build();

        System.out.println("Default Locale: " + defaultLocale);
        System.out.println("US Locale: " + usLocale);
        System.out.println("Custom Locale: " + customLocale);
        System.out.println("Builder Locale: " + builderLocale);
    }
}

2. Using Locale with Date/Time Formatting

Locale is commonly used to format dates and times in a way that is familiar to a specific region:

package org.kodejava.util;

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

public class DateLocalizationExample {
    public static void main(String[] args) {
        Date now = new Date();

        // Formatting date in French (France)
        Locale frenchLocale = new Locale("fr", "FR");
        DateFormat frenchDateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, frenchLocale);
        System.out.println("Date in French: " + frenchDateFormatter.format(now));

        // Formatting date in German (Germany)
        Locale germanLocale = new Locale("de", "DE");
        DateFormat germanDateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, germanLocale);
        System.out.println("Date in German: " + germanDateFormatter.format(now));
    }
}

3. Using Locale with Numbers and Currency Formatting

The NumberFormat class allows you to format numbers and currencies according to a locale:

package org.kodejava.util;

import java.text.NumberFormat;
import java.util.Locale;

public class NumberLocalizationExample {
    public static void main(String[] args) {
        double amount = 12345.67;

        // Format currency in US locale
        Locale usLocale = Locale.US;
        NumberFormat usFormatter = NumberFormat.getCurrencyInstance(usLocale);
        System.out.println("In US: " + usFormatter.format(amount));

        // Format currency in Japanese locale
        Locale japanLocale = Locale.JAPAN;
        NumberFormat japanFormatter = NumberFormat.getCurrencyInstance(japanLocale);
        System.out.println("In Japan: " + japanFormatter.format(amount));
    }
}

4. Internationalizing Messages with ResourceBundles

For text and messages, Java provides the ResourceBundle class, which allows you to store localized strings in property files.

  1. Create Properties Files (e.g., messages_en_US.properties, messages_fr_FR.properties):
    # messages_en_US.properties
    greeting=Hello
    farewell=Goodbye
    
    # messages_fr_FR.properties
    greeting=Bonjour
    farewell=Au revoir
    
  2. Read ResourceBundle Based on Locale:
    package org.kodejava.util;
    
    import java.util.Locale;
    import java.util.ResourceBundle;
    
    public class ResourceBundleExample {
      public static void main(String[] args) {
         // Locale for English (US)
         Locale usLocale = new Locale("en", "US");
         ResourceBundle bundleUS = ResourceBundle.getBundle("messages", usLocale);
         System.out.println("US Greeting: " + bundleUS.getString("greeting"));
         System.out.println("US Farewell: " + bundleUS.getString("farewell"));
    
         // Locale for French (France)
         Locale frLocale = new Locale("fr", "FR");
         ResourceBundle bundleFR = ResourceBundle.getBundle("messages", frLocale);
         System.out.println("French Greeting: " + bundleFR.getString("greeting"));
         System.out.println("French Farewell: " + bundleFR.getString("farewell"));
      }
    }
    

5. Switching Locales Dynamically

You can dynamically switch between different locales at runtime, based on user preferences or system settings:

package org.kodejava.util;

import java.util.Locale;

public class LocaleSwitcher {
   public static void setLocale(String language, String country) {
      Locale.setDefault(new Locale(language, country));
   }

   public static void main(String[] args) {
      // Default locale
      System.out.println("Default Locale: " + Locale.getDefault());

      // Switch to French
      setLocale("fr", "FR");
      System.out.println("Current Locale: " + Locale.getDefault());
      // Perform locale-specific operations...

      // Switch back to English
      setLocale("en", "US");
      System.out.println("Current Locale: " + Locale.getDefault());
      // Perform locale-specific operations...
   }
}

Key Points:

  1. The Locale object is essential for tailoring applications for specific languages and regions.
  2. Utilize DateFormat, NumberFormat, and ResourceBundle for locale-based formatting and localized messages.
  3. Keep localized data (like messages) in separate resource files (.properties) to facilitate easier translation.
  4. Avoid hardcoding language-specific content directly in the code—this ensures maintainability and scalability.

How do I get all available currency codes?

The example presented in this code snippet show you how to get the available currency codes. We will need the locale information and use the Currency class for this example.

package org.kodejava.util;

import java.util.Currency;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

public class CurrencySymbolDemo {
    public static void main(String[] args) {
        CurrencySymbolDemo cs = new CurrencySymbolDemo();

        Map<String, String> currencies = cs.getAvailableCurrencies();
        for (String country : currencies.keySet()) {
            String currencyCode = currencies.get(country);
            System.out.println(country + " => " + currencyCode);
        }
    }

    /**
     * Get the currencies code from the available locales information.
     *
     * @return a map of currencies code.
     */
    private Map<String, String> getAvailableCurrencies() {
        Locale[] locales = Locale.getAvailableLocales();

        // We use TreeMap so that the order of the data in the map sorted
        // based on the country name.
        Map<String, String> currencies = new TreeMap<>();
        for (Locale locale : locales) {
            try {
                currencies.put(locale.getDisplayCountry(),
                        Currency.getInstance(locale).getCurrencyCode());
            } catch (Exception e) {
                // when the locale is not supported
            }
        }
        return currencies;
    }
}

You will have something like this printed on the screen:

...
Honduras => HNL
Hong Kong SAR China => HKD
Hungary => HUF
Iceland => ISK
India => INR
Indonesia => IDR
Iran => IRR
Iraq => IQD
Ireland => EUR
Isle of Man => GBP
...

How do I change the date format symbols for a specified locale?

package org.kodejava.text;

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

public class ChangeDateFormatSymbols {
    public static void main(String[] args) {
        Locale id = new Locale("in", "ID");
        String pattern = "EEEE, dd MMM yyyy";
        Date today = new Date();

        // Gets formatted date specify by the given pattern for
        // Indonesian Locale no changes for default date format
        // is applied here.
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, id);
        String before = sdf.format(today);
        System.out.println("Before format change: " + before);

        // Create a DateFormatSymbols object for Indonesian locale.
        DateFormatSymbols dfs = new DateFormatSymbols(id);

        // Gets String array of default format of weekdays.
        String[] days = dfs.getWeekdays();
        String[] newDays = new String[days.length];
        for (int i = 0; i < days.length; i++) {
            // For each day, apply toUpperCase() method to
            // capitalized it.
            newDays[i] = days[i].toUpperCase();
        }

        // Set String array of weekdays.
        dfs.setWeekdays(newDays);

        // Gets String array of default format of short months.
        String[] shortMonths = dfs.getShortMonths();
        String[] months = new String[shortMonths.length];
        for (int j = 0; j < shortMonths.length; j++) {
            // For each short month, apply toUpperCase() method
            // to capitalized it.
            months[j] = shortMonths[j].toUpperCase();
        }

        // Set String array of short months.
        dfs.setShortMonths(months);

        // Create a SimpleDateFormat object by given pattern and 
        // symbol and then format the date object as String.
        sdf = new SimpleDateFormat(pattern, dfs);
        String after = sdf.format(today);
        System.out.println("After change format : " + after);
    }
}

Here are the output of our program:

Before format change: Selasa, 19 Okt 2021
After change format : SELASA, 19 OKT 2021

How do I get a formatted date for a specific pattern and locale?

If you want to change formatting styles provided by DateFormat, you can use SimpleDateFormat class. The SimpleDateFormat class is locale-sensitive.

If you instantiate SimpleDateFormat without a Locale parameter, it will format the date and time according to the default Locale. Both the pattern and the Locale determine the format. For the same pattern, SimpleDateFormat may format a date and time differently if the Locale varies.

package org.kodejava.text;

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

public class SimpleDateFormatChangeLocalePattern {
    public static void main(String[] args) {
        String pattern = "dd-MMM-yyyy";
        Date today = new Date();

        // Gets a formatted date according to the given pattern.
        // Here only the pattern is passed as argument of the
        // SimpleDateFormat constructor, so it will format the
        // date according to the default Locale.
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String local = sdf.format(today);
        System.out.println("Date in default locale: " + local);

        Locale[] locales = {
                Locale.CANADA,
                Locale.FRANCE,
                Locale.GERMANY,
                Locale.US,
                Locale.JAPAN
        };

        for (Locale locale : locales) {
            // Format a date according to the given pattern for each locale.
            sdf = new SimpleDateFormat(pattern, locale);
            String after = sdf.format(today);
            System.out.println(locale.getDisplayCountry() + " | format: " + after);
        }
    }
}

Here are the variety of output produces when formatting a date in the same date pattern but varies in Locale

Date in default locale: 19-Oct-2021
Canada | format: 19-Oct.-2021
France | format: 19-oct.-2021
Germany | format: 19-Okt.-2021
United States | format: 19-Oct-2021
Japan | format: 19-10月-2021

How do I get default date and time format for a defined country?

The DateFormat class allows you to format dates and times with predefined styles in a locale-sensitive manner. Formatting dates or times with the DateFormat class is a two-step process.

First, you create a formatter with the getDateInstance() method for formatting date or getTimeInstance() method for formatting time or getDateTimeInstance() when you want formatting both date and time.

Second, you invoke the format method, which returns a String containing the formatted date. The following example formats today’s date and time by calling those two methods.

package org.kodejava.text;

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

public class LocaleDateTime {
    public static void main(String[] args) {
        Locale[] locales = {
                Locale.CANADA, Locale.FRANCE, Locale.GERMANY, Locale.US, 
                Locale.JAPAN
        };

        Date today = new Date();
        for (Locale locale : locales) {
            StringBuilder sb = new StringBuilder();
            sb.append(locale.getDisplayCountry()).append(System.lineSeparator());
            sb.append("------------------------").append(System.lineSeparator());

            // Gets a DateFormat instance for the specified locale
            // and format a date object by calling the format method.
            DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
            String date = df.format(today);
            sb.append("Default date format: ").append(date)
                    .append(System.lineSeparator());

            // Gets a DateFormat instance for the specified locale
            // and format a time information by calling the format method.
            DateFormat tf = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);
            String time = tf.format(today.getTime());
            sb.append("Default time format: ").append(time)
                    .append(System.lineSeparator());

            System.out.println(sb);
        }

        // Gets date and time formatted value for Italy locale using
        // To display a date and time in the same String, create the
        // formatter with the getDateTimeInstance method.
        // The first parameter is the date style, and the second is
        // the time style. The third parameter is the Locale
        DateFormat dtf = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,
                DateFormat.DEFAULT, Locale.ITALY);
        String datetime = dtf.format(today);

        System.out.println("date time format in " +
                Locale.ITALY.getDisplayCountry() + ": " + datetime);
    }
}

Here are the produces output:

Canada
------------------------
Default date format: Oct. 19, 2021
Default time format: 6:11:45 a.m.

France
------------------------
Default date format: 19 oct. 2021
Default time format: 06:11:45

Germany
------------------------
Default date format: 19.10.2021
Default time format: 06:11:45

United States
------------------------
Default date format: Oct 19, 2021
Default time format: 6:11:45 AM

Japan
------------------------
Default date format: 2021/10/19
Default time format: 6:11:45

date time format in Italy: 19 ott 2021, 06:11:45