0
私はノードjsアプリケーションで多言語を管理するためにExpressjsとi18nを使用しています。ここでノードjの永続的なロケール変更
は私の国際化の設定です:私は、ロケール設定を変更したい場合は
var i18n = require('i18n');
i18n.configure({
locales:['en', 'fr'],
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();
};
server.js
var i18n = require('./configs/i18n');
...
app.use(i18n);
は実際に私がする必要がある
版のi18n.js各ルートでそれを行います:
app.get('/index', function(req, res){
res.setLocale('fr')
res.render('pages/index');
});
setLocale()
を一度使用するとロケールが永久に変更されますか?
そしてベストプラクティスは何ですか?毎回自分のルート内で言語を指定する必要がありますか?例:
app.get('/:locale/index', function(req, res){
res.setLocale(req.params.locale)
res.render('pages/index');
});
app.get('/:locale/anotherroute', function(req, res){
res.setLocale(req.params.locale)
res.render('pages/anotherroute');
});
または、各ユーザーのデータベースにロケールを格納する必要がありますか?
あなたの回答者に感謝します。私は不明のプロパティ 'ロケール'を読み取ることができないのですか? 'app:get( '/:locale/index'、function(req、res))' – John
私はポストを更新し、私はバグを修正して、より良い解決法を追加します。 HTTPヘッダーと 'acceptsLanguage()'関数に基づいています。私はそれがあなたの問題に答えることを願っています –
Okありがとう。私のpassportJSユーザセッション変数で取得しますか? – John