Robin Dijkhof
Robin Dijkhof

Reputation: 19278

jsreport-core how add localization?

Consider the following example: https://playground.jsreport.net/w/anon/VkLWfMyMb-7

I was able to recreate this using jsreports-online. How can I add localization to jsreport-core?

app.get('/test', (req, res) => {

    jsreport().init().then((reporter: Reporter) => {
        const templatePath: string = path.join(__dirname, 'assets', 'template.html');
        const template: string = fs.readFileSync(templatePath, 'utf8');

        const exampledatapath: string = path.join(__dirname, 'assets', 'exampledata.json');
        const exampledata: string = fs.readFileSync(exampledatapath, 'utf8');

        let data = JSON.parse(exampledata);

        reporter.render({
            template: {
                content: template,
                engine: 'handlebars',
                recipe: 'chrome-pdf',
            },
            data: data
        }).then(function (out: any) {
            out.stream.pipe(res);
        })
            .catch(function (e: any) {
                res.end(e.message);
            });
    }).catch(function (e: any) {
        res.end(e.message);
    });
});

Best I could come up with is something like this data['$localizedResource'] = {key1: 'value1'};

Is there a better or build-in way to do it?

Upvotes: 0

Views: 393

Answers (1)

Jan Blaha
Jan Blaha

Reputation: 3095

You better handle localization on your own in such a case.

Using the resources extension with jsreport-core would require to install additional extension data. Insert resources to the documents store, reference it in the template call...

I see you are anyway sending the template content manually, not using the store. In this case, you better to store your localized files inside plain json, read them using nodejs and extend the input data with it.

reporter.render({
  template: {
    content: template,
    engine: 'handlebars',
    recipe: 'chrome-pdf',
  },
  data: {
    ...data,
    $localizedResource: JSON.parse(fs.readFileSync('....mylabels-en.json').toString())
  }
}

Upvotes: 0

Related Questions