Sailsjs change localization

后端 未结 2 1491
礼貌的吻别
礼貌的吻别 2020-12-14 23:58

I\'ve been using Sails.js for quite some time and was wondering if there is a way to manually change the localization from the controllers depending on the url.

Exam

相关标签:
2条回答
  • 2020-12-15 00:35

    Update 2020 to @sgress454's answer

    // api/policies/localize.js`

    module.exports = function(req, res, next) {
      // This worked for testing
      // You can use req.param('lang') instead of 'in'
      req.setLocale('in');
      next();
    
    };
    
    0 讨论(0)
  • 2020-12-15 00:54

    You can always change the locale in a controller action by using req.setLocale() or by setting the value of req.locale. You can also handle this more globally by using a policy:

    // config/routes.js
    
    module.export.routes = {
    
      '/:lang/': 'MyController.index',
      '/:lang/help': 'MyController.help',
      '/:lang/contact': 'MyController.contact',
      ...etc...
    
    }
    

    // config/policies.js
    
    module.exports.policies = {
    
       '*' : 'localize'
    
    }
    

    // api/policies/localize.js
    
    module.exports = function(req, res, next) {
    
       req.locale=req.param('lang');
       next();
    
    };
    
    0 讨论(0)
提交回复
热议问题