【问题标题】:jsreport-core how add localization?jsreport-core如何添加本地化?
【发布时间】:2019-12-05 02:30:35
【问题描述】:

考虑以下示例:https://playground.jsreport.net/w/anon/VkLWfMyMb-7

我能够使用 jsreports-online 重新创建它。如何将本地化添加到 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);
    });
});

我能想到的最好的就是这样的data['$localizedResource'] = {key1: 'value1'};

有更好的或内置的方法吗?

【问题讨论】:

    标签: jsreport


    【解决方案1】:

    在这种情况下,您最好自己处理本地化。

    resources extension 与jsreport-core 一起使用需要安装额外的扩展data。将资源插入文档存储,在模板调用中引用它...

    我看到您仍然手动发送模板内容,而不是使用商店。在这种情况下,您最好将本地化文件存储在纯 json 中,使用 nodejs 读取它们并使用它扩展输入数据。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多