How to create a multi language report?

2019-01-15 15:12发布

问题:

I want to create a document, which prints in more than one language, based on the Locale.

I have created 2 resource bundles, one in English and one in Chinese, but I am not sure how to use them.

回答1:

Here is the sample of how to implement internationalization support for JasperReports.

The main idea is to use special expression $R{} for localizing text and images.

The sample for images:

<image scaleImage="Clip"> 
    <reportElement positionType="Float" x="20" y="20" width="100" height="50"/> 
    <imageExpression class="java.lang.String"><![CDATA[$R{image.flag}]]></imageExpression> 
</image>

The samples for text (the $R{} syntax):

<textField isBlankWhenNull="true"> 
 <reportElement x="20" y="100" width="530" height="20"/> 
 <textElement/> 
 <textFieldExpression class="java.lang.String"><![CDATA[$R{sampleString}]]></textFieldExpression> 
</textField> 

or (the msg() method):

text.message=The program picked up {0} as a random number.
<textField isStretchWithOverflow="true" isBlankWhenNull="true"> 
 <reportElement x="20" y="210" width="530" height="20"/> 
 <textElement/> 
 <textFieldExpression class="java.lang.String"><![CDATA[msg($R{text.message}, $P{number})]]></textFieldExpression> 
</textField>


回答2:

Since the document generator may be part of your application, you should somewhere have a language selector menu-item, check-box or combo-box which is already preselected.

So, why don't you just add an if statement that reads the Locale, or the needed language before the report generation, and load the appropriate report accordingly to the locale.

This way you will need to keep one jrxml file for every language. It will be fairly easy to just translate the headers and labels manually.

Your data should be already translated in your database, where you have to keep the relevant attribute values multilingual anyway.

You will need to modify the SQL query for the appropriate language, but since the Query is part of your jrxml it will be executed automatically.