Include label value in i18n attribute lookup

Previously, only the object and method name from the label tag were
used when looking up the translation for a label. If a value is
given for the label, this ought to be additionally used. 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".
This commit is contained in:
Joshua Cody 2014-05-05 17:48:53 -05:00
parent b6bab2af14
commit d5f2c5c20e
4 changed files with 44 additions and 2 deletions

View File

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

View File

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

View File

@ -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 for="post_language_spanish">Espanol</label>', label(:post, :language, value: "spanish"))
end
end
def test_label_with_locales_symbols
with_locale :label do
assert_dom_equal('<label for="post_body">Write entire text here</label>', label(:post, :body))

View File

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