Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

ey-pairing
Copy link

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)

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)
@ey-pairing
Copy link
Author

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}"],
Copy link
Member

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}"],

@josevalim
Copy link
Contributor

Thanks. This looks fine to me. Although we need to update the documentation (or properly document Errors#full_messages if it doesn't mention errors.formats yet).

@rafaelfranca
Copy link
Member

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.

@jacobo
Copy link

jacobo commented Aug 16, 2012

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 def full_message or is there somewhere better?

@rafaelfranca
Copy link
Member

Yes, CHANGELOG should be added as part of this pull request.

I would add the documentation in the comments above the full_message definition. Something like what is above the def generate_message in the same file.

@@ -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}", {
Copy link
Contributor

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?

@frodsan
Copy link
Contributor

frodsan commented Oct 29, 2012

any news on this?

@bai
Copy link

bai commented Jan 8, 2013

Any updates on this one?

@steveklabnik
Copy link
Member

@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.

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

Successfully merging this pull request may close these issues.

None yet

8 participants