diff --git a/TODO.rdoc b/TODO.rdoc index 219beb7f..1b65358e 100644 --- a/TODO.rdoc +++ b/TODO.rdoc @@ -1,8 +1,6 @@ == General -* Allow I18n lookup for labels and hints based on the current action * Sample CSS -* Test forms with non objects * Get default string options from column definition * Add support to default :prompt methods on datetime inputs * Add support to default label method diff --git a/lib/simple_form/components/base.rb b/lib/simple_form/components/base.rb index 9366a506..699d2c04 100644 --- a/lib/simple_form/components/base.rb +++ b/lib/simple_form/components/base.rb @@ -48,7 +48,12 @@ module SimpleForm end def translate(default='') - lookups = [ :"#{object_name}.#{attribute}", :"#{attribute}", default ] + action = template.params[:action] if template.respond_to?(:params) + lookups = [] + lookups << :"#{object_name}.#{action}.#{attribute}" if action.present? + lookups << :"#{object_name}.#{attribute}" + lookups << :"#{attribute}" + lookups << default I18n.t(lookups.shift, :scope => :"simple_form.#{basename.to_s.pluralize}", :default => lookups) end end diff --git a/test/components/hint_test.rb b/test/components/hint_test.rb index 7490c3ba..9b72a39b 100644 --- a/test/components/hint_test.rb +++ b/test/components/hint_test.rb @@ -38,9 +38,19 @@ class ErrorTest < ActionView::TestCase end end + test 'hint should use i18n based on model, action, and attribute to lookup translation' do + store_translations(:en, :simple_form => { :hints => { :user => { + :edit => { :name => 'Content of this input will be truncated...' } + } } }) do + params.merge!(:action => 'edit') + with_hint_for @user, :name, :string + assert_select 'span.hint', 'Content of this input will be truncated...' + end + end + test 'hint should use i18n with model and attribute to lookup translation' do - store_translations(:en, :simple_form => { :hints => { :user => { :name => - 'Content of this input will be capitalized...' + store_translations(:en, :simple_form => { :hints => { :user => { + :name => 'Content of this input will be capitalized...' } } }) do with_hint_for @user, :name, :string assert_select 'span.hint', 'Content of this input will be capitalized...' @@ -48,8 +58,8 @@ class ErrorTest < ActionView::TestCase end test 'hint should use i18n just with attribute to lookup translation' do - store_translations(:en, :simple_form => { :hints => { :name => - 'Content of this input will be downcased...' + store_translations(:en, :simple_form => { :hints => { + :name => 'Content of this input will be downcased...' } }) do with_hint_for @user, :name, :string assert_select 'span.hint', 'Content of this input will be downcased...' diff --git a/test/components/label_test.rb b/test/components/label_test.rb index 8ff1b971..6b52f544 100644 --- a/test/components/label_test.rb +++ b/test/components/label_test.rb @@ -41,6 +41,16 @@ class LabelTest < ActionView::TestCase assert_select 'label[for=user_description]', /User Description!/ end + test 'label should use i18n based on model, action, and attribute to lookup translation' do + store_translations(:en, :simple_form => { :labels => { :user => { + :new => { :description => 'Nova descrição' } + } } } ) do + params.merge!(:action => 'new') + with_label_for @user, :description, :text + assert_select 'label[for=user_description]', /Nova descrição/ + end + end + test 'label should use i18n based on model and attribute to lookup translation' do store_translations(:en, :simple_form => { :labels => { :user => { :description => 'Descrição' diff --git a/test/test_helper.rb b/test/test_helper.rb index ecbf8273..e530e3ee 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -49,6 +49,10 @@ class ActionView::TestCase false end + def params + @params ||= {} + end + def user_path(*args) '/users' end