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
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.
Expand this group, find your . jrxml report and rightclick on it. In context menu choose 'Compile report'. Then you get compiled .
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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With