JasperReports ResourceBundle

In this tutorial, we create a report in multiple languages in JasperReports library. The sources for the example can be found at the author's Github repository.

JasperReports is an open-source reporting library. It can create reports in various formats including PDF, HTML, XLS, or CSV. JasperReports creates page-oriented, ready-to-print documents.

A resource bundle is a Java properties file that contains locale-specific data. It is a way of internationalizing Java applications by making the code locale-independent.

Localized report in JasperReports

The following application creates a report that can be generated in three different languages.

.
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── zetcode
        │           └── main
        │               ├── CommandLineRunner.java
        │               └── JasperResourceBundle.java
        └── resources
            ├── flags
            │   ├── england.png
            │   ├── russia.png
            │   └── slovakia.png
            ├── in18
            │   ├── labels.properties
            │   ├── labels_ru.properties
            │   └── labels_sk.properties
            └── report2.xml

This is the project structure. Country flags are located in the src/main/resources/flags/ directory and labels in src/main/resources/in18/ directory.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zetcode</groupId>
    <artifactId>JasperResourceBundle</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <dependencies>
        
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.4.3</version>
        </dependency>
        
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-fonts</artifactId>
            <version>6.0.0</version>
        </dependency>
        
    </dependencies>             
</project>

The Maven pom.xml file contains the jasperreports and jasperreports-fonts dependencies.

labels.properties
message=Tea report
flag=flags/england.png

These are English properties.

labels_ru.properties
message=Чайный oтчет
flag=flags/russia.png

These are Russian properties.

labels_sk.properties
message=Čajový report
flag=flags/slovakia.png

These are Slovak properties.

report2.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
   "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport xmlns = "http://jasperreports.sourceforge.net/jasperreports"
              xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation = "http://jasperreports.sourceforge.net/jasperreports
                                    http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"            
              name="report2" pageWidth="595" pageHeight="842" 
              columnWidth="555" leftMargin="20" rightMargin="20"
              topMargin="20" bottomMargin="20">
    
    <style name="title" fontSize="18" fontName="DejaVu Sans" hAlign="Center" />   
      
    <background>
        <band height="802">
            <staticText>
                <reportElement mode="Opaque" x="0" y="0" width="555" 
                               height="802" backcolor="#cccccc"/>
                <textElement/>
                <text><![CDATA[]]></text>
            </staticText>
        </band>
    </background>        
    
    <title>
        <band height="200">

            <textField>
                <reportElement x="0" y="10" width="595" height="25" style="title" />
                <textElement />
                
                <textFieldExpression class = "java.lang.String">
                    <![CDATA[$R{message}]]>
                </textFieldExpression>
            </textField>     
            
            <image>
                <reportElement x="15" y="50" width="150" height="100" />
                <imageExpression><![CDATA[$R{flag}]]></imageExpression>
            </image>            
            
        </band>
    </title>

</jasperReport>

This is the report template file. It displays a localized text and image.

<background>
    <band height="802">
        <staticText>
            <reportElement mode="Opaque" x="0" y="0" width="555" 
                            height="802" backcolor="#cccccc"/>
            <textElement/>
            <text><![CDATA[]]></text>
        </staticText>
    </band>
</background>  

Some flags have a white stripe; therefore, we change the background colour of the report using the backcolor tag.

<textFieldExpression class = "java.lang.String">
    <![CDATA[$R{message}]]>
</textFieldExpression>

The message of the report is retrieved from the properties file with the $R{} syntax.

<image>
    <reportElement x="15" y="50" width="150" height="100" />
    <imageExpression><![CDATA[$R{flag}]]></imageExpression>
</image>  

Similarly, we get the path to the flag with the $R{} syntax. The path is passed to the imageExpression tag.

JasperResourceBundle.java
package com.zetcode.main;

import java.util.HashMap;
import java.util.Locale;
import java.util.ResourceBundle;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

public class JasperResourceBundle {

    public void start() throws JRException {
        
        String xmlFile = "src/main/resources/report2.xml";
        JasperReport jreport = JasperCompileManager.compileReport(xmlFile);

        ResourceBundle bundle = ResourceBundle.getBundle("in18/labels",
                new Locale("ru", "RU"));
        
        HashMap params = new HashMap();
        params.put("REPORT_RESOURCE_BUNDLE", bundle);

        JasperPrint jprint = JasperFillManager.fillReport(jreport, params,
                new JREmptyDataSource());

        JasperExportManager.exportReportToPdfFile(jprint,
                "src/main/resources/report2.pdf");
    }
}

The JasperResourceBundle creates a JasperPrint file from the data source and the report2.xml template. JasperPrint represents a page-oriented document that can be viewed, printed, or exported to other formats.

String xmlFile = "src/main/resources/report2.xml";
JasperReport jreport = JasperCompileManager.compileReport(xmlFile);

The XML template file is compiled into a JasperReport. JasperReport is a compiled template ready to be filled with data.

ResourceBundle bundle = ResourceBundle.getBundle("in18/labels",
        new Locale("ru", "RU"));

A ResourceBundle is created. Is specifies the location of the properties files and the current locale used.

HashMap params = new HashMap();
params.put("REPORT_RESOURCE_BUNDLE", bundle);

The bundle is passed as a REPORT_RESOURCE_BUNDLE parameter to the report.

JasperPrint jprint = JasperFillManager.fillReport(jreport, params,
        new JREmptyDataSource());

A JasperPrint object is created; an object that can be viewed, printed, or exported to other formats. We pass an empty data source and parameters to the JasperFillManager.fillReport() method.

JasperExportManager.exportReportToPdfFile(jprint,
        "src/main/resources/report2.pdf");   

The JasperExportManager.exportReportToPdfFile() method exports the JasperPrint into a PDF file.

CommandLineRunner.java
package com.zetcode.main;

public class CommandLineRunner {

    public static void main(String[] args) throws Exception {

        JasperResourceBundle app = new JasperResourceBundle();
        app.start();
    }
}

The CommandLineRunner sets up the application.

In this tutorial, we have generated a multi-language report in JasperReports. You might be interested in these related tutorials: JasperReports multiple data sources, JasperReports filtering data, JasperReports scriptlets, Creating a report with JasperReports API, Creating a report from CSV with JasperReports, and Java tutorial.