【发布时间】:2014-12-11 13:57:49
【问题描述】:
使用:NodeJS (Express 4) + i18n + 下划线。
我想在 NodeJS (Express 4) 中绑定和翻译一个下划线模板。
- 绑定工作正常。
- 翻译在模板之外也能正常工作。
但我在模板中的翻译有问题:下划线不理解语法 :
[ReferenceError: __ is not defined]
这是我的 NodeJS 代码:
var express = require('express'),
app = express(),
cons = require('consolidate'),
i18n = require('i18n');
_ = require('underscore'),
// setup i18n
app.use(i18n.init);
i18n.configure({
locales: ['en', 'fr'],
directory:'./app/locales',
defaultLocale: 'en'
});
// setup hbs
app.engine('html',cons.underscore);
app.set('views', './app/views');
app.set('view engine', 'html');
// translation test ok
console.log('Translation test: ' + i18n.__('hello'));
// rendering template generates error
app.render('test.html', {hello: 'Welcome !'}, function(err, html){
if(err){
console.log(err);
} else {
console.log(html);
}
});
这是我的 Undescore 模板“test.html”:
<h1><%= hello %></h1>
<p><%= __('hello') %></p>
还有英文“en.json”的 JSON i18n 文件:
{
"hello": "hello my friend"
}
【问题讨论】:
标签: javascript node.js express internationalization underscore.js