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
i18n error messages: custom formats by attribute #7369
Conversation
First place lookup is now: errors.formats.attributes.#{attribute} Example use case: errors: attributes: last_name: blank: you must provide your full name formats: attributes: last_name: "%{message}" (same fallbacks as before)
This patch could also be reasonably be applied to 3.2-stable branch |
I18n.t(:"errors.format", { | ||
:default => "%{attribute} %{message}", | ||
I18n.t(:"errors.formats.attributes.#{attribute}", { | ||
:default => [:"errors.format","%{attribute} %{message}"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:default => [:"errors.format", "%{attribute} %{message}"],
Thanks. This looks fine to me. Although we need to update the documentation (or properly document |
Thank you. Seems good. I made a code convention comment. Also we will need a CHANGELOG entry and update the documentation for this. Unfortunately we don't merge feature addition to 3-2-stable. This branch is only fo bug fixes now. |
Thanks for the prompt reply! Should we submit a CHANGELOG change as part of this pull request? By documentation do you mean just the bit of comments above |
Yes, CHANGELOG should be added as part of this pull request. I would add the documentation in the comments above the |
@@ -355,8 +355,8 @@ def full_message(attribute, message) | |||
return message if attribute == :base | |||
attr_name = attribute.to_s.tr('.', '_').humanize | |||
attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) | |||
I18n.t(:"errors.format", { | |||
:default => "%{attribute} %{message}", | |||
I18n.t(:"errors.formats.attributes.#{attribute}", { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it really be looking for the string under :"#{@base.class.i18n_scope}.errors"
(activerecord/activemodel) namespace first (just like it is done for validation error messages)?
Additionally, this doesn't allow us to set different message formats for fields with the same name in different models. Should it support that? Maybe this syntax would be additional fallback? Basically we could have defaults array that's similar to the one constructed in ActiveModel::Errors#generate_message.
Or maybe I should do another pull request to add these things :)
Thoughts?
any news on this? |
Any updates on this one? |
@jacobo @ey-pairing it's been... 5 months since we've heard from you. If you're interested in keeping up with this pull request, please let me know, but I'm giving this a close as stale . Thanks. |
First place lookup is now: errors.formats.attributes.#{attribute}
Example use case:
(same fallbacks as before)