6. Localizing Reports
This chapter shows how to incorporate internationalization while using the report designer. This allows the report designer to specify labels in the report template that will be substituted during report generation with values from the language appropriate properties file.
Externalize Label Strings
The first step is to externalize the strings required for the report. This is done by creating a properties file with the value of each property being the text you want to appear in the report. The name of each property will be used in the report definition.
As with other internationalization efforts, a default set of properties can be created using the .properties extension. Then language specific properties files can override the defaults given the default local of the system generating the report.
In your favorite text editor, create a properties file named "MyReport.properties" and add the following name-value pairs:
report.title=Sample Product Inventory Report label.1=Product Code label.2=Product Name label.3=Quantity
Define the Resource Path
The report template needs to know where it will be able to find the properties files that will be used as a resource for localized strings.
In report designer, click on the Report element in the Structure pane.
In the Properties pane, specify the directory in which the MyReport.properties file was saved.
Add Resource Labels
The resource labels defined on the report will look in the resource files (in the report's resource path) when the report is generated. The resource key of the resource label is the property name for this lookup.
Drag a Resource Label to the Report Header section of the report and expand it to a suitable size for a report title.
In the properties pane for the newly created Resource Label, set the Resource Base to the name of the properties file (not the full filename, but the name portion of the filename ... this way the report generator can search for other files with the same base name depending on the default local of the system). In our example, specify the Resource Base of "MyReport".
Next, set the Resource Key to the name of the property in the properties file that should be used for substation. In our example, we use "report.title" for the report title.
Click on the Preview button, and you should see the value taken from the properties file displayed on the screen.
Repeat the process to add labels at the bottom of the Report Header section for label.1, label.2 and label.3
Again, click on the Preview button to see that the system found and substituted the proper labels.
Create Localized Resource Files
If the report needs to be translated to different languages, more resource files (properties files) need to be created - one per language.
Since the base properties file was created in English, we can create the English properties file by copying MyReport.properties to MyReport_en.properties (in the same directory).
If we need to translate the report to German locale for Switzerland, create the properties file (in the same directory) with the name MyReport_de_CH.properties and translate the property values to German. (NOTE: only the property values ... the property names need to be the same in all the properties files for the substitution to occur correctly).
NOTE: the resource files (properties files) are searched with the most specific locale first and then moving to the most generic. So with MyReport.properties, if your default locale is set to en_US, the application will look for resource files in the following order:
- MyReport_en_US.properties
- MyReport_en.properties
- MyReport.propertiesÂ