My favorites | Sign in
Project Logo
                
Search
for
Updated Dec 03, 2009 by bergerfx
Labels: Featured
GettextAntTasksManual  
Explains how to build message templates and message bundles using Apache Ant.

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" keywords="-k -ktrc -ktr -kmarktr -ktrn:1,2 -ktrl">
  <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 tobiaskringe, Nov 06, 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 bergerfx, Dec 03, 2009

Thanks, Tobias, I updated the wiki page accordingly.


Sign in to add a comment
Hosted by Google Code