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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

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

:attribute => attr_name,
:message => message
})
Expand Down
6 changes: 6 additions & 0 deletions activemodel/test/cases/validations/i18n_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def test_errors_full_messages_uses_format
assert_equal ["Field Name empty"], @person.errors.full_messages
end

def test_errors_full_messages_looks_up_custom_attribute_format
I18n.backend.store_translations('en', :errors => {:formats => {:attributes => {:name => "Name is: %{message}"}}})
@person.errors.add('name', 'empty')
assert_equal ["Name is: empty"], @person.errors.full_messages
end

# ActiveModel::Validations

# A set of common cases for ActiveModel::Validations message generation that
Expand Down