我想在我的express(nodejs)中实现多语言但是我无法理解为什么我的ejs不理解“__”下划线。
应用程序.js
var i18n = require('./i18n');
app.use(i18n);
Run Code Online (Sandbox Code Playgroud)
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();
};
Run Code Online (Sandbox Code Playgroud)
路由器.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);
});
Run Code Online (Sandbox Code Playgroud)
/locales/en.json
{
"hello": "Hello.",
"helloWithHTML": "helloWithHTML."
}
Run Code Online (Sandbox Code Playgroud)
索引.ejs
<%= __("hello")%>
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
__ is not defined at eval (eval at compile …Run Code Online (Sandbox Code Playgroud)