Java Reference
In-Depth Information
<textField>
<reportElement x="0" y="0" width="200" height="30" />
<textFieldExpression>
<![CDATA[$R{localization.text1}]]>
</textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>
The
resourceBundle
attribute of the
<jasperReport>
element tells JasperReports
where to get the localized strings to use for the report. In order for this attribute to
work correctly, a property file with a root name matching the value of the attribute
must exist anywhere in the CLASSPATH when filling the report. In this example,
a property file with the name
localizationdemo.properties
must exist in the
CLASSPATH when using the default locale. To use a different locale, the name of
the file must be
localizationdemo_[locale].properties
. For example, to use a
Spanish locale, the name would be
localizationdemo_es.properties
.
The following property file can be used with the preceding template to generate the
report using the default locale:
localization.text1=This is English text.
This, of course, assumes that the default locale uses the English language. In order
for JasperReports to pick it up as the resource bundle for the default locale, the file
must be saved as
localizationdemo.properties
.
To generate a report from the preceding template in Spanish,
localization_
es.properties
must look like this:
localization.text1=Este texto es en Español.
Notice how in both the property files the key �text before the equal sign) is the same.
This must be the case for each locale property file that we wish to use, since this key
is what JasperReports uses to obtain the localized text to display in the report. As can
be seen in the previous example, the syntax to obtain the value for
resourceBundle
properties is
$R{key}
.
To let JasperReports know what locale we wish to use, we need to assign a value to
a built-in parameter. This parameter's name is defined as a constant called
REPORT_
LOCALE
, and this constant is defined in the
net.sf.jasperreports.engine.
JRParameter
class. The constant's value must be an instance of
java.util.Locale
.
The following example demonstrates this procedure.