【问题标题】:How to use i18n in Pug template如何在 Pug 模板中使用 i18n
【发布时间】:2018-04-02 05:58:12
【问题描述】:

我在我的 Express + pug 应用程序中使用 i18n。我通过以下方式完成了 i18 配置:

i18n = require("i18n")

i18n.configure({
        locales:['en', 'mn'],
        directory: __dirname + '/locales'
    });

app.use(i18n.init);

app.use(function(req, res, next) {
    // express helper for natively supported engines
    res.locals.__ = res.__ = function() {
        return i18n.__.apply(req, arguments);
    };

    next();
});

但我不知道如何在 pug 文件中使用它。我已经尝试过这样的方法,但没有成功:

input(type="text" name="near-location" placeholder="__('Ask')")
input(type="text" name="near-location" placeholder="${__('Ask')}"
input(type="text" name="near-location" placeholder="#{Ask}")

我应该如何在 pug 模板中使用 i18n?

【问题讨论】:

  • 你找到解决办法了吗
  • @ramon22,我最终使用了 ejs

标签: express internationalization pug


【解决方案1】:

试试这个,它对我有用:

input(type="text" name="near-location" placeholder=__('Ask'))

【讨论】:

    【解决方案2】:

    我得到它的工作必须在编译模板文件时添加该函数,以便 pug 识别模板中的 i18n #{__('test')}。

    const i18n = require("i18n");
    const pug = require('pug');
    
    const compiledHTML = pug.compileFile(templatePath)({
        name: user.name
        __: i18n.__
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      • 2016-05-01
      相关资源
      最近更新 更多