Skip to content
This repository has been archived by the owner on Aug 4, 2018. It is now read-only.

Commit

Permalink
use translation in helpers.label
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmike committed Sep 26, 2013
1 parent e55ee46 commit 5691608
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/bootstrap_forms/helpers/wrappers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,26 @@ def error_string
end

def human_attribute_name
object.class.human_attribute_name(@name) rescue @name.to_s.titleize
object_name = @object_name.to_s.dup

# Copied from https://github.com/rails/rails/blob/fb24f419051aec00790a8e6fb5785eeb6f503f4a/actionpack/lib/action_view/helpers/tags/label.rb#L38
object_name.gsub!(/\[(.*)_attributes\]\[\d\]/, '.\1')
method = @name.to_s
if object.respond_to?(:to_model)
key = object.class.model_name.i18n_key
i18n_default = ["#{key}.#{method}".to_sym, ""]
end

i18n_default ||= ""
result = I18n.t("#{object_name}.#{method}", :default => i18n_default, :scope => "helpers.label").presence

result ||= if object && object.class.respond_to?(:human_attribute_name)
object.class.human_attribute_name(method)
end

result ||= method.humanize

result
end

def input_div(&block)
Expand Down
8 changes: 8 additions & 0 deletions spec/support/shared_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
it 'allows symbol as field name' do
@builder.check_box(:name).should == @builder.check_box('name')
end

context "with helper translations" do
before(:all) { I18n.backend.store_translations I18n.locale, {:helpers => {:label => {"item" => {"name" => "name translation"}}}} }
after(:all) { I18n.backend.reload! }
it 'uses helper translation' do
@builder.check_box(:name).should include("name translation")
end
end
end

describe 'radio_buttons' do
Expand Down

0 comments on commit 5691608

Please sign in to comment.