Sunday, January 3, 2016

Jasper Localization From Property File:Encoding Issue


Recently In a Project I used Jasper to generate PDF forms.While doing this task I encountered a problem with Localization of Jasper Fields from Property Files.

Normally we include below attribute in JRXML header to point to localization files.







Localization files will be in same folder where JRXML is placed and localization files will be in below format.
  • jasperreports_en.properties
  • jasperreports_th.properties
  • etc
And each file will have key value pair like

delivery.date=Delivery Date
delivery.print.date=Print Date

and it will be used in JRXML in below format


and Localization is passed to JRXML in below format
 parameterMap.put(JRParameter.REPORT_LOCALE, new Locale("th"));
Encoding Issue:

When I generated the PDF for Thai language the output was a bunch of junk values.



The problem is resource bundle reads the property files in ASCII encoding even though property file is encoded in UTF-8

Soultion:

Remove the resourcebundle attribute from JRXML and configure it in Java class with UTF-8 Encoding.


 
 try 
 {
  InputStream input = this.getClass().getClassLoader().getResourceAsStream("jasperreport_"+locale.getLanguage()+ ".properties");
  Reader reader = new InputStreamReader(input, "UTF-8");
  ResourceBundle resourceBundle = new PropertyResourceBundle(reader);
  parameters.put(JRParameter.REPORT_RESOURCE_BUNDLE,resourceBundle);
 } 
 catch (IOException  e1) 
 {
  LOG.error("Error locating localize file ",e1);
 } 

7 comments:

Green Grace said...
This comment has been removed by a blog administrator.
Lanco Hills said...

It's really a great and helpful piece of line.I am glad that you shared this helpful info with us.Please keep us informed like this.
Thanks for sharing.

Lanco Hills Hyderabad
sai keerthi prime Hyderabad
Ramky One Kosmos
Bella Vista Villas

geetha sri said...
This comment has been removed by a blog administrator.
Pranavi said...
This comment has been removed by a blog administrator.
Gayathri said...

Nice Blog... Thank you for sharing this information

Bharitya City Nikoo Homes Phase 2
DNR Casablanca
Godrej Air Hoodi
Tata New Haven
Goyal Orchid Whitefield
Casagrand Royce
Salarpuria Celesta
Shriram Southern Crest
L and T Raintree Boulevard
Prestige Park Square

Gayathri said...
This comment has been removed by the author.
Gayathri said...

Nice Blog... Thank you for sharing this information

Mahindra Lifespaces Ashvita
Incor One City
Kalpataru Residency
The Botanika
Lodha Bellezza
Marina Skies
Marina Skies Hyderabad

Post a Comment