Nodejs:使用i18n-2客户端或服务器端或两者

我正在开发一个nodejs的项目,我想添加一个语言翻译的function。

在互联网上检查,我看到2个选项,似乎确定(国际和国内18)。

我在服务器端添加了正确的代码来configuration它。

在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(); }); 

所以,如果在同一个文件中,我把这个代码放在下面,假设它是用defaultLocale打印的,但是用西class牙文打印

 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") ); 

除了这个问题,我想知道如何将这个i18nvariables发送到客户端,以便在模板的视图中使用它,或者如果我必须创build另一个。

问候

问题是在对象初始化期间不使用defaultLocale选项。 你可以在这里查看这个lib如何做。 从locales数组中取出第一个el。 所以,如果你在1日使用'en'语言,它将会正常工作。