How to make a Multilingual Java Application

This post explains how to make a multilingual Java application.

Introduction to Property files in Java

As language changes depending on each computer and the region, it is considered locale specific.

Locale-specific information in Java is stored in .property files as key/value pairs.

Java Property files are text files with a .properties file extension. Each row within the Property file contains a key/value pairs. The key is written first, then the character = or :, followed by the value. Comments in .property files can be added by starting the row with characters # or !.

Example of Property file:

#this is a comment for a Java property file
key=value
!this is also a comment for a Java property file
key2:value2

Properties are grouped as families of properties. Each family of properties is composed of many properties that offer the same information in different locales. All resource bundles within the same family shared a common name, for example, “MyResources”.

The default Property filename is the one whose name is the family name plus extension .properties. An example of default property filename is “MyResources.properties”. This file is the reference in case the locale is not present in any other Property file within the family.

Other .properties files are created by adding the language and country. You need to add to the family name the ISO 2-letter language code in lowercase and ISO 2-letter country code in uppercase, linked with underscores. For example, if you want to create the .properties file for the family MyResources for the Mexican variant of Spanish, you must create the file “MyResources_es_MX.properties”.

You can create a .properties file only for a language, without specifying the country. Just add the language code, omitting the country code. For example, if you want to create the properties file for Spanish language, Property file shoud be called “MyResources_es.properties”.

Properties files are accessed from Java program using an instance of class ResourceBundle.

There is an example of this in the detailed description.

Steps to make a Java application Multilingual

This section describes the steps to make a Java application multilingual.

In this example, we are doing a multilingual Hello World application.

1. Create the default Property File for the Family of Resource Bundles

Decide a name for the family of resource bundles. It will be MyResources in this example.

Create a property file with the same name as the family or resource bundles and the .properties extension. In this example, the filename would be “MyResources.properties”.

In case the user locale is not found on any .properties file, this file will be accessed as default.

MyResource.properties:

HelloWorld = Hello, World!
GoodbyeWorld = Goodbye, World!

2. Create the other Locales for the Family of Resource Bundles

Create the other files for different locales

MyResource_en_ES.properties:

HelloWorld = Hello, World!
GoodbyeWorld = Goodbye, World!

MyResource_es_EN.properties:

HelloWorld = ¡Hola, Mundo!
GoodbyeWorld = ¡Adiós, Mundo!

3. Write main program

Create the main program. In this example, the main class is called “MultilangHelloWorld”.

IMPORTANT: code assumes that the Properties files are located in a package called “mypackage”. You need to change the path if the package is different. Remember that if there is a package inside a package, the separator for the path is a dot (.).

MultilangHelloWorld.java

package multilanghelloworld;

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

public class MultilangHelloWorld {
    public static void main(String[] args) {
        //get system locale
        Locale myLocale = Locale.getDefault();
        System.out.println("Current locale: " + myLocale);
        System.out.println("Current country: " + myLocale.getCountry());
        System.out.println("Current language: " + myLocale.getLanguage());

        //get resource from default bundle (en, ES)
        ResourceBundle myResources = ResourceBundle.getBundle("mypackage.MyResources", myLocale);
        String helloWorld = myResources.getString("HelloWorld");
        System.out.println(helloWorld);
        
        //change local to Spanish
        Locale.setDefault(new Locale("es", "ES"));
        Locale newLocale = Locale.getDefault();
        myResources = ResourceBundle.getBundle("mypackage.MyResources", newLocale);
        
        //get resource from specified bundle (es, ES)
        String goodbyeWorld = myResources.getString("GoodbyeWorld");
        System.out.println(goodbyeWorld);
    }
}

4. Run Program

The system local in my test computer was en_ES

If this is the case, output will be:

Current locale: en_ES
Current country: ES
Current language: en
Hello, World!
¡Adiós, Mundo!

You might also be interested in…

External References

Leave a Reply

Your email address will not be published. Required fields are marked *