Nuiton I18n Hello World

This first tutorial is aimed to show you how to use Nuiton I18n library the easiest way. We will create a main method that will display th 'Hello World' text, then we will translate this text in other languages.

The corresponding java class looks like this :

import static org.nuiton.i18n.I18n.t;
import static org.nuiton.i18n.I18n.n;

class HelloWorld {

    public static void main (String[] args){
        System.out.println(_("Hello World"));
        System.out.println(n_("Goodbye World"));
    }
}

If you compile and launch this class (take care to put nuiton-i18n, commons-logging and commons-beanutils jar files in classpath or it will not work), you will get the following result :

Hello World
Goodbye World

Now we want to translate those strings. We will then create a resource file named hellWorld_fr_FR.properties (we will translate the application in french) that we will put in a i18n file and that will contain the translated strings (Take care, in the key, all the special characters and spaces have to be escaped by  ).

Hello\ World = Bonjour le monde
Goodbye\ World = Au revoir le monde

We compile again and test the class (put the directory that contains i18n in the classpath or Nuiton I18n will not find the properties file). Nothing has changed, but this is normal, the system has not been initialised so it does not know whiwh language to load. We will then modify our class adding the line

org.nuiton.i18n.I18n.init(null, Locale.FRANCE);

before the first call to Nuiton I18n library. This will tell Nuiton I18n which language to use (in this case french). We put null to the initializer, like that Nuiton I18n looks in the classpath for all the files i18n/?????fr_FR and loads them.

To test, you just need to compile and launch the application again, you should obtain :

2 avr. 2010 16:13:29 org.nuiton.i18n.init.I18nInitializer resolvBundles
INFO: 1 bundle(s) found, in 1 file(s).
2 avr. 2010 16:13:29 org.nuiton.i18n.I18nStore init
INFO: 1 bundle(s) found, [1 file(s)].
2 avr. 2010 16:13:29 org.nuiton.i18n.I18nStore addLanguage
INFO: I18nLanguage <locale: fr_FR, nbStences:2>, nbEntries: 1, nbSentences: 2.
Bonjour le monde
Goodbye World

This output can appear strange, but this is perfectly normal. The first lines inform you how initialisation is happening. Those are logs comming from Nuiton I18n. We can see the number of bundles found, in our case 1, and on the language setting, the number of strings found, in our case 2. Then comes our translated application. Yes, but the last line is not translated you can say. But if you take the class we wrote, the string "Goodbye World" is put in parameter of the n_() method (and not _()). The n_() method indicates a string that is translated elsewhere on the application, but must not be translated here. So it is replaced by the string not translated.

Congratulations, you translated your first application with Nuiton I18n

Tutorial sources

This tutorial sources can be downloaded here (zip) or here (tar.gz). They are packaged with the libraries needed for the compilation and execution of this example.