aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/form_helper.rb
diff options
context:
space:
mode:
authorCarsten Gehling <cg@cg-laptop.(none)>2009-12-29 13:06:19 +0100
committerJosé Valim <jose.valim@gmail.com>2010-01-02 22:27:02 +0100
commitbef968d37942bfb2b7a59fca0e4451e096197c0a (patch)
tree544865b276f700657666ce5d26bd8205691f0afa /actionpack/lib/action_view/helpers/form_helper.rb
parent653fa4c10c3e4a34cfe0fe93a2612ed178ca4455 (diff)
downloadrails-bef968d37942bfb2b7a59fca0e4451e096197c0a.tar.gz
rails-bef968d37942bfb2b7a59fca0e4451e096197c0a.tar.bz2
rails-bef968d37942bfb2b7a59fca0e4451e096197c0a.zip
I18n label helper [#745 status:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'actionpack/lib/action_view/helpers/form_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb39
1 files changed, 36 insertions, 3 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index d0c66eda60..9baa9f1fff 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -504,8 +504,9 @@ module ActionView
end
# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
- # assigned to the template (identified by +object+). The text of label will default to the attribute name unless you specify
- # it explicitly. Additional options on the label tag can be passed as a hash with +options+. These options will be tagged
+ # assigned to the template (identified by +object+). The text of label will default to the attribute name unless a translation
+ # is found in the current I18n locale (through views.labels.<modelname>.<attribute>) or you specify it explicitly.
+ # Additional options on the label tag can be passed as a hash with +options+. These options will be tagged
# onto the HTML as an HTML element attribute as in the example shown, except for the <tt>:value</tt> option, which is designed to
# target labels for radio_button tags (where the value is used in the ID of the input tag).
#
@@ -513,6 +514,29 @@ module ActionView
# label(:post, :title)
# # => <label for="post_title">Title</label>
#
+ # You can localize your labels based on model and attribute names.
+ # For example you can define the following in your locale (e.g. en.yml)
+ #
+ # views:
+ # labels:
+ # post:
+ # body: "Write your entire text here"
+ #
+ # Which then will result in
+ #
+ # label(:post, :body)
+ # # => <label for="post_body">Write your entire text here</label>
+ #
+ # Localization can also be based purely on the translation of the attribute-name like this:
+ #
+ # activemodel:
+ # attribute:
+ # post:
+ # cost: "Total cost"
+ #
+ # label(:post, :cost)
+ # # => <label for="post_cost">Total cost</label>
+ #
# label(:post, :title, "A short title")
# # => <label for="post_title">A short title</label>
#
@@ -751,7 +775,16 @@ module ActionView
add_default_name_and_id_for_value(tag_value, name_and_id)
options.delete("index")
options["for"] ||= name_and_id["id"]
- content = (text.blank? ? nil : text.to_s) || method_name.humanize
+
+ content = if text.blank?
+ i18n_label = I18n.t("views.labels.#{object_name}.#{method_name}", :default => "")
+ i18n_label if i18n_label.present?
+ else
+ text.to_s
+ end
+ content ||= object.class.human_attribute_name(method_name) if object
+ content ||= method_name.humanize
+
label_tag(name_and_id["id"], content, options)
end