In Jasperreport, individual information can also be treated in an international way based on a non-pass locale. According to my arrangement, there should be the following three ways:
(1) Adopt Printwhenexpression Way : printwhenexpression attribute, refers to when printing, according to set expression (return only Boolean value) to determine, if true, Then the information is printed, and if False, the information is not printed directly. In this way to achieve (generally with statictext), the need for several international languages, the corresponding elements will appear several times, each element of the printwhenexpression according to the need to set, such as zh_cn==true this Simplified Chinese element display, Zh_ The CN is false to display that element in traditional Chinese, as follows:
viewing XML, the x, y, and width of two statictext, including other properties, are identical except for the printwhenexpression expression:
<statictext><reportelement x= "0" y= "0" width= "555" height= " uuid=" 3a479cfe-3b0b-42c3-91aa-4689e74e5e90 "><printwhenexpression><! [cdata[$P {report_locale}.getcountry (). Equals ("CN")]]></printwhenexpression></reportelement>< Textelement textalignment= "Center" verticalalignment= "Middle" ><font size= " " Pdffontname= "Stsong-light" pdfencoding= "Unigb-ucs2-h" ispdfembedded= "true"/></textelement> <text><! [cdata[This is implemented using Printwhenexpression]]></text></statictext><statictext><reportelement x= "0" y= "0" width= "555" height= "All" uuid= "3a479cfe-3b0b-42c3-91aa-4689e74e5e90" > <printwhenexpression><! [cdata[! $P {report_locale}.getcountry (). Equals ("CN")]]></printwhenexpression></reportelement> <textelement textalignment= "Center" verticalalignment= "Middle" ><font size= " " Pdffontname= "stsong-Light " pdfencoding=" Unigb-ucs2-h " ispdfembedded=" true "/></textelement><text><! [cdata[This is the use of printwhenexpression]]></text></statictext>
(2) using expression on the element , using TextField, set expression expressions such as: $P {report_locale}.getcountry (). Equals ("CN")? " This is controlled by expression: "This is controlled through expression." Of course, the premise is that language is Java, if it's groovy or something else that doesn't have the Equals method;
(3) The form of resource bundle is implemented : This method needs to set the properties of the resource bundle on the report, the property value is the name of the i18n file to be read, then the corresponding internationalized file needs to be added under Classpath. Then right-click the element, Edit expression, and set it to the corresponding key in the $r{properties file, as follows:
A.
B.classpath Add the corresponding internationalization file, the file name is the value of the previous step resource bundle + "_" +locale (such as ZH_CN,ZH_TW), as follows:
C.ireport Right-click on the element, edit expression, fill $r{my.detail} to complete.
Finally, a final export of the PDF, contains three, although the implementation of different ways, the results are consistent:
Attach the Jrxml file and Java:
<?xml version= "1.0" encoding= "UTF-8"? ><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=" i18n " pagewidth=" 595 " pageHeight=" 842 " columnwidth=" 555 " leftmargin=" " rightmargin=" " topmargin=" " bottommargin=" 20 " resourcebundle= "i18n" uuid= "49274885-3989-4457-a0db-36c6ac64ff1b" ><property name= " Ireport.zoom " value=" 1.0 "/><property name=" ireport.x " value=" 0 "/><property Name= "Ireport.y" value= "0"/><background><band splittype= "Stretch"/></background ><title><band height= " splittype=" "Stretch" ><statictext><reportelement x= "0" y= "0" width= "555" height= "" uuid= "3a479c"Fe-3b0b-42c3-91aa-4689e74e5e90 "><printwhenexpression><! [cdata[$P {report_locale}.getcountry (). Equals ("CN")]]></printwhenexpression></reportelement>< Textelement textalignment= "Center" verticalalignment= "Middle" ><font size= " " Pdffontname= "Stsong-light" pdfencoding= "Unigb-ucs2-h" ispdfembedded= "true"/></textelement> <text><! [cdata[This is implemented using Printwhenexpression]]></text></statictext><statictext><reportelement x= "0" y= "0" width= "555" height= "All" uuid= "3a479cfe-3b0b-42c3-91aa-4689e74e5e90" > <printwhenexpression><! [cdata[! $P {report_locale}.getcountry (). Equals ("CN")]]></printwhenexpression></reportelement> <textelement textalignment= "Center" verticalalignment= "Middle" ><font size= " " Pdffontname= "Stsong-light" pdfencoding= "Unigb-ucs2-h" ispdfembedded= "true"/></textelement> <text><! [CDAta[This is the use of printwhenexpression]]></text></statictext></band></title>< pageheader><band height= " splittype=" "Stretch"/></pageheader><columnheader> <band height= " splittype=" Stretch "><textfield><reportelement x=" 0 " y=" 0 " width= "555" height= " uuid=" b988adc9-7631-4230-b93e-213e90c2ed5f "/><textElement " Textalignment= "Center" verticalalignment= "Middle" ><font size= " pdffontname=" Stsong-light " pdfencoding=" Unigb-ucs2-h " ispdfembedded=" true "/></textelement>< textfieldexpression><! [cdata[$P {report_locale}.getcountry (). Equals ("CN")? " This is controlled by expression: "This is controlled by expression"]]></textfieldexpression></textfield></band> </columnheader><detail><band height= " splittype=" "Stretch" ><textField> <reportelement x= "0" y= " width=" 555 " height=" &nbs "P;uuid= "bcdebbfa-12f5-42cf-8bdd-404b27651d91"/><textelement textalignment= "Center" Verticalalignment= "Middle" ><font size= " pdffontname=" Stsong-light " pdfEncoding=" Unigb-ucs2-h " ispdfembedded=" true "/></textelement><textfieldexpression><! [Cdata[$R {my.detail}]]></textfieldexpression></textfield></band></detail>< columnfooter><band height= " splittype=" "Stretch"/></columnfooter><pagefooter> <band height= " splittype=" "Stretch"/></pagefooter><summary><band height= " splittype=" "Stretch"/></summary></jasperreport>
package com.zzq.test.myjasper;import java.io.file;import java.util.hashmap;import Java.util.locale;import java.util.map;import net.sf.jasperreports.engine.jremptydatasource;import net.sf.jasperreports.engine.JRParameter;import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.jasperfillmanager;import net.sf.jasperreports.engine.jasperprint; import net.sf.jasperreports.engine.export.jrpdfexporter;import Net.sf.jasperreports.engine.util.jrloader;import net.sf.jasperreports.export.simpleexporterinput;import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;public class I18N { private static final string name = system.getproperty (" Java.class.path "). Split ("; ") [0] + file.separator + "i18n"; /** * Compile jrxml into Jasper format and save in Classpath directory * @return * @throws Exception */ public static void compile () throws Exception { jaspercompilemanager.compilereporttofile (NAME + ". Jrxml"); } /** * Populate the compiled Jasper with data, regenerate a jrprint file * @throws Exception */ public static void filldata () throws Exception { map<string, object> params = new HashMap<String, Object> (); Params.put (Jrparameter.report_locale, locale.taiwan); // You cannot use Jasperfillmanager.fillrepor directly here.Ttofile (name + ". Jasper", params) call, need to pass a jremptydatasource, otherwise the exported file has the problem, the specific PDF blank, the Excel file has the problem jasperfillmanager.fillreporttofile (NAME + ". Jasper", Params, new jremptydatasource ()); } /** * export a report as pdf * @throws Exception */ public static void printpdf () throws exception { jasperprint jasperprint = ( Jasperprint) jrloader.loadobject (New file (name + ". Jrprint")); file destfile = new file (name + ". pdf"); jrpdfexporter exporter = new jrpdfexporter (); &Nbsp;exporter.setexporterinput (New simpleexporterinput (jasperprint)); exporter.setexporteroutput (New simpleoutputstreamexporteroutput (destFile)); exporter.exportreport (); } public static void main (String[] args) throws Exception { compile (); filldata (); printpdf (); }}
Internationalization of Jasperreport+ireport (i18n)