Introduction to Programming, Revisited

Many introductory programming courses start with a very simple first program, the traditional “hello world”. It is intended to teach how to edit, compile and run a program in a new programming environment using a simplified context, and it serves its purpose quite well. Unfortunately, the simple practice used in storing and displaying the text is quite contrary to a basic principle of software internationalization.

Here is the canonical hello program written in Java (you can find examples of such programs in just about every programming language):

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

This is not the way to do it in an international program. Of course, the “hello world” program is trivial, and few people would seriously localize it into several different languages, but we need to focus on the practice and not the program. If everyone who learns a new programming language also learns the wrong way to incorporate user-visible text in their programs, it becomes increasingly difficult later to learn how to do it right, and also to understand why to do it right.

The better way to write “hello world” in Java involves the use of a property file with a key-value pair. The name of the file is Messages.properties.

# Messages.properties contains the default user-visible text
hello=Hello, world!

The source code for the program becomes only a few lines longer:

import java.util.*;

public class HelloWorld {
    public static void main(String[] args) {
        ResourceBundle bundle = ResourceBundle.getBundle("Messages");
        String hello = bundle.getString("hello");
        System.out.println(hello);
    }
}

This version of the program uses the java.util.ResourceBundle class to load a property file called Messages.properties and to get a string using the key hello.

If you want to try this yourself, just save the source code into a file called HelloWorld.java and compile it using the following command in Terminal:

% javac HelloWorld.java

Then run the program:

% java HelloWorld

and you should see

Hello, world!

as expected.

It could be argued that, written this way, “hello world” becomes more complicated than it needs to be for a beginner, because it now involves system APIs and two source file. However, real working applications are much more complicated, and this is not an unreasonable amount of forward references to handle. The original Java example already contains special features of the language which are typically not explained, but must be taken as they are for the time being (such as a class definition, including a static method with a special meaning, and the System.out object).

The biggest advantage of doing it this way is that it is ready to accommodate the translation of the “Hello, world!” text into any language we want, and we don’t need to change the source code at all. The program doesn’t even have to be recompiled. Once we supply a new property file with the translation, the program selects the correct one automatically at runtime. If there is no translation for the user’s system language setting, the program falls back to the original property file.

Here is another property file, Messages_fr.properties, containing the French equivalent of “Hello, world!”:

# Messages_fr.properties contains the French translation
hello=Bonjour, tout le monde !

Note that the key is the same, hello, and it must be. Otherwise our bundle.getString call would not work.

You can temporarily force the Java runtime to use a different language than that indicated by your current system settings. For that, use the user.language key with the language code for French, fr, as follows:

% java -Duser.language=fr HelloWorld

When run this way, you should see the output:

Bonjour, tout le monde !

The Java runtime automatically searched for a property file with a name that matched the language code fr.

Incidentally, this example neatly sidesteps the issue of character encoding. For that and much more about software internationalization, see our services.