Java i18n - Internationalization in Java With Examples

Internationalization in Java or Java i18n is a very important feature. Java provides internationalization (i18n) support through resource bundles.

Internationalization in Java

For making your application support different locales, we need to create locale specific properties file. The file names follow the pattern of bundle name with language code and country code, for example ApplicationMessages_en_US.properties.

Once the property files for specific locales are ready, all you need to do is initialize the resource bundle with correct Locale. Java provides two classes java.util.ResourceBundle and java.util.Locale that are used for this purpose. ResourceBundle reads the locale specific property file and you can get the locale specific value for any key.

This is very helpful in making your web application texts locale specific, you can get the locale information from the HTTP request and generate the dynamic page with that locale resource bundle files. You can also provide option to user to chose the locale and update the labels dynamically.

Java i18n Example

For java i18n example, I have created the project whose structure is like below image.

java-i18n-example

Here is the java code for JavaInternationalizationExample class.

Here bundle name is ApplicationMessages and I have 2 locale specific resource bundles and one default resource bundle.

ApplicationMessages.properties

ApplicationMessages_fr_FR.properties

ApplicationMessages_sv_SE.properties

Notice the use of Locale class, there are some locales already defined but we can always create new locale by passing language code and country code to it’s constructor.

When I run the above program, here is the output.

That’s all for quick java i18n example. Internationalization in java is very useful in web application to serve pages in locale specific language.

By admin

Leave a Reply