最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - internationalization in Node.js, Express, i18n - Stack Overflow

matteradmin13PV0评论

I'm using Node.js together with Express framework and i18n module. So I use

var i18n = require('i18n');
app.configure(function() {
    [...]
    app.use(i18n.init);
    app.use(app.router);
});

in my app settings. Everything works fine, but what I really need is to force i18n to use the langauage I want. The scenario is as follows: when user is not logged in, then i18n searches for the langauage in accept-language header and it is ok. But when user is logged in, then I want to hold chosen langauage somewhere in user settings, retrieve it and force i18n module to use this langauage. How to do this (assuming I already know how to save/retrieve the langauge into/from db)?

I'm using Node.js together with Express framework and i18n module. So I use

var i18n = require('i18n');
app.configure(function() {
    [...]
    app.use(i18n.init);
    app.use(app.router);
});

in my app settings. Everything works fine, but what I really need is to force i18n to use the langauage I want. The scenario is as follows: when user is not logged in, then i18n searches for the langauage in accept-language header and it is ok. But when user is logged in, then I want to hold chosen langauage somewhere in user settings, retrieve it and force i18n module to use this langauage. How to do this (assuming I already know how to save/retrieve the langauge into/from db)?

Share Improve this question asked Jan 15, 2012 at 22:18 freakishfreakish 56.5k12 gold badges137 silver badges177 bronze badges 1
  • just to mention i18next - i18n for node.js might be a good alternative - just have a look! – jamuhl Commented Sep 7, 2012 at 10:25
Add a ment  | 

2 Answers 2

Reset to default 12

Ach, sorry. I should wait a bit and inspect the i18n module. It seems that module provides to functions

i18n.getLocale();

which retrieves current language and

i18n.setLocale('en');

which sets locale as we want. The documentation really should mention this. It is important, I hope the answer helps someone. :)

This was just released yesterday, but it sounds like it will hit the issue you are running into: locale

Post a comment

comment list (0)

  1. No comments so far