From 119ccbdf7669489eaf200ade5272c1e9b29cdedc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 30 Sep 2010 08:12:45 +0200 Subject: [PATCH] Add SimpleForm.translate to turn off translations for labels, hint and placeholders in cases you are not using them. --- CHANGELOG.rdoc | 1 + lib/generators/simple_form/templates/simple_form.rb | 4 +++- lib/simple_form.rb | 5 +++++ lib/simple_form/components/labels.rb | 4 +--- lib/simple_form/inputs/base.rb | 9 +++++---- test/components/label_test.rb | 9 +++++++++ 6 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 3897adf4..3d29a0ff 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -8,6 +8,7 @@ * Add :search and :tel input types, with :tel mapping automatically from attributes matching "phone" - HTML5 * Add :required html attribute for required inputs - HTML5 * 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 * Search for validations on both association and attribute diff --git a/lib/generators/simple_form/templates/simple_form.rb b/lib/generators/simple_form/templates/simple_form.rb index 075f6772..e0a47306 100644 --- a/lib/generators/simple_form/templates/simple_form.rb +++ b/lib/generators/simple_form/templates/simple_form.rb @@ -1,6 +1,5 @@ # Use this setup block to configure all options available in SimpleForm. SimpleForm.setup do |config| - # 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. # config.components = [ :label_input, :hint, :error ] @@ -49,4 +48,7 @@ SimpleForm.setup do |config| # Default size for text inputs. # config.default_input_size = 50 + + # When true, do not use translations for labels, hints or placeholders. + # config.translate = true end diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 0d53f0ae..b544bff8 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -74,6 +74,11 @@ module SimpleForm mattr_accessor :default_input_size @@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 # to create a fresh initializer with all configuration values. def self.setup diff --git a/lib/simple_form/components/labels.rb b/lib/simple_form/components/labels.rb index 4d18878a..e8137927 100644 --- a/lib/simple_form/components/labels.rb +++ b/lib/simple_form/components/labels.rb @@ -54,13 +54,11 @@ module SimpleForm # First check human attribute name and then labels. 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) else attribute_name.to_s.humanize end - - translate(:labels, default) end end end diff --git a/lib/simple_form/inputs/base.rb b/lib/simple_form/inputs/base.rb index df3883d8..ba230601 100644 --- a/lib/simple_form/inputs/base.rb +++ b/lib/simple_form/inputs/base.rb @@ -121,19 +121,20 @@ module SimpleForm # # Take a look at our locale example file. def translate(namespace, default='') + return nil unless SimpleForm.translate lookups = [] lookups << :"#{object_name}.#{lookup_action}.#{reflection_or_attribute_name}" lookups << :"#{object_name}.#{reflection_or_attribute_name}" lookups << :"#{reflection_or_attribute_name}" lookups << default - I18n.t(lookups.shift, :scope => :"simple_form.#{namespace}", :default => lookups) + I18n.t(lookups.shift, :scope => :"simple_form.#{namespace}", :default => lookups).presence end # The action to be used in lookup. def lookup_action - return unless template.controller.action_name - - action = template.controller.action_name.to_sym + action = template.controller.action_name + return unless action + action = action.to_sym ACTIONS[action] || action end end diff --git a/test/components/label_test.rb b/test/components/label_test.rb index 157e82ae..ea0b0e76 100644 --- a/test/components/label_test.rb +++ b/test/components/label_test.rb @@ -83,6 +83,15 @@ class LabelTest < ActionView::TestCase 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 store_translations(:en, :simple_form => { :labels => { :user => { :company => 'My company!' }