Вопрос

Does anybody knows how to localize an error message from the controller? Can anyone point me a link to solve this?

This is the problem that I had right now.

var localStrategy = function localStrategy() {
'use strict';

return new LocalStrategy(function (username, password, done) {
    OUR_API.login({
        'username': username,
        'password': password
    }).then(function (accessToken) {
        return done(null, {
            'username': username,
            'accessToken': accessToken
        });
    }, function (err) {
        var errMessage;
        if(err.message.error.code === 123){
            errMessage = 'The email or password you’ve entered is incorrect.';
        }
        if(err.message.error.code === 125){
            errMessage = 'Your account has been locked.';
        }

        return done(null, false, errMessage);
    });
}); };

and this is a part of my dust page to display the message.

  {?messages}
    <ul>
       {#messages}
         <li>{.}</li>
       {/messages}

    </ul>
  {/messages}

and this is the code that would render my dust template, this is a passportjs implementation:

  model.messages = req.flash('error');
  res.render('login', model);

As you could see I have an errMessage that is hard-coded, how could I localize this? I'm using krakenjs and this code is a authentication strategy of Passportjs.

And any advice on how to implement this properly?

Это было полезно?

Решение

I made a work around to fix my problem.

I changed the hard-coded message to a just return a code, then on the dust, I displayed the appropriate message depending on the code passed from the controller.

Here's the code on the controller.

 return new LocalStrategy({
    passReqToCallback: true
}, function (req, username, password, done) {
    OUR_API.login({
        username: username,
        password: password
    }).then(function (accessToken) {
        return done(null, {
            username: username,
            accessToken: accessToken
        });
    }, function (err) {
        var errCode;
        if (err && err.message && err.message.error) {
            errCode = err.message.error.code.toString();
        }
        return done(null, false, errCode);
    });
});

then on the dust page.

   {?errorCode}
       {@select key="{errorCode}"}
         {@eq value="0001"}{@pre type="content" key="error.message.0001"/}{/eq}
         {@eq value="0002"}{@pre type="content" key="error.message.0002"/}{/eq}
         {@default}{@pre type="content" key="error.message.default"/}{/default}
       {/select}
   {/errorCode}

Hope this helps anyone having same problem as mine. Or anyone has better ideas?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top