Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

internationalization in Node.js, Express, i18n

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)?

like image 616
freakish Avatar asked Jan 15 '12 22:01

freakish


People also ask

What is i18n internationalization?

Internationalization (sometimes shortened to "I18N , meaning "I - eighteen letters -N") is the process of planning and implementing products and services so that they can easily be adapted to specific local languages and cultures, a process called localization .

What is i18n NPM?

Lightweight simple translation module with dynamic JSON storage. Supports plain vanilla Node. js apps and should work with any framework (like Express, restify and probably more) that exposes an app. use() method passing in res and req objects.


1 Answers

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. :)

like image 135
freakish Avatar answered Nov 15 '22 16:11

freakish