Library using Nuiton I18n tutorial (using Maven)

Why a different tutorial for a library and a final application ?

When you create a final application, at build time, we create a bundle gathering all the strings of the application and its dependencies in order to optimise the application loading (we can save several seconds this way). When we create a library, no bundle is created, so the use and configuration of the library is easier.

The tutorial

In this tutorial, we will create a library with a unique class that will just display a message. That is this message that will be translated.

Create the Maven project and configure the Nuiton I18n plugin

We create a standard Maven project. In the pom.xml file, we will configure the Maven Nuiton I18n plugin.

<plugin>
    <groupId>org.nuiton.i18n</groupId>
    <artifactId>i18n-maven-plugin</artifactId>
    <version>4.1</version>
    <executions>
        <execution>
            <goals>
                <goal>parserJava</goal>
                <goal>gen</goal>
            </goals>
        </execution>
    </executions>
</plugin>

In the plugin configuration, we secify the goals to execute. The parserJava goal extract all the strings to translate from the java code. The gen goal get the resource file that contains the translated strings and add the extracted strings that are not already present in it. You can then translate the strings.

In order to work, the plugin needs to have the Nuiton I18n liibrary in the project dependencies.

<dependency>
    <groupId>org.nuiton.i18n</groupId>
    <artifactId>nuiton-i18n-api</artifactId>
    <version>4.1</version>
    <scope>compile</scope>
</dependency>

We make static import in our code, so we need to configure the maven-compiler-plugin

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <encoding>UTF-8</encoding>
        <showDeprecation>true</showDeprecation>
        <showWarnings>true</showWarnings>
    </configuration>
</plugin>

And in order to get the plugin and library Nuiton I18n, we have to add the nuiton repositories.

<repositories>

    <!-- depot des releases nuiton -->

    <repository>
        <id>nuiton.release</id>
        <name>NuitonReleaseRepository</name>
        <url>http://maven.nuiton.org/release</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
            <checksumPolicy>warn</checksumPolicy>
        </releases>
    </repository>

    <!-- depot des snapshots nuiton -->

    <repository>
        <id>nuiton.snapshot</id>
        <name>NuitonSnapshotRepository</name>
        <url>http://maven.nuiton.org/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
            <checksumPolicy>fail</checksumPolicy>
        </snapshots>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>

</repositories>

<pluginRepositories>

    <!-- depot des releases nuiton -->

    <pluginRepository>

        <id>nuiton.release</id>
        <name>NuitonReleaseRepository</name>
        <url>http://maven.nuiton.org/release</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
            <checksumPolicy>warn</checksumPolicy>
        </releases>
    </pluginRepository>

    <!-- depot des snapshots nuiton -->

    <pluginRepository>
        <id>nuiton.snapshot</id>
        <name>NuitonSnapshotRepository</name>
        <url>http://maven.nuiton.org/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
            <checksumPolicy>fail</checksumPolicy>
        </snapshots>
        <releases>
            <enabled>false</enabled>
        </releases>
    </pluginRepository>

</pluginRepositories>

Crate our library

Our library, to make it simple, will contain only one class and a static method.

package org.myOrganisation.myLibrary;

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

public class myLibrary {

    static public void myMethod(){
        System.out.println(_("My message to translate"));
    }
}

First build

When you build your library for the first time with the command mvn compile, you should see two new resource files containing each one a string to translate without translation.

You can now translate the string in both language, you will then have the translation available for your next builds.

Second build

If you build the library again, then integrate it in a final application using I18n, the message will be displayed translated.

Tutorial sources