Java Language Locale


Example

The java.util.Locale class is used to represent a "geographical, political or cultural" region to localize a given text, number, date or operation to. A Locale object may thus contain a country, region, language, and also a variant of a language, for instance a dialect spoken in a certain region of a country, or spoken in a different country than the country from which the language originates.

The Locale instance is handed to components that need to localize their actions, whether it is converting the input, output, or just need it for internal operations. The Locale class cannot do any internationalization or localization by itself

Language

The language must be an ISO 639 2 or 3 character language code, or a registered language subtag of up to 8 characters. In case a language has both a 2 and 3 character language code, use the 2 character code. A full list of language codes can be found in the IANA Language Subtag Registry.

Language codes are case insensitive, but the Locale class always use lowercase versions of the language codes

Creating a Locale

Creating a java.util.Locale instance can be done in four different ways:

Locale constants
Locale constructors
Locale.Builder class
Locale.forLanguageTag factory method 

Java ResourceBundle

You create a ResourceBundle instance like this:

Locale locale = new Locale("en", "US");
ResourceBundle labels = ResourceBundle.getBundle("i18n.properties");
System.out.println(labels.getString("message"));

Consider I have a property file i18n.properties:

message=This is locale

Output:

This is locale

Setting Locale

If you want to reproduce the state using other languages, you can use setDefault() method. Its usage:

 setDefault(Locale.JAPANESE); //Set Japanese