Jump to content
We've recently updated our Privacy Statement, available here ×
  • How to change report locale on fly


    prusyn
    • Version: v7 Product: JasperReports® Server

    Usecase:

    [toc on_off::hide=1]

    I would like to control report locale picked by JasperReports Server using parameters of the report, not the user session locale.

    Is this achievable at all?


    Resolution:

    At this moment, based on locale used by user in JasperReports Server, the report will be executed using different bundle file. Take a look in this screencast: http://somup.com/cq1hIdnJQl

    If you want to run report with french locale, user should be logged in with french locale as REPORT_LOCALE parameter is defined during report execution and it cannot be overwritten. There is a way to do something similar, but the change has to be made on report level.

    The idea is to overwrite default REPORT_LOCALE parameter of subreport. By default you cannot overwrite this parameter for main report because it takes its value from the system. So, steps are next:

    1. Create an empty report;
    2. Create parameter Language
    3. Create its subreport and define its parameter REPORT_LOCALE like below:
      <subreport> 
         <reportElement x="0" y="0" width="560" height="560" 
                        uuid="16df228b-dfbb-4725-81dd-41ebc45a50a7"/> 
         <subreportParameter name="REPORT_LOCALE"> 
            <subreportParameterExpression>
               <![CDATA[new java.util.Locale($P{Language})]]>
            </subreportParameterExpression> 
         </subreportParameter> 
         <connectionExpression>
            <![CDATA[$P{REPORT_CONNECTION}]]>
         </connectionExpression> 
         <subreportExpression>
            <![CDATA["ChildReport.jasper"]]>
         </subreportExpression> 
      </subreport> 
    4. Add several resource bundle files.
    5. Upload everything to JRS
    6. Enjoy the ability to schedule the report in locale you want.

    I prepared a screencast which demonstrates this behavior: http://somup.com/cq11o2nJGr Also, new project attached with all of resources.

    reportlocale.zip


    User Feedback

    Recommended Comments

    Thank you for this explanation!

    I was trying to set it up as you suggested - with a main raport and subraport. However when i run the main raport, i get the "resource not found" error.

    The language resource is set up in both main raport and subraport. When i run only the subraport, it finds the language resources and works fine. When i run the main rapport, then the error occures. 

    Would someone be so kind and help me figure out what i am doing wrong?

     

    Here is the error:

    net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRException: Resource not found at: Blank_A4_1.jasper.
    	at com.jaspersoft.studio.editor.preview.view.control.ReportController.fillReport(ReportController.java:553)
    	at com.jaspersoft.studio.editor.preview.view.control.ReportController.access$18(ReportController.java:528)
    	at com.jaspersoft.studio.editor.preview.view.control.ReportController$1.run(ReportController.java:446)
    	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
    Caused by: net.sf.jasperreports.engine.JRException: Resource not found at: Blank_A4_1.jasper.
    	at net.sf.jasperreports.repo.RepositoryUtil.getResourceFromLocation(RepositoryUtil.java:158)
    	at net.sf.jasperreports.repo.RepositoryUtil.getReport(RepositoryUtil.java:117)
    	at net.sf.jasperreports.engine.fill.JRFillSubreport.loadReport(JRFillSubreport.java:477)
    	at net.sf.jasperreports.engine.fill.JRFillSubreport.loadReportSource(JRFillSubreport.java:442)
    	at net.sf.jasperreports.engine.fill.JRFillSubreport.getReportSource(JRFillSubreport.java:397)
    	at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateReportSource(JRFillSubreport.java:365)
    	at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:506)
    	at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:357)
    	at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:383)
    	at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:548)
    	at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2613)
    	at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:836)
    	at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:275)
    	at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:119)
    	at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:622)
    	at net.sf.jasperreports.engine.fill.BaseFillHandle$ReportFill.run(BaseFillHandle.java:135)
    	at java.base/java.lang.Thread.run(Unknown Source)

     

    Here is the script of the main rapport parent_language.jrxml.

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Created with Jaspersoft Studio version 6.20.0.final using JasperReports Library version 6.20.0-2bc7ab61c56f459e8176eb05c7705e145cd400ad  -->
    <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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="Vorlagen/languages" uuid="28040505-dc2b-435c-a00c-487edd066d98">
    	<parameter name="language" class="java.lang.String"/>
    	<queryString>
    		<![CDATA[]]>
    	</queryString>
    	<detail>
    		<band height="342">
    			<subreport>
    				<reportElement x="180" y="10" width="200" height="200" uuid="84a158cb-e021-428b-8380-00f140e36a50"/>
    				<subreportParameter name="REPORT_LOCALE">
    					<subreportParameterExpression><![CDATA[new java.util.Locale($P{language})]]></subreportParameterExpression>
    				</subreportParameter>
    				<subreportExpression><![CDATA["Blank_A4_1.jasper"]]></subreportExpression>
    			</subreport>
    		</band>
    	</detail>
    	<pageFooter>
    		<band height="64">
    			<textField>
    				<reportElement x="384" y="34" width="100" height="30" uuid="1dd589ca-7390-4aac-8db0-68cd575b80da"/>
    				<textFieldExpression><![CDATA[$P{language}]]></textFieldExpression>
    			</textField>
    		</band>
    	</pageFooter>
    </jasperReport>

     

    Here is the code of subraport Blank_A4_1.jasper:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Created with Jaspersoft Studio version 6.20.0.final using JasperReports Library version 6.20.0-2bc7ab61c56f459e8176eb05c7705e145cd400ad  -->
    <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="Blank_A4_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="Vorlagen/languages" isIgnorePagination="true" uuid="dd3470f4-32ab-40e4-b4a4-f1fa801f99b1">
    	<queryString>
    		<![CDATA[]]>
    	</queryString>
    	<background>
    		<band splitType="Stretch"/>
    	</background>
    	<title>
    		<band height="79" splitType="Stretch"/>
    	</title>
    	<pageHeader>
    		<band height="35" splitType="Stretch"/>
    	</pageHeader>
    	<columnHeader>
    		<band height="61" splitType="Stretch">
    			<textField>
    				<reportElement x="370" y="15" width="100" height="30" uuid="f7b3a783-c5fb-44c5-8729-aa49768a52d2"/>
    				<textFieldExpression><![CDATA[$P{REPORT_LOCALE}]]></textFieldExpression>
    			</textField>
    		</band>
    	</columnHeader>
    	<detail>
    		<band height="125" splitType="Stretch">
    			<textField>
    				<reportElement x="306" y="69" width="100" height="30" uuid="64cb4632-4d04-4244-a07b-14385501b5b8"/>
    				<textFieldExpression><![CDATA[$R{Test}]]></textFieldExpression>
    			</textField>
    		</band>
    	</detail>
    	<columnFooter>
    		<band height="45" splitType="Stretch"/>
    	</columnFooter>
    	<pageFooter>
    		<band height="54" splitType="Stretch"/>
    	</pageFooter>
    	<summary>
    		<band height="42" splitType="Stretch"/>
    	</summary>
    </jasperReport>

     

    Link to comment
    Share on other sites



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...