Add SimpleForm.translate to turn off translations for labels, hint and placeholders in cases you are not using them.

This commit is contained in:
José Valim 2010-09-30 08:12:45 +02:00
parent 188cd008ae
commit 119ccbdf76
6 changed files with 24 additions and 8 deletions

View File

@ -8,6 +8,7 @@
* Add :search and :tel input types, with :tel mapping automatically from attributes matching "phone" - HTML5 * Add :search and :tel input types, with :tel mapping automatically from attributes matching "phone" - HTML5
* Add :required html attribute for required inputs - HTML5 * Add :required html attribute for required inputs - HTML5
* Add optional :components option to input to control component rendering (by github.com/khoan) * Add optional :components option to input to control component rendering (by github.com/khoan)
* Add SimpleForm.translate as an easy way to turn off SimpleForm internal translations
* bug fix * bug fix
* Search for validations on both association and attribute * Search for validations on both association and attribute

View File

@ -1,6 +1,5 @@
# Use this setup block to configure all options available in SimpleForm. # Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config| SimpleForm.setup do |config|
# Components used by the form builder to generate a complete input. You can remove # Components used by the form builder to generate a complete input. You can remove
# any of them, change the order, or even add your own components to the stack. # any of them, change the order, or even add your own components to the stack.
# config.components = [ :label_input, :hint, :error ] # config.components = [ :label_input, :hint, :error ]
@ -49,4 +48,7 @@ SimpleForm.setup do |config|
# Default size for text inputs. # Default size for text inputs.
# config.default_input_size = 50 # config.default_input_size = 50
# When true, do not use translations for labels, hints or placeholders.
# config.translate = true
end end

View File

@ -74,6 +74,11 @@ module SimpleForm
mattr_accessor :default_input_size mattr_accessor :default_input_size
@@default_input_size = 50 @@default_input_size = 50
# When off, do not use translations in hint and labels.
# It is a small performance improvement if you are not such features.
mattr_accessor :translate
@@translate = true
# Default way to setup SimpleForm. Run rails generate simple_form:install # Default way to setup SimpleForm. Run rails generate simple_form:install
# to create a fresh initializer with all configuration values. # to create a fresh initializer with all configuration values.
def self.setup def self.setup

View File

@ -54,13 +54,11 @@ module SimpleForm
# First check human attribute name and then labels. # First check human attribute name and then labels.
def label_translation #:nodoc: def label_translation #:nodoc:
default = if object.class.respond_to?(:human_attribute_name) translate(:labels) || if object.class.respond_to?(:human_attribute_name)
object.class.human_attribute_name(reflection_or_attribute_name.to_s) object.class.human_attribute_name(reflection_or_attribute_name.to_s)
else else
attribute_name.to_s.humanize attribute_name.to_s.humanize
end end
translate(:labels, default)
end end
end end
end end

View File

@ -121,19 +121,20 @@ module SimpleForm
# #
# Take a look at our locale example file. # Take a look at our locale example file.
def translate(namespace, default='') def translate(namespace, default='')
return nil unless SimpleForm.translate
lookups = [] lookups = []
lookups << :"#{object_name}.#{lookup_action}.#{reflection_or_attribute_name}" lookups << :"#{object_name}.#{lookup_action}.#{reflection_or_attribute_name}"
lookups << :"#{object_name}.#{reflection_or_attribute_name}" lookups << :"#{object_name}.#{reflection_or_attribute_name}"
lookups << :"#{reflection_or_attribute_name}" lookups << :"#{reflection_or_attribute_name}"
lookups << default lookups << default
I18n.t(lookups.shift, :scope => :"simple_form.#{namespace}", :default => lookups) I18n.t(lookups.shift, :scope => :"simple_form.#{namespace}", :default => lookups).presence
end end
# The action to be used in lookup. # The action to be used in lookup.
def lookup_action def lookup_action
return unless template.controller.action_name action = template.controller.action_name
return unless action
action = template.controller.action_name.to_sym action = action.to_sym
ACTIONS[action] || action ACTIONS[action] || action
end end
end end

View File

@ -83,6 +83,15 @@ class LabelTest < ActionView::TestCase
end end
end end
test 'input should not use i18n label if translate is false' do
swap SimpleForm, :translate => false do
store_translations(:en, :simple_form => { :labels => { :age => 'Idade' } } ) do
with_label_for @user, :age, :integer
assert_select 'label[for=user_age]', /Age/
end
end
end
test 'label should use i18n with lookup for association name' do test 'label should use i18n with lookup for association name' do
store_translations(:en, :simple_form => { :labels => { store_translations(:en, :simple_form => { :labels => {
:user => { :company => 'My company!' } :user => { :company => 'My company!' }