Я хочу реализовать многоязычность в моем экспрессе (nodejs). Однако я не могу понять, почему мои ejs не понимают подчеркивание "__".
app.js
var i18n = require('./i18n');
app.use(i18n);
i18n.js
var i18n = require('i18n');
i18n.configure({
locales:['fr', 'en'],
directory: __dirname + '/locales',
defaultLocale: 'en',
cookie: 'lang'
});
module.exports = function(req, res, next) {
i18n.init(req, res);
res.locals.__ = res.__;
var current_locale = i18n.getLocale();
return next();
};
router.js
console.log(res.__('hello')); // print ok
console.log(res.__('helloWithHTML')); // print ok
req.app.render('index', context, function(err, html) {
res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
res.end(html);
});
/ locales / en.json
{
"hello": "Hello.",
"helloWithHTML": "helloWithHTML."
}
index.ejs
<%= __("hello")%>
Я получил сообщение об ошибке:
__ is not defined at eval (eval at compile (/home/nodejs/node_modules/ejs/lib/ejs.js:618:12), :41:7) at returnedFn
Однако я вижу сообщение журнала от маршрутизатора:
console.log(res.__('hello')); // print out Hello
console.log(res.__('helloWithHTML')); // print out helloWithHTML
Работает нормально, я вижу как hello
и helloWithHTML
values.
Но ejs
вообще не распознают i18n
переменную.
Как мне решить мою проблему?