Meteor errors internationalization

2019-08-15 00:33发布

I'm trying to implement internationalization through just-i18n and it works fine so far.

Problem is I'm also using accounts-password and especially Meteor.loginWithPassword(user, password, [callback]).

On login error, the callback has an error object that basically looks like this :

{
    details: undefined,
    error: 403,
    errorType: "Meteor.Error",
    message: "User not found [403]",
    reason: "User not found"
}

I thought the error code was unique and went with a i18n configuration file like this :

i18n.map 'fr_FR',
  login:
    signin: 'S\'authentifier'
  errors:
    403: 'L\'utilisateur n\'existe pas'

So I could call it this way :

Session.set "error", i18n("errors." + err.error)

But actually, no matter what's the error, user not found or incorrect password, the error code is not unique :

{
    details: undefined,
    error: 403,
    errorType: "Meteor.Error",
    message: "Incorrect password [403]",
    reason: "Incorrect password"
}

As i don't consider checking on a string value really consistent, how can i differenciate both ?

How would I go implementing internationalization with meteor built-in login ?

1条回答
霸刀☆藐视天下
2楼-- · 2019-08-15 00:39

403 here is not a Meteor number for the particular error, but rather a HTTP status code. The same code can be caused by different errors.

Since the only difference between the error objects you get are reason and message, you need to use one of them to set up the internationalization code. They shouldn't change too much between Meteor releases, so you should be fine with this solution.

i18n.map 'fr_FR',
  login:
    signin: 'S\'authentifier'
  errors:
    403:
      'Incorrect password': 'Le password est incorrectu'
      'User not found': 'L\'utilisateur n\'existe pas'

Ah, and don't worry, login, signin, errors and 403 are internally represented as strings as well, so there's nothing inconsistent in such solution.

查看更多
登录 后发表回答