Internationalization in Java

Internationalization in Java is the process of designing an application that can be adapted to various local languages like Japanese, Chinese and etc to provide output in the same given language. Internationalization is also called as i18n in Java. This is because, between i and n there are 18 characters.

Problem

Let’s say, we have a software that is currently used by the customers who know the English language. Later our software is going to use by non-native English users from France or Germany. So, in this case, these users from France and Germany may not feel comfortable using it.

Solution

Now, we have the following solutions to fix this problem:

  1. To develop separate software for these new customer groups or
  2. A single software that can understand customer’s locality or region and displays the language based on the that

Though, we have to use Internationalization service from Java to implement both the options, the first option is not feasible. Because the number of user locality increases the number of new software required.

The second option is the only best solution to the problem.

Hence, Java has very good support for Internationalization because it supports Unicode representation.

Example of Internationalization in Java

Step 1: Setting Up the Project

First, let’s create a new Java project in your favorite IDE. Make sure you have the latest version of the JDK installed.

Step 2: Creating Resource Bundles

Resource bundles are key-value pairs that contain the localized strings for different languages. In our example, we will create two resource bundles: one for English and one for French.

Create a new package in your project called com.codersathi.resources. Inside this package, create two properties files: messages_en.properties and messages_fr.properties.

The messages_en.properties file should contain the following key-value pairs:

greeting=Hello!
farewell=Goodbye!

The messages_fr.properties file should contain the following key-value pairs:

greeting=Bonjour!
farewell=Au revoir!

Feel free to add more key-value pairs for a more comprehensive example.

I’ve used google translator to translate English to French content. If anything is wrong in translation nevermind. 😀

Step 3: Implementing Internationalization in Java

Now, let’s write the Java code to load and display the localized strings. Create a new class called InternationalizationDemo in your project.

example of internationalization in java
package com.codersathi;

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

public class InternationalizationDemo {
    public static void main(String[] args) {
        // Set the default locale to English
        Locale.setDefault(new Locale("en"));

        // Load the resource bundle for the current locale
        ResourceBundle bundle = ResourceBundle.getBundle("com.codersathi.resources.messages");

        // Display the localized strings
        System.out.println(bundle.getString("greeting"));
        System.out.println(bundle.getString("farewell"));
    }
}

In the above code, we set the default locale to English using the “Locale.setDefault()” method. This ensures that if a translation for the current locale is not available, the English strings will be used as a fallback.

We then load the resource bundle using the ResourceBundle.getBundle() method, passing the base name of the resource bundle (“com.codersathi.resources.messages”) as the argument. The “getBundle()” method automatically selects the appropriate resource bundle based on the default locale.

Finally, we use the “getString()” method of the resource bundle to retrieve and display the localized strings.

Step 4: Testing the Internationalized Application

Now that we have implemented internationalization in our Java application, let’s test it by changing the default locale.

In the “InternationalizationDemo” class, modify the line that sets the default locale to French:

Locale.setDefault(new Locale("fr"));

Save the changes and run the application. You should now see the French translations being displayed:

Bonjour!
Au revoir!

Conclusion

Internationalization is an essential aspect of software development, especially for applications that target a global audience. Java provides powerful tools and libraries to make internationalization easy. In this article, we learned how to implement internationalization in Java. By following these steps, we can ensure that our Java applications are ready to be localized for different languages and regions.


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments