Thursday, February 9, 2012

Ant Practice: Encoding Message Bundles by Ant

As we mentioned at JSTL Practice: I18N (Internationalization) practice, Locale, Bundle and Message.,
to create the message bundles for i18n, we have to run native2ascii in command line,
and may move the files as need or type very long path.

Now we know we can do these tasks by Ant (see Ant Introduction and First Try),
we will do this practice as below:

Write build file

For encoding message resource (from specific folder)
to message properties (and put to specific folder),
we have to write the build file as below:

<project name="genMessageBundles" default="encodeMessage" basedir=".">

    <!-- path property
        name denotes the property name
        location denotes the path
        the ${basedir} is the root of the project -->
    <property name="message.resource.path" location="${basedir}/src/test/jstl/i18n/resources"/>
    <property name="" location="${basedir}/src/test/jstl/i18n/properties"/>

    <!-- clear old files
        this will delete all *.properties
        under /src/test/jstl/i18n/properties -->
    <target name="clean">
        <delete dir="${}" includes="**/*.properties" />

    <!-- encoding msgs
        this will encode all *.resource
        under /src/test/jstl/i18n/resources 
        rename the encoded files to *.properties
        and put them to /src/test/jstl/i18n/properties
        encoding is UTF-8 -->
    <target name="encodeMessage" depends="clean">
        <echo message="begin"/>
            <native2ascii src="${message.resource.path}" encoding="UTF-8"
                includes="**/*.resource" dest="${}" ext=".properties" />
        <echo message="end"/>

Modify Resource Files

Then we modify the message resource files as below:

Run Ant Build

After we change the message resource files,
we can run Ant Build to encode them to message properties files.

Test the result

Then we can run the web project and see the new messages:

The full project is at github

File of this practice:

No comments:

Post a Comment