From ba49ab8f12e765aed375c3948a7e710bf522be82 Mon Sep 17 00:00:00 2001 From: Carsten Gehling Date: Tue, 22 Dec 2009 10:04:02 +0100 Subject: [PATCH 1/3] Label helper now supports I18n locale files --- actionpack/lib/action_view/helpers/form_helper.rb | 22 ++++++++++++++++++-- actionpack/lib/action_view/locale/en.yml | 8 +++++++ actionpack/test/lib/controller/fake_models.rb | 1 + actionpack/test/template/form_helper_test.rb | 12 +++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index d0c66ed..bbb6531 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..) 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 :value 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,16 @@ module ActionView # label(:post, :title) # # => # + # If you have the following defined in your locale (e.g. en.yml) + # views: + # labels: + # post: + # body: "Write your entire text here" + # + # label(:post, :body) + # # => + # + # # label(:post, :title, "A short title") # # => # @@ -751,7 +762,12 @@ 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 = text.blank? ? nil : text.to_s + content ||= I18n.t("views.labels.#{object_name}.#{method_name}", :raise => true) rescue nil + content ||= object.class.human_attribute_name(method_name) unless object.nil? + content ||= method_name.humanize + label_tag(name_and_id["id"], content, options) end diff --git a/actionpack/lib/action_view/locale/en.yml b/actionpack/lib/action_view/locale/en.yml index 5e2a92b..76d3511 100644 --- a/actionpack/lib/action_view/locale/en.yml +++ b/actionpack/lib/action_view/locale/en.yml @@ -110,8 +110,16 @@ other: "{{count}} errors prohibited this {{model}} from being saved" # The variable :count is also available body: "There were problems with the following fields:" + attributes: + post: + cost: "Total cost" support: select: # default value for :prompt => true in FormOptionsHelper prompt: "Please select" + + views: + labels: + post: + body: "Write entire text here" diff --git a/actionpack/test/lib/controller/fake_models.rb b/actionpack/test/lib/controller/fake_models.rb index 823de8b..b0e5d7a 100644 --- a/actionpack/test/lib/controller/fake_models.rb +++ b/actionpack/test/lib/controller/fake_models.rb @@ -54,6 +54,7 @@ end class Post < Struct.new(:title, :author_name, :body, :secret, :written_on, :cost) extend ActiveModel::Naming include ActiveModel::Conversion + extend ActiveModel::Translation alias_method :secret?, :secret diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 44734ab..d797049 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -51,6 +51,18 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal('', label(:post, :secret?)) end + def test_label_with_locales_strings + assert_dom_equal('', label("post", "body")) + end + + def test_label_with_human_attribute_name + assert_dom_equal('', label(:post, :cost)) + end + + def test_label_with_locales_symbols + assert_dom_equal('', label(:post, :body)) + end + def test_label_with_for_attribute_as_symbol assert_dom_equal('', label(:post, :title, nil, :for => "my_for")) end -- 1.6.3.3 From 44d8ea3837fbf71cd20ef65c9ddd3f376d7014a6 Mon Sep 17 00:00:00 2001 From: Carsten Gehling Date: Tue, 22 Dec 2009 10:23:29 +0100 Subject: [PATCH 2/3] Better documentation for I18n label helper --- actionpack/lib/action_view/helpers/form_helper.rb | 17 +++++++++++++++-- 1 files changed, 15 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index bbb6531..7d96463 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -514,15 +514,28 @@ module ActionView # label(:post, :title) # # => # - # If you have the following defined in your locale (e.g. en.yml) + # 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) - # # => + # # => + # + # 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(:post, :title, "A short title") # # => -- 1.6.3.3 From de3e66660359d34790eb503c4a1994646e2e1b0c Mon Sep 17 00:00:00 2001 From: Carsten Gehling Date: Tue, 22 Dec 2009 10:53:09 +0100 Subject: [PATCH 3/3] =?utf-8?q?Refactored=20to=20avoid=20using=20exceptions=20(thanks=20Jos=C3=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit --- actionpack/lib/action_view/helpers/form_helper.rb | 10 +++++++--- 1 files changed, 7 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 7d96463..9baa9f1 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -776,9 +776,13 @@ module ActionView options.delete("index") options["for"] ||= name_and_id["id"] - content = text.blank? ? nil : text.to_s - content ||= I18n.t("views.labels.#{object_name}.#{method_name}", :raise => true) rescue nil - content ||= object.class.human_attribute_name(method_name) unless object.nil? + 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) -- 1.6.3.3