Meteor errors internationalization

我的未来我决定 提交于 2019-12-24 08:18:31

问题


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:


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.



来源:https://stackoverflow.com/questions/24191787/meteor-errors-internationalization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!