Recent posts

Using Properties Files to Localize BIRT Reports

I’ve received a few questions asking how to localize BIRT reports. BIRT reports can be localized so that your users in offices around the world can see a report in their desired language and formating. BIRT can automatically take care of the number, date, and currency formatting but there are other parts of the report to consider that would normally consist of static text. These ncluding labels or text elements within a report, table and column headers, chart titles, etc. For these elements, we need to replace the static text with external text from a properties file.

To get started, I created a couple of .properties file. You will need one property file for each locale you plan to support.  For our example, I created one called MyResources_us_EN.properties and MyResources_fr.properties.  These are just text files and contain name value pairs like below.

welcome=Hello

goodbye=Later

…and…

welcome=Bonjour

goodbye=Au revoir

Once you have the properties files created, add them to your report project.  You can now select one of the properties files in the Resource File property of the report design (screenshot below).

Set Resource File

Now that the properties file has been set, you can assign ‘keys’ to various controls within the report.  To add a key to a label, select the label and then Localization on the Property Editor tab.

Assign resource key

Press the […] button and you can pick the key to use from a list

Pick resource key

Perform these same steps for any other labels within the report.  You can use external text within the chart as well, anywhere you see the following icon.

External Text

Pressing this button will invoke a list like below where you can pick the resource key to use.

Select external text

If you need to externalize text that comes from a database, then you will need to use the mapping feature.  For example, if I want to localize the text from a group header and I want to replace the office number (1 through 7) with the words “First Office” for English and “Premier Bureau” for French, then I start by selecting the Office Number data field.  On the Map tab in the Property Editor, press the Add button to create a map rule.

BIRT Create Map Rule

In this example, when the Office Code field equals Office # 1, then replace the text with the value from our properties file that matches ‘1′.  Create a unique mapping for each value you want to externalize.

Once you have created all your mappings and have assigned the keys to each label or chart, then you are ready to test that it all works.  In order for the resource files to get selected correctly at runtime, we have to remove the locale specific information from the name of our resources file.  You do this by selecting the whitespace on the report again, and then on the Resources tab.  Remove ‘_us_EN’ from the name of the resource file so that it now reads ‘MyResources’ without any locale information at the end.

To test for different languages while in the BIRT Designer, select Window | Preferences | Report Design | Preview.  I’ll select French since that was the only other locale that I have a properties file for.

BIRT Preview Language Selection

Now when I run the report, my labels are replaced, the chart title is replaced, and the group header fields have been replaced.

BIRT Report Preview

If you are trying to set the name of the properties file or assign the resource keys with scripting, then you can do something like below in the beforeFactory event:

var rr = reportContext.getReportRunnable();

rr.designHandle.setProperty(”includeResource”, “MyResources”);

rr.getDesignInstance().getLabel(”GoodbyeLabel”).textKey = “goodbye”;

… or in the onPrepare event of a label control, you can use:

this.textKey = “welcome”;

If you are using the REAPI to set the location of the resource file… or assign keys, then you can use code similar to:

config.setResourcePath(”C:/work/eclipse/BIRT_221/resources”);

task.setLocale(ULocale.FRENCH);

IReportRunnable design = null;

ReportDesignHandle dh = null;

design = engine.openReportDesign(”C:/workspace_221/birt/Localization.rptdesign”);

dh = (ReportDesignHandle) design.getDesignHandle();

LabelHandle mylabel = (LabelHandle) dh.findElement(”LastLabel”);

mylabel.setTextKey(”welcome”);

All of these examples above can be downloaded from:

http://www.birt-exchange.com/modules/wfdownloads/singlefile.php?cid=2&lid=351

4 Comments
Comment by scottr:

Virgil,

Nice article.

You may want to mention the use of reportContext.getMessage to translate labels into the appropriate resource bundle. This is particularly useful when writing any scripts.

reportContext.getMessage( resourceLabel );\
reportContext.getMessage( resourceLabel, locale );
reportContext.getMessage( resourceLabel, Object []);

When scripting this will allow you to use resource labels in your script and the REAPI will look up the appropriate value from the correct bundle

You can also get the current locale through
reportContext.getLocale()

sr

Comment by dstoian:

It looks like the .properties files are only working if they are ANSI encoded. I read somewhere that if one needs to use unicode, then one needs to run his/her unicode file through a program called “native2ascii”. It also looks like the charts are not so friendly when it comes to .properties files with unicode… There is no mention about localizing the labels for parameter prompts: does it work along the same lines?

Comment by dstoian:

I tried localizing the labels for parameter prompts the same way, and it works.

Comment by dstoian:

See here for more details on how to internationalize your BIRT reports:

http://www.birt-exchange.org/wiki/BIRT:Internationalization/

Leave a comment

Sorry, you must log in to post a comment.


vdodson
vdodson
Virgil Dodson is a Developer Evangelist at Actuate Corporation. Virgil has over 13 years experience as a software developer. For the past 6 years he has helped Java developers get started with Actuate's embedded reporting products. He holds a Bachelor of Science degree in Computer Information Systems from DeVry.
more...