|
GettextAntTasksManual
Explains how to build message templates and message bundles using Apache Ant.
Featured 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"> <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.
My .pot file contains translations but there are no translations in the java file being generated. What my be the cause?
@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).
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.
maybe i am missing some knowledge about i18n, but what about a task for msginit to create po files from pot-files?
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.
Where can we find (or ask for) help on the ant tasks ?