Zolbayar
Zolbayar

Reputation: 936

How to use i18n in Pug template

I'm using i18n for my Express + pug app. I've done i18 config in following way:

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();
});

But I don't know how to use it in the pug file. I've tried it like these and it didn't work:

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

How should I use i18n in a pug template?

Upvotes: 1

Views: 2548

Answers (2)

Alejandro
Alejandro

Reputation: 11

Try this, it works for me:

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

Upvotes: 1

ramon22
ramon22

Reputation: 3628

I got it work had to add the function when compiling the template file, for pug to recognize the i18n in the template #{__('test')}.

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

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

Upvotes: 0

Related Questions