Skip to content

Error messages do not let us translate the attribute #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gravsten opened this issue Aug 21, 2016 · 7 comments
Closed

Error messages do not let us translate the attribute #281

gravsten opened this issue Aug 21, 2016 · 7 comments

Comments

@gravsten
Copy link

If I have a property of my model called "password" to be validated, while I can pass the translated error messages in the options, the attribute never gets translated.

To solve this problem, I have changed on line in validate.js (around line 426):

  convertErrorMessages: function(errors, options) {

...
error = v.capitalize(v.prettify(errorInfo.attribute)) + " " + error;

needs to be changed to:
error = v.capitalize(v.prettify(errorInfo.options.tr || errorInfo.attribute)) + " " + error;

While I considered naming the option 'attribute', this would have collided with other uses (e.g. in equality validator). So I went for 'tr', which refers to the i18n.tr() function.

As a result, the attribute name gets properly translated for the user.

@gravsten
Copy link
Author

As a follow up, there was one more change needed especially for the 'equality' validator:
if (!comparator(value, otherValue, options, attribute, attributes)) {
return v.format(message, {attribute: v.prettify(options.attribute_tr || options.attribute)});
}

Please note the additional 'options.attribute_tr || ' above, which let us translate the linked attribute.

@gheoan
Copy link
Contributor

gheoan commented Aug 22, 2016

ansman/validate.js#148

@gravsten
Copy link
Author

Here is an example use of the above changes to support translated attribute:
@required({ tr: i18n.tr('email'), message: i18n.tr('validatejs.presence.message') })

And another for the special case of @Equality with two attributes:
@equality({ attribute: 'new_password_1', attribute_tr: i18n.tr('new_password'), tr: i18n.tr('confirmation'), message: i18n.tr('validatejs.equality.message') })

@gravsten
Copy link
Author

I have updated the aforementioned proposed changes to avoid prettifying the provided translations:
error = v.capitalize(errorInfo.options.tr || v.prettify(errorInfo.attribute)) + " " + error;
and for @Equality:
return v.format(message, {attribute: options.attribute_tr || v.prettify(options.attribute)});

This proposal has the merit of being simple, working now, and compatible with all existing Aurelia applications (if you don't use the 'tr' option, the attributes will just keep not being translated).
But I would be glad to hear about other possible solutions for translating validation attributes.

@jdanyow
Copy link
Contributor

jdanyow commented Aug 27, 2016

this is now possible- check out the examples in the docs.

@jdanyow jdanyow closed this as completed Aug 27, 2016
@Strato
Copy link

Strato commented Sep 19, 2016

this is now possible- check out the examples in the docs.

@jdanyow Possible how? What doc are you talking about? Validate.js or Aurelia?

Can you provide a link please?

@jdanyow
Copy link
Contributor

jdanyow commented Sep 19, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants