0
votes

The loopback framework uses the strong-globalize module to provide translations for api responses. I've looked it up and see that there are a few default translations in the framework.

We want to use the 'Accept-Language' header field to determine which translation should be used. It is also mentioned in the strong-globalized readme.

But it is not working the translation is always english.

What we have to do to make it work?

1

1 Answers

0
votes

You need to create a middleware and set the current language there.

//server/middlewares/lang.js
var negotiator = require('negotiator');
var SG = require('strong-globalize');

module.exports = function(options) {
  var allowedLangs = ['en', 'fa'];
  return function localeHandler(req, res, next) {
    var lang = new negotiator(req).language(allowedLangs);
    var g = SG();
    g.setLanguage(lang);    
    next();
  };
};

And

//server/middleware.json
 "inital:after": {
    "./middlewares/lang": {}
  },

UPDATE

If you want to translate the built-in validation error messages, you should translate in client or set the translated messages for the validation.

//custom-user.js

CustomUser.validations.email = lodash.reject(CustomUser.validations.email, function(val) {
    return val.validation === 'presence';
  });

CustomUser.validatesPresenceOf('email', {message: g.t('email-can-not-be-blank')});