IntroductionAdd 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. IncludeThe 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 targetsNow write targets that make use of the gettext tasks provided by the library. Message extractionThis 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 mergingThis 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 bundleThis 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 bundlesThis 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>
|
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.
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.
Thanks, Tobias, I updated the wiki page accordingly.