Resourcebundles mit Ant automatisch auf Konsistenz prüfen

2. February 2010

ResourceBundles are used in many common programming languages (, Adobe ,…) for internationalization of applications. You simply create one resourcebundle file per language your application will support. In order to keep these files consistent over time is not a trivial task.
This blog post is about how to integrate checking resourcebundle files into the build process.

Resourcebundles (also known as properties files) often lack the necessary consistency. Keys/Values are changed or new keys are added in one language (most likely the mother tongue of the developer), and the changes never end up being applied to all the other languages. Sometimes these inconsistencies are simply due to typos. Let’s look at an example:

myapp_de.properties: (German properties)

app.title=Meine Anwendung
app.welcomeDlg.title=Herzlich Willkommen
app.welcomeDlg.text=Hallo {0}!\nDu hast {1} neue Nachrichten (Gesamt: {2}).
app.welcomeDlg.disable.tooltip=Dialog nicht mehr anzeigen

myapp_en.properties (English properties):
app.title=My Application
app.welcome.title=Welcome
app.welcomeDlg.text=Hello {0}!\nYou have got {1} unread messages.

As you can see, I’ve packed some of the most common resourcebundle errors into these small files:

  • The key app.welcome.title in myapp_en.properties does not correspond to the key app.welcomeDlg.title in the German version of the properties file (myapp_de.properties).
  • The value of app.welcomeDlg.text is missing the third placeholder in the english version ({2}).
  • The key app.welcomeDlg.disable.tooltip is not present in the english version at all.

How can we find these errors automatically? There are lots of tools and plug-ins for development environments like Eclipse and Intelli-J IDEA helping the developers keep these files consistent (e.g., Eclipse ResourceBundle Editor). In most cases one would to have these consistency checks and their settings centralized, or even better: have them integrated into the build process and run them automatically with ever change of your source code.
If your build is based on ANT, you can use a library which can do all these checks for you. The library is called rscBundleCheck (by Peter Fichtner) and is hosted at sourceforge.org.
UPDATE: Peter Fichtner has released version 1.3.1 of rscBundleCheck – great documentation is available at http://rscbundlecheck.sourceforge.net/.

A minimal build script checking the resourcebundle files in the directory ‘locale/’ looks as follows:
build.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project name="rscBlog" basedir=".">   
   <taskdef name="resourceCheck"
      classname="org.dyndns.fichtner.rsccheck.ant.RscBundleCheckTask"
      classpath="rscbundlecheck.jar"/>
   <resourceCheck>
      <fileset dir="locale/">
         <include name="myapp*.properties"/>
      </fileset>
      <checks>
         <include name="duplicate key check"/>
         <include name="empty key check"/>
         <include name="cross bundle check"/>
         <include name="line end check"/>
         <include name="placeholder check"/>
         <include name="unicode check"/>
      </checks>
   </resourceCheck>
</project>

The section <checks> contains a list of checks which need to be performed. If you omit this section, all checks available in the library are performed.
The following checks are available:

  • allowed char key check
  • cross bundle check
  • duplicate key check
  • empty key check
  • empty value check
  • invalid char check
  • key regexp check
  • line end check
  • placeholder check
  • unicode check
  • unused key check
  • upper lower check
  • If we check our two properties files from above using this ANT script, we get the following result:

    C:\Catalysts\rscBlog>ant -f build.xml
    Buildfile: build.xml
    [resourceCheck] Including C:\Catalysts\rscBlog\locale\myapp_de.properties
    [resourceCheck] Including C:\Catalysts\rscBlog\locale\myapp_en.properties
    [resourceCheck] Enabled checks: [duplicate key check, empty key check, cross bundle check, line end check, placeholder check, unicode check]
    [resourceCheck] cross bundle check: Missing key(s) [app.welcomeDlg.disable.tooltip, app.welcomeDlg.title] (C:\Catalysts\rscBlog\locale\myapp_en.properties:null)
    [resourceCheck] cross bundle check: Missing key(s) [app.welcome.title] (C:\Catalysts\rscBlog\locale\myapp_de.properties:null)
    [resourceCheck] placeholder check: inconsistent placeholder for key app.welcomeDlg.text (expected [1, 0], actual [2, 1, 0]) (C:\Catalysts\rscBlog\locale\myapp_de.properties:5:Hallo {0}!\nDu hast {1} neue Nachrichten (Gesamt: {2}).)
    C:\Catalysts\rscBlog\build.xml:4: cross bundle check: Missing key(s) [app.welcomeDlg.disable.tooltip, app.welcomeDlg.title] (C:\Catalysts\rscBlog\locale\myapp_en.properties:null)
    BUILD FAILED
    Total time: 0 seconds

    Of course the build fails, since the resourcebundle files have some errors. The tool found all of them. Now we can go about integrating the checks into the build process.
    You can download the library as a JAR file fromhttp://sourceforge.net/projects/rscbundlecheck/, including the source.

    I’ld love to read your feedback/suggestions in the comments section.

    Tags: , ,

    Click to send this page to Twitter! Click to share this page on Facebook! 

    1. 12. February 2010 at 17:32 | #1

      Hi Dominik,
      vielen Dank für Deinen tollen Artikel. Ich befürchte, dass die Dokumentation auf http://rscbundlecheck.sourceforge.net/ nicht veraltet sondern eher zu neu war: Ich hatte noch Änderungen die ich noch nicht per jar releast hatte bereits beschrieben.
      Auch das Problem mit dem Classloader hatte ich vor einem Jahr gefixt, siehe auch http://rscbundlecheck.cvs.sourceforge.net/viewvc/rscbundlecheck/antTasks/src/org/dyndns/fichtner/rsccheck/ant/RscBundleCheckTask.java?view=diff&r1=text&tr1=1.33&r2=text&tr2=1.32&diff_format=h
      Ich habe den Task neu gebaut (u.a. ist damit das Classloader-Problem weg), er kann bei sourceforge heruntergeladen werden.
      Wenn Du Ideen, Wünsche oder Probleme hast einfach per Mail
      melden. Vor allem an Vorschlägen für eine möglichst gute Doku wäre ich interessiert.

      Viele Grüße
      Peter

    2. 12. February 2010 at 18:32 | #2

      @Peter Fichtner
      Hallo Peter,
      danke für die Rückmeldung. Ich werd mir bei Gelegenheit die neue Version ansehen und den Artikel aktualisieren.
      Beste Grüße
      Dominik