Total.js 等待本地化事件

Total.js wait for event in localization

正在尝试等待 total.js 中的异步调用以进行本地化。但是,当我尝试让 localization.js 部分等待响应时,程序会继续并自动选择语言 'en'。下面的代码没有处理异步过程,但我分享它是为了展示我正在尝试做的事情。知道怎么做吗?

var COOKIE = '__language',
allowed = {en: true, dk: true};

F.onLocate = function (req, res) {
var self = this,
language = req.query.language || req.cookie(COOKIE);
var userIP = self.ip

// Set the language according to the querystring and store to the cookie
if (language) {
    if (!allowed[language])
        return 'en';
    res.cookie(COOKIE, language, '2 days');
    return language;
}

// addition if cookie is not set, first time Danish users will have 'dk' cookie set
else {
 // return from here works
   Util.waitUtils.request('https://ipfind.co?ip='+userIP +'&auth=myauthcodehere'
    , ['get'], function(err, data, status, headers) {


          var result = JSON.parse(data);

          if(result.country_code === 'DK')
            {
              res.redirect('/?language=dk');

             }
 });
}

res.cookie(COOKIE, 'en', '2 days');
return 'en';
};

这是不可能的。 F.onLocale 是同步的。 解决方案是使用中间件:

F.middleware('language', function(req, res, next) {

    // ...
    // ...

    RESTBuilder.make(function(builder) {
        builder.url('https://ipfind.com?ip={0}&myauthcodehere={1}'.format(req.ip, 'myauthcodehere'));
        builder.exec(function(err, response) {

            req.language = response.country_code;
            next();

            // or
            // res.redirect('?language=dk');
            // next = null;
        });
    });
});

谢谢。