1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #14997 from jpcody/nested_value_i18n

Include label value in i18n attribute lookup
This commit is contained in:
Rafael Mendonça França 2014-05-06 16:40:45 -03:00
commit 29ae431c51
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.