【问题标题】:use i18n-2 in node.js outside of res scope在 res 范围之外的 node.js 中使用 i18n-2
【发布时间】:2019-04-03 19:08:47
【问题描述】:

我试图瞄准,这样我就可以在被调用的函数中使用 i18n。

我有错误:

(node:15696) UnhandledPromiseRejectionWarning: TypeError: i18n.__ is not a function

我怎样才能让 i18n 可以在函数内部工作而不必在请求中?

Server.js:

var    i18n = require('i18n-2');

global.i18n = i18n;
i18n.expressBind(app, {
    // setup some locales - other locales default to en silently
    locales: ['en', 'no'],
    // change the cookie name from 'lang' to 'locale'
    cookieName: 'locale'
});

app.use(function(req, res, next) {
    req.i18n.setLocaleFromCookie();
    next();
});

//CALL another file with some something here.

其他文件.js:

somefunction() {
               message = i18n.__("no_user_to_select") + "???";

}

我应该如何解决这个问题?

【问题讨论】:

    标签: javascript node.js internationalization global


    【解决方案1】:

    如果您仔细阅读Using with Express.js 下的文档,就会清楚地记录它的使用方式。通过i18n.expressBind绑定i18n到express app后,i18n可以通过req对象使用,所有express中间件都可以使用:

    req.i18n.__("My Site Title")
    

    所以somefunction 应该是这样的中间件:

    function somefunction(req, res, next) {
      // notice how its invoked through the req object
      const message = req.i18n.__("no_user_to_select") + "???";
      // outputs -> no_user_to_select???
    }
    

    或者您需要通过如下中间件显式传入req 对象:

    function somefunction(req) {
      const message = req.i18n.__("no_user_to_select") + "???";
      // outputs -> no_user_to_select???
    }
    
    app.use((req, res, next) => {
      somefunction(req);
    });
    

    如果你想直接使用i18n,你需要instantiate它,就像文档一样

    const I18n = require('i18n-2');
    
    // make an instance with options
    var i18n = new I18n({
        // setup some locales - other locales default to the first locale
        locales: ['en', 'de']
    });
    
    // set it to global as in your question
    // but many advise not to use global
    global.i18n = i18n;
    
    // use anywhere
    somefunction() {
      const message = i18n.__("no_user_to_select") + "???";
      // outputs -> no_user_to_select???
    }
    

    许多人不鼓励使用全局。

    // international.js
    // you can also export and import
    const I18n = require('i18n-2');
    
    // make an instance with options
    var i18n = new I18n({
        // setup some locales - other locales default to the first locale
        locales: ['en', 'de']
    });
    
    module.exports = i18n;
    
    // import wherever necessary
    const { i18n } = require('./international');
    

    【讨论】:

    • 对于我需要调用的当前函数,在彼此之间调用,可能没有任何 req 对象。可以根据保存了某种 i18n 变量的用户 ID 获取语言吗?我明白你的意思。 (Y)
    • 那你就可以通过先做实例来使用全局版本了。 Possible to get languages based on userid that have some sort of i18n variable saved 假设您正在从某个数据库中检索?
    • 是保存在数据库中。会chrck回答
    • 如果某些变量基于用户 ID 数据库,我将如何处理?我知道这不是原来的问题。感谢您的方法,将接受。
    • 你可以使用setLocale(locale),因为locale 是在你的option 中定义的,比如.setLocale('de')(假设你已经从db 中检索了de
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    相关资源
    最近更新 更多