如何在Pug模板中使用i18n

时间:2017-12-11 02:02:39

标签: express internationalization pug

我正在使用i18n作为我的Express + pug应用。我已经按照以下方式完成了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}")

我应该如何在哈巴狗模板中使用i18n?

2 个答案:

答案 0 :(得分:0)

试试这个,它对我有用:

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

答案 1 :(得分:0)

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

const i18n = require("i18n");
const pug = require('pug');

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