首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Total.js在本地化中等待事件

Total.js在本地化中等待事件
EN

Stack Overflow用户
提问于 2016-11-13 20:39:23
回答 1查看 213关注 0票数 0

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

代码语言:javascript
复制
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';
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-14 10:01:27

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

代码语言:javascript
复制
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;
        });
    });
});

谢谢。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40578569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档