My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GettextAntTasksManual  
Explains how to build message templates and message bundles using Apache Ant.
Featured
Updated May 1, 2010 by berge...@gmail.com

Introduction

Add and edit the following lines to your Ant build.xml file.

Include and Edit

<property name="gettexttasks.jar" value="lib/gettext-ant-tasks-0.9.3.jar"/>

The line above defines the location of the gettext-ant-tasks Jar file. The value should be modified to denote the Jar's location relative to the build.xml.

Include

The lines below can be copied verbatim. They load the Ant gettext tasks from the Jar.

<target name="init.gettext" description="Loads the Ant gettext tasks">
  <taskdef name="gettext-extract" classname="org.xnap.commons.ant.gettext.GettextExtractKeysTask" classpath="${gettexttasks.jar}"/>
  <taskdef name="gettext-merge" classname="org.xnap.commons.ant.gettext.GettextMergeKeysTask" classpath="${gettexttasks.jar}"/>
  <taskdef name="gettext-generate-default" classname="org.xnap.commons.ant.gettext.GenerateDefaultBundleTask" classpath="${gettexttasks.jar}"/>
  <taskdef name="gettext-dist" classname="org.xnap.commons.ant.gettext.GettextDistTask" classpath="${gettexttasks.jar}"/>
</target>

Write custom targets

Now write targets that make use of the gettext tasks provided by the library.

Message extraction

This target extracts all the keys from the Java source files and stores them in a so called .pot file which can be given to translators or which is merged with existing .po files.

<target name="extract-messages" description="Extracts message keys from the source code" depends="init.gettext">

<gettext-extract keysFile="messages.pot" poDirectory="po">
  <fileset dir="src/main/java" includes="**/*.java"/>
  <fileset dir="src2/main/java" includes="**/*.java"/>
</gettext-extract>

</target>

Message merging

This target merges the newly extracted keys into the existing po translation files.

<target name="merge-messages" description="Merges newly extracted messages into existing po files" depends="init.gettext">
  <gettext-merge keysFile="messages.pot" poDirectory="po"/>
</target>

Generating a default bundle

This target generates a default message bundle where the translation equals the message to be translated.

<target name="generate-default-bundle" description="Generates a default bundle" depends="init.gettext">
  <gettext-generate-default targetBundle="org.mynamespace.i18n.Messages" outputDirectory="po" potfile="po/messages.pot"/>
</target>

Generate bundles

This target generates the Java ResourceBundles from the translation po files only taking into account languages that have been translated more than 65%. The generated class files are then packed into a Jar file.

<target name="generate-bundles-jar" description="Generates Java ResourceBundles and jars them up" depends="init.gettext">
  <gettext-dist targetBundle="org.mynamespace.i18n.Messages" poDirectory="po" outputDirectory="po" percentage="65"/>
  <jar destfile="lib/messages.jar" basedir="po"
    includes="org/**"/>
</target>
Comment by tobiaskr...@gmail.com, Nov 6, 2008

I had to add <taskdef name="gettext-generate-default" classname="org.xnap.commons.ant.gettext.GenerateDefaultBundleTask" classpath="${gettexttasks.jar}"/> to the includes for the ant-tasks to work.

Comment by wow...@users.sourceforge.net, Nov 18, 2009

The task 'gettext-dist' derives the locale from the part of the filename before the last dot. For instance, if you have a file named 'po/nl/messages.po', the locale will be 'messages'. So, something like 'po/nl.po' is a better name.

Comment by project member berge...@gmail.com, Dec 3, 2009

Thanks, Tobias, I updated the wiki page accordingly.

Comment by meedi...@gmail.com, Mar 17, 2010

My .pot file contains translations but there are no translations in the java file being generated. What my be the cause?

Comment by tobiaskr...@gmail.com, Mar 24, 2010

@meediake: Do you mean .po file? Those are the target language specific files, which contain the translation. One (trivial) cause could be that incomplete translations from po files are not added to the target bundles by default (specified by the 'percentage="65"' parameter).

Comment by tobiaskr...@gmail.com, Mar 24, 2010

A general note: The parameters given here are ant-task version 0.9.3 specific. Especially the 'keywords="-k -ktrc -ktr -kmarktr -ktrn:1,2 -ktrl"' parameter does not work with the new way translation context is handled since version 0.9.6. You may leave this parameter out to use the tasks default settings, which should work fine.

Comment by michael....@gmail.com, Apr 16, 2010

maybe i am missing some knowledge about i18n, but what about a task for msginit to create po files from pot-files?

Comment by project member berge...@gmail.com, May 1, 2010

Good point about the keywords not being up-to-date. I removed them from the example, since they don't have to be changed if the ant tasks are used in conjunction with the gettext commons library.

Comment by francois...@atosorigin.com, Nov 8, 2010

Where can we find (or ask for) help on the ant tasks ?


Sign in to add a comment
Powered by Google Project Hosting