日本語版

FreemMarker meets JavaDoc

31st Dec.'06

Unfortunately, the current version of JavaDoc does not support i18n(internationalization). Here shows that there is a Doclet for i18n but it is not generally available and the procedure seems a bit difficult. Here shows an idea to use FreeMarker to generate JavaDoc documents.

FreeMarker is a template engine. This can be used as a pre-processor for Java source code also. In this manner, you can place JavaDoc comments in more than one language into your single source code:

/**
<#if locale="ja">
サイズ0配列。
<#else>
Empty arrays.
</#if>
 */
public class EmptyArrays {
    EmptyArrays() {}

<#...> represents the directive of FreeMarker. One of the important things is that you place all of the FreeMarker directives in the JavaDoc comment so that the source code is still able to be compiled by javac without FreeMarker processing. This also ensures that line numbers in compilation errors are correctly point the error lines. Use appropriate encoding to write your source code such as UTF-8 so that JavaDoc comment in more than one language can sit in single source file. When you compile your source code by javac, make sure to specify -encoding option. Otherwise, javac will complain if the default encoding is different than one of your environment.

You can generate JavaDoc by an Ant task. First of all, download FMPP from here and unpack the archive into appropriate directory. Once you get the fmpp.jar file, you can run the JavaDoc by the following Ant task:

  <property name="fmpp.lib" path="/usr/local/java/freemarker/fmpp_0.9.11/lib/fmpp.jar"/>
  <taskdef name="fmpp" classname="fmpp.tools.AntTask" classpath="${fmpp.lib}"/>

  <target name="javadoc" description="Generate javadoc.">
    <macrodef name="javadoc-i18n">
      <attribute name="locale"/>
      <sequential>
        <mkdir dir="javadocsrc-@{locale}"/>
        <fmpp sourceroot="src" outputroot="javadocsrc-@{locale}"
              data="locale:@{locale}" logfile="log-@{locale}.fmpp"/>   
        <javadoc sourcepath="javadocsrc-@{locale}" destdir="javadoc-@{locale}"
                 windowtitle="Ruimo util" Locale="@{locale}" Encoding="UTF-8"
                 packagenames="com.ruimo.util.*" charset="UTF-8">
          <link href="http://java.sun.com/javase/6/docs/api"/>
        </javadoc>
      </sequential>
    </macrodef>

    <javadoc-i18n locale="ja"/>
    <javadoc-i18n locale="en"/>
  </target>

You will get the Japanese and English version of JavaDoc in the directory javadoc-ja and javadoc-en. Of courese, you can easily add other languages as many as you wish.


Valid XHTML 1.0 Strict

 Any comment will be appreciated.

 Back to top page.