我不确定i18n是否适合这个。因为看起来这个模块只关注于改变语言属性(在视图渲染中)而不是视图本身。我希望我能够很好地解释自己:)
所以这个想法是当一个人改变语言时,渲染器从语言目录中选择视图(具有相同的文件名)。
您可以使用此类目录层次结构:
-views
--EN
---index.jade
---contact.jade
--NL
---index.jade
---contact.jade
--form-contact.jade
要明确,'form-contact.jade'
文件将包含表单(包含在'contact.jade'
文件中。因此'form-contact.jade'
确实会使用 i18n别名对象(将驻留在en.json或nl.json文件中)。
目前我只有以下内容:
app.js
i18n.configure({
locales: ['en', 'nl'],
directory: __dirname + '/locales',
defaultLocale: 'en',
extension: '.json',
register: global
});
app.use(i18n.init);
app.use(function (req, res, next) {
console.log(i18n);
if (req.query.lang != undefined && i18n.locales.indexOf(req.query.lang) >= 0) {
i18n.setLocale(req.query.lang);
}else{
i18n.setLocale(i18n.defaultLocale);
}
next();
});
现在,我如何让res.render选择正确的视图?
router.get('/contact', function(req, res, next) {
res.render('/contact', { title: '...', menu: '...' });
});
编辑:
如果你们有关于翻译大型静态页面(完整的html / css)的建议,请提出你的看法:)
答案 0 :(得分:0)
你的方式是错的。 如果您对每种语言都有看法,那么使用i18n有什么意义呢?
你可以得到
?lang=
值并发送res.render ( '/' + req.query.lang + '/contact.html').
尝试方式是
-locales
--en.json
--nl.json
-view
--cabinet.html
--main.html
<强> app.js 强>
i18n.configure({
locales: ['en', 'nl'],
directory: __dirname + '/locales',
defaultLocale: 'nl',
updateFiles: false,
objectNotation: true
});
app.use(i18n.init);
app.use(function (req, res, next) {
console.log(i18n);
if (req.query.lang != undefined && i18n.locales.indexOf(req.query.lang) >= 0) {
i18n.setLocale(req.query.lang);
}else{
i18n.setLocale(i18n.defaultLocale);
}
next();
});
en.json 示例
{
"cabinet": {
"menu": {
"title": "Menu",
},
"dashboard": {
"title": "Dashboard"
},
"statistic": {
"datePeriod": "Date period",
"choose": "Choose",
}
},
"main": {
"title": "Main",
"body": {
"hi": "Hi everyone!"
}
}
与nl.json相同
<强> main.html中强>
I use Swig template
<html>
<head>
<title>{{ translate.title }}</title>
</head>
<body>
{{ translate.body.hi }}!!
</body>
</html>
<强> router.js 强>
router.get('/main', function(req, res, next) {
res.render('/main', { title: '...', menu: '...', translate: res.__('main') });
});
router.get('/cabinet', function(req, res, next) {
res.render('/cabinet', { title: '...', menu: '...', translate: res.__('cabinet') });
});
答案 1 :(得分:0)
只需使用@cheks提到的单独页面,或者更好的是使用像Angular
这样的前端框架,您可以去:
<div if-language="en">my english text</div>
<div if-language="fr">mon texte français</div>