Reputation: 129
I'm developing a proyect in nodejs, I want to add a feature for language translation.
Checking on internet i saw 2 options that seem ok (i18n and i18n-2).
I have added the proper code in my server side in order to configure it.
In express.js
var i18n = require('i18n-2');
i18n.expressBind(app, {
// setup some locales - other locales default to vi silently
locales: ['es', 'en'],
// set the default locale
defaultLocale: 'en',
// set the cookie name
cookieName: 'locale',
extension: ".json"
});
app.use(function(req, res, next) {
req.i18n.setLocaleFromQuery();
req.i18n.setLocaleFromCookie();
next();
});
So if in this same file I put this code below, it's suppose to print in english language because of the defaultLocale but it prints in Spanish
var i18n2 = new (require('i18n-2'))({
locales: ['es', 'en'],
// set the default locale
defaultLocale: 'en',
// set the cookie name
cookieName: 'locale',
extension: ".json"
});
console.log("Mensaje de i18n");
console.log( i18n2.__("hola") );
Apart of this question, I want to know how I can send this i18n variable to the client side in order to use it in the views of templates or if I have to create another one.
Greetings
Upvotes: 3
Views: 858
Reputation: 141
the problem is that defaultLocale option is not used during object initialization. You can check here how this lib doing it. just taking the first el from the locales array. So if you put 'en' language at 1st, it will work properly.
Upvotes: 1