i18n 翻译部分适用于具有车把支持的 Node Express 应用程序
i18n translation partially works in node express app with handlebars support
我的问题是当我访问 ?lang=en
查询参数 url 时从未使用英文翻译。然而,匈牙利语文本更改效果很好,在默认的 hu 语言中显示 text to test on hungarian
没有问题。出了什么问题?
app.js:
var express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
exphbs = require('express-handlebars'),
i18n = require('i18n');
var app = express();
app.engine('.hbs', exphbs({
extname: '.hbs',
defaultLayout: 'main',
helpers: {
__: function() { return i18n.__.apply(this, arguments); },
__n: function() { return i18n.__n.apply(this, arguments); }
}
}));
app.set('view engine', '.hbs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/', require('./routes/portfolio'));
i18n.configure({
locales: ['hu', 'en'],
fallbacks: {'en': 'hu'},
defaultLocale: 'hu',
cookie: 'locale',
queryParameter: 'lang',
directory: __dirname + '/locales',
directoryPermissions: '755',
autoReload: true,
updateFiles: true,
api: {
'__': '__', //now req.__ becomes req.__
'__n': '__n' //and req.__n can be called as req.__n
}
});
app.use(i18n.init);
views/portfolio.hbs:
<span id="text">{{{__ "text to test"}}}</span>
locales/hu.json:
{
"text to test": "text to test on hungarian"
}
locales/en.json:
{
"text to test": "text to test on english"
}
完整控制台登录开始:
i18n:debug will use C:\www\node\lantosistvan\locales\hu.json +0ms
i18n:debug read C:\www\node\lantosistvan\locales\hu.json for locale: hu +5ms
i18n:debug will use C:\www\node\lantosistvan\locales\en.json +3ms
i18n:debug read C:\www\node\lantosistvan\locales\en.json for locale: en +1ms
lantosistvan-portfolio:server Listening on port 3000 +16ms
i18n:warn WARN: No locale found - check the context of the call to __(). Using hu as current locale +5s
GET /about-me?lang=en 304 90.678 ms - -
感谢您的帮助!
更改路由器的顺序 i18n.configure 看起来问题已解决,因此您必须在路由器调用之前放置 i18n。
我的问题是当我访问 ?lang=en
查询参数 url 时从未使用英文翻译。然而,匈牙利语文本更改效果很好,在默认的 hu 语言中显示 text to test on hungarian
没有问题。出了什么问题?
app.js:
var express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
exphbs = require('express-handlebars'),
i18n = require('i18n');
var app = express();
app.engine('.hbs', exphbs({
extname: '.hbs',
defaultLayout: 'main',
helpers: {
__: function() { return i18n.__.apply(this, arguments); },
__n: function() { return i18n.__n.apply(this, arguments); }
}
}));
app.set('view engine', '.hbs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/', require('./routes/portfolio'));
i18n.configure({
locales: ['hu', 'en'],
fallbacks: {'en': 'hu'},
defaultLocale: 'hu',
cookie: 'locale',
queryParameter: 'lang',
directory: __dirname + '/locales',
directoryPermissions: '755',
autoReload: true,
updateFiles: true,
api: {
'__': '__', //now req.__ becomes req.__
'__n': '__n' //and req.__n can be called as req.__n
}
});
app.use(i18n.init);
views/portfolio.hbs:
<span id="text">{{{__ "text to test"}}}</span>
locales/hu.json:
{
"text to test": "text to test on hungarian"
}
locales/en.json:
{
"text to test": "text to test on english"
}
完整控制台登录开始:
i18n:debug will use C:\www\node\lantosistvan\locales\hu.json +0ms
i18n:debug read C:\www\node\lantosistvan\locales\hu.json for locale: hu +5ms
i18n:debug will use C:\www\node\lantosistvan\locales\en.json +3ms
i18n:debug read C:\www\node\lantosistvan\locales\en.json for locale: en +1ms
lantosistvan-portfolio:server Listening on port 3000 +16ms
i18n:warn WARN: No locale found - check the context of the call to __(). Using hu as current locale +5s
GET /about-me?lang=en 304 90.678 ms - -
感谢您的帮助!
更改路由器的顺序 i18n.configure 看起来问题已解决,因此您必须在路由器调用之前放置 i18n。