Allow I18n lookup for labels and hints based on the current action
This commit is contained in:
parent
8894d40326
commit
233e1df4fa
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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...'
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -49,6 +49,10 @@ class ActionView::TestCase
|
|||
false
|
||||
end
|
||||
|
||||
def params
|
||||
@params ||= {}
|
||||
end
|
||||
|
||||
def user_path(*args)
|
||||
'/users'
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue