sails: req.getLocale() returns always 'en' language on ajax request

Node version: 8.15.1 Sails version (sails): 1.1.0 ORM hook version (sails-hook-orm): 2.0.0 Sockets hook version (sails-hook-sockets): 1.4.0 Organics hook version (sails-hook-organics): 0.13.0 Grunt hook version (sails-hook-grunt): 3.0.2


Hello again 😃 When I try normal request, req.getLocale() returns correct language code ('cs' in my case), but when I make ajax request on the same page req.getLocale() returns 'en'.

Both requests are in the browser with same Accept-Language: cs,sk;q=0.8,en-US;q=0.5,en;q=0.3 … so I think problem must be somewhere on the backend. Missed I something or what’s wrong here?

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Hi, thanks for explanation, I came with this solution (maybe somebody will have better idea, because I know, that this isn’t right purpose of policies):

/config/policies.js:

module.exports.policies = {
  '*': 'lang'
}

/policies/lang.js :

module.exports = async function (req, res, proceed) {
    let lang = req.allParams().lang;
    if (lang){
        req.session.lang = lang;
        req.setLocale(lang);
    }else{
        if (!req.session.lang){
            if (!req.isSocket){
                req.session.lang = req.getLocale();
            }
        }else{
            req.setLocale(req.session.lang);
        }
    }
    return proceed();
};