my-koop/service.website — pull request #286 by Cellule (2014-11-20 06:10:32+00:00)
Title: i18n error parsing

common way to parse localization on errors in actions. This can help generate useful error messages.

it will take an error like `err = {app: {name: "invalid"} }` and generate `err.i18n = [{key: "app.name.invalid"}]`
it also supports 1 variable (not sure how to use more than one)
`err = {app: {name: "invalid__bad__"} }` and generate `err.i18n = [{key: "app.name.invalid", var: "bad"}]`

For instance, I can have a validation constraint like
```ts
var constraint = {
id: id,
name: {
presence: {message: "^empty"},
length: {
minimum: 4,
maximum: 45,
tooShort: "^tooShort__%{count}__",
tooLong: "^tooLong__%{count}__"
}
},
};
```
and a backend route that returns
```ts
callback(new ApplicationError(
err,
{
name: "duplicate"
}
));
```
then usage would be
```js
var self = this;
actions.mailinglist.update({
i18nErrors: {keys: ["app", "validation"]},
silent: true,
data: {
id: this.state.id,
name: this.state.name,
description: this.state.description
}
}, function(err) {
if(err) {
return self.setState({
feedback: {
i18n: !_.isEmpty(err.i18n) ?
_.map(err.i18n, function(i18n) {
return {
key: "mailinglist::updateMailingList." + i18n.key,
var: i18n.var
}
})
: [{key: "errors", context: err.context}],
style: "danger"
}
})
}
self.setState({
feedback: {i18n: [{key: "success"}], style: "success"}
});
});
```
and a localization file that looks like
```js
updateMailingList: {
application: {
name: {
duplicate: "This name is already used"
}
},
validation: {
name: {
empty: "Name cannot be blank",
tooShort: "Name must be at least __var__ characters long",
tooLong: "Name must be at most __var__ characters long"
}
}
}
```