Tutorial

Java i18n - Internationalization in Java

Published on August 3, 2022
Default avatar

By Pankaj

Java i18n - Internationalization in Java

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

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, Internationalization in Java Here is the java code for JavaInternationalizationExample class.

package com.journaldev.i18n;

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

public class JavaInternationalizationExample {

    public static void main(String[] args) {
        //default locale
        ResourceBundle bundle = ResourceBundle.getBundle("ApplicationMessages");
        //Get ResourceBundle with Locale that are already defined
        ResourceBundle bundleFR = ResourceBundle.getBundle("ApplicationMessages", Locale.FRANCE);
        //Get resource bundle when Locale needs to be created
        ResourceBundle bundleSWE = ResourceBundle.getBundle("ApplicationMessages", new Locale("sv", "SE"));
        
        //lets print some messages
        printMessages(bundle);
        printMessages(bundleFR);
        printMessages(bundleSWE);
        
    }

    private static void printMessages(ResourceBundle bundle) {
        System.out.println(bundle.getString("CountryName"));
        System.out.println(bundle.getString("CurrencyCode"));
    }

}

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

CountryName=USA
CurrencyCode=USD

ApplicationMessages_fr_FR.properties

CountryName=France
CurrencyCode=Euro

ApplicationMessages_sv_SE.properties

CountryName=Sweden
CurrencyCode=Kr

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.

USA
USD
France
Euro
Sweden
Kr

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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
March 23, 2017

Amazing! Easy to follow. Great article. thanks

- Kholofelo Maloma

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel