Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load jasperreports resourcebundle at runtime?

Is it possible with jasper Reports to load a ResourceBundle (for i18n) at runtime?

I want to create a report from a jrxml file (for example c:\reports\report.jrxml)
with my labels in the properties file located at (c:\messages\report.properties).

I only found example where the property file is in the classloader.

Thanks

like image 239
cremersstijn Avatar asked Mar 11 '13 12:03

cremersstijn


People also ask

Where do I put JasperReports properties?

jasper. properties is looked up by jasper reports in the classpath, so it can be directly in the WEB-INF/classes folder, or in the root folder of any of the jars in WEB-INF/lib.

How do I compile a Jrxml file?

Expand this group, find your . jrxml report and rightclick on it. In context menu choose 'Compile report'. Then you get compiled .


1 Answers

John Ferguson's blog mentions that the trick is to override the REPORT_RESOURCE_BUNDLE parameter with a custom ResourceBundle instance.

// Compiling the report is not a necessary step; prefer using .jasper files
// that have been pre-compiled to avoid this compilation step.
//
JasperDesign jasperDesign = JasperManager.loadXmlDesign("Report.jrxml");
JasperReport jasperReport = JasperManager.compileReport(jasperDesign);

Map parameters = new HashMap();
parameters.put("REPORT_LOCALE",LocaleManager.currentLocale());
parameters.put("REPORT_RESOURCE_BUNDLE",resourceBundle);
Connection conn = DBConnectionFactory.getConnection();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
                                                       parameters,
                                                       conn);

The resourceBundle can come from anywhere. For example:

try(FileInputStream fis = new FileInputStream("/tmp/report.properties")) {
  ResourceBundle resourceBundle = new PropertyResourceBundle(fis);

  // Pass resourceBundle into the report, as shown above.
}
like image 163
cremersstijn Avatar answered Oct 20 '22 03:10

cremersstijn