Overwrite localization at runtime



  • Can I overwrite the language of a report with beforeRender, or is it mandatory to do it with the api call?

    req.data has already the localized values ready for the report, the only way would be to overwrite those with the values from $resource 'manually'. Is this a correct and recommended procedure?

    Like:

    req.data.$localizedResource = req['data']['$resource']['en-shipping'];
    


  • The localization runs before the script's beforeRender so it is too late to change the language there.

    I would personally, in this case, don't use the extra extension and do it exclusively just by script.

    const jsreport = require('jsreport-proxy')
    async function beforeRender(req, res) {
      const localizedResource = await jsreport.documentStore.collection('data').findOne({ 
        name: 'en' + '-data'  // your condition to find the right resource
      })
      
     Object.assign(req.data, {
       $localizedResource: JSON.parse(localizedResource.dataJson)
     }) 
    }
    

Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.