diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md
index bedbf78172..a6f6ac78db 100644
--- a/actionview/CHANGELOG.md
+++ b/actionview/CHANGELOG.md
@@ -1,3 +1,23 @@
+* Take label values into account when doing I18n lookups for model attributes.
+
+ The following:
+
+ # form.html.erb
+ <%= form_for @post do |f| %>
+ <%= f.label :type, value: "long" %>
+ <% end %>
+
+ # en.yml
+ en:
+ activerecord:
+ attributes:
+ post/long: "Long-form Post"
+
+ Used to simply return "long", but now it will return "Long-form
+ Post".
+
+ *Joshua Cody*
+
* Change `asset_path` to use File.join to create proper paths:
https://some.host.com//assets/some.js
diff --git a/actionview/lib/action_view/helpers/tags/label.rb b/actionview/lib/action_view/helpers/tags/label.rb
index 6335e3dd4d..a5bcaf8153 100644
--- a/actionview/lib/action_view/helpers/tags/label.rb
+++ b/actionview/lib/action_view/helpers/tags/label.rb
@@ -35,9 +35,9 @@ module ActionView
if block_given?
content = @template_object.capture(&block)
else
+ method_and_value = tag_value.present? ? "#{@method_name}.#{tag_value}" : @method_name
content = if @content.blank?
@object_name.gsub!(/\[(.*)_attributes\]\[\d+\]/, '.\1')
- method_and_value = tag_value.present? ? "#{@method_name}.#{tag_value}" : @method_name
if object.respond_to?(:to_model)
key = object.class.model_name.i18n_key
@@ -51,7 +51,7 @@ module ActionView
end
content ||= if object && object.class.respond_to?(:human_attribute_name)
- object.class.human_attribute_name(@method_name)
+ object.class.human_attribute_name(method_and_value)
end
content ||= @method_name.humanize
diff --git a/actionview/test/template/form_helper_test.rb b/actionview/test/template/form_helper_test.rb
index 0ad0ae6b4b..7b680aac08 100644
--- a/actionview/test/template/form_helper_test.rb
+++ b/actionview/test/template/form_helper_test.rb
@@ -19,6 +19,9 @@ class FormHelperTest < ActionView::TestCase
attributes: {
post: {
cost: "Total cost"
+ },
+ :"post/language" => {
+ spanish: "Espanol"
}
}
},
@@ -154,6 +157,12 @@ class FormHelperTest < ActionView::TestCase
end
end
+ def test_label_with_human_attribute_name_and_options
+ with_locale :label do
+ assert_dom_equal('', label(:post, :language, value: "spanish"))
+ end
+ end
+
def test_label_with_locales_symbols
with_locale :label do
assert_dom_equal('', label(:post, :body))
diff --git a/guides/source/i18n.md b/guides/source/i18n.md
index c1b575c7b7..0eba3af6e8 100644
--- a/guides/source/i18n.md
+++ b/guides/source/i18n.md
@@ -712,6 +712,19 @@ en:
Then `User.model_name.human(count: 2)` will return "Dudes". With `count: 1` or without params will return "Dude".
+In the event you need to access nested attributes within a given model, you should nest these under `model/attribute` at the model level of your translation file:
+
+```yaml
+en:
+ activerecord:
+ attributes:
+ user/gender:
+ female: "Female"
+ male: "Male"
+```
+
+Then `User.human_attribute_name("gender.female")` will return "Female".
+
#### Error Message Scopes
Active Record validation error messages can also be translated easily. Active Record gives you a couple of namespaces where you can place your message translations in order to provide different messages and translation for certain models, attributes, and/or validations. It also transparently takes single table inheritance into account.