简体   繁体   中英

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

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.
}

loading bundle from resources package:

ResourceBundle bundle=ResourceBundle.getBundle("/reports/bundles/bundle",Locale.CANADA_FRENCH);

filling report::

 params.put("REPORT_RESOURCE_BUNDLE", bundle);
JasperPrint jasPrint = JasperFillManager.fillReport(reportStream, params, data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM