i18n gettext (.pot, .po language files)

1 Reference

-gettext home > https://www.gnu.org/software/gettext/

-gettext maven plugin > https://github.com/rlf/maven-gettext-plugin/

2 Introduction

the GNU `gettext' utilities are a set of tools that provides a framework to help other GNU packages produce multi-lingual messages. These tools include a set of conventions about how programs should be written to support message catalogs, a directory and file naming organization for the message catalogs themselves, a runtime library supporting the retrieval of translated messages, and a few stand-alone programs to massage in various ways the sets of translatable strings, or already translated strings.

3 Maven config

Let's configure our Maven Java project for using PO files for language translation, the relevant project structure is:

src/main/resources/po/
                      en.po
                      es.po
                      pl.po
                      keys.po
pom.xml

The maven-gettext-plugin goal "dist" is used for generating Java ResourceBundle classes from .po files.

The following example will generate a Messages_xx.class file for every .po language file available:

<plugins>
   <plugin>
        <!-- https://github.com/rlf/maven-gettext-plugin/ -->
        <groupId>com.github.rlf</groupId>
        <artifactId>gettext-maven-plugin</artifactId>
        <version>1.2.10</version>
     <executions>
     <execution>
      <goals>
       <goal>dist</goal>
      </goals>
      <configuration>
       <javaVersion>2</javaVersion>
       <outputDirectory>${project.build.directory}/classes</outputDirectory>
       <outputFormat>class</outputFormat>
       <poDirectory>${basedir}/src/main/resources/po</poDirectory>
       <sourceLocale>en</sourceLocale>
       <targetBundle>ude.cou.pera.gettext.Messages</targetBundle>
      </configuration>
     </execution>
    </executions>
   </plugin>
</plugins>

4 Sample content for the keys.pot and es.po files

keys.pot:

#: Hello.java:14
msgid "Hello, world!"
msgstr ""

#: Hello.java:18, java-format
msgid "This program is running as process number {0}."
msgid_plural "These programs are running as process number {0}."
msgstr[0] ""
msgstr[1] ""

es.po:

# Mensajes en español para GNU gettext.
# Copyright (C) 2014 Yoyodyne, Inc. (msgids)
#
# This file is distributed under the same license as the gettext package.
#
# Max de Mendizábal <max@upn.mx>, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004.
# Antonio Ceballos <aceballos@gmail.com>, 2014, 2015
#
msgid ""
msgstr ""
"Project-Id-Version: hello-java-0.19.4.73\n"
"Report-Msgid-Bugs-To: bug-gnu-gettext@gnu.org\n"
"PO-Revision-Date: 2015-06-23 22:44+0200\n"
"Last-Translator: Antonio Ceballos <aceballos@gmail.com>\n"
"Language-Team: Spanish <es@tp.org.es>\n"
"Language: es\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: Hello.java:14
msgid "Hello, world!"
msgstr "¡Hola, mundo!"

#: Hello.java:18, java-format
msgid "This program is running as process number {0}."
msgid_plural "These programs are running as process number {0}."
msgstr[0] "Este programa se ejecuta con el número de proceso {0}."
msgstr[1] "EStos programas se ejecutan con el número de proceso {0}."

5 Example using the generated ResourceBundle classes

private static final String RESOURCE_BUNDLE = "ude.cou.pera.gettext.Messages";

String langCode = "pl";
ResourceBundle resourceBundle = ResourceBundle.getBundle(RESOURCE_BUNDLE, new Locale(langCode));

LOGGER.info("Locale language : " + resourceBundle.getLocale().getLanguage());

LOGGER.info("Locale country : " + resourceBundle.getLocale().getCountry());

String resource = GettextResource.gettext(resourceBundle, "Hello, world!");

LOGGER.info("Translated is : " + resource);

6 Troubleshooting

Plugin "gettext-maven-plugin" does not execute in an Openshift environment.

No solution was found for this issue, alternative was moving to XLIFF file format (see another article in this site).