Site Search:

Read and set the locale by using the Locale object

Back>

Internationalization is a process of placing strings in a property file and using classes like DateFormat so that the right format is used based on user preferences. Localization means actually supporting multiple locales. A locale is a specific geographical, political, or cultural region. Localization includes translating strings to different languages, outputting dates and numbers in the correct format for that locale. Internationalization and localization are often abbreviated as i18 and l10n.

Locale
Locale


The java.util.Locale class represents Locale. Your current locale can be found with:
Locale locale = Locale.getDefault();
System.out.println(locale);

A typical output would be en_US. It is {language}_{Country}. The underscore and country are optional. It is valid for a locale to be only a language, for example: en.

You can set Locale to a locale other then the default. There are 3 ways to create a new locale:


  1. Use Locale constants provided by Locale class: Locale.CANADA, Local.CANADA_FRENCH.
  2. Use the constructor: new Locale("fr"); new Locale("fr", "FRENCE");
  3. Use the builder design pattern:
Locale locale = new Locale.builder().setLanguage("en").setRegion("US").build();
the order doesn't matter:
Locale locale = new Locale.builder().setRegion("US").setLanguage("en").build();

Once you have a locale object, you can set the default locale as:
Locale.setDefault(locale);