Permalink
Browse files

Fixes #584 . Retry if value received from i18n is not a string

  • Loading branch information...
1 parent 382b283 commit 43153bcf550d587ad0f7de89f119611f52c7834b Aditya Sanghi committed Jun 1, 2011
Showing with 16 additions and 7 deletions.
  1. +7 −7 lib/formtastic/localized_string.rb
  2. +9 −0 spec/i18n_spec.rb
@@ -35,7 +35,7 @@ def localized_string(key, value, type, options = {}) #:nodoc:
if use_i18n
model_name, nested_model_name = normalize_model_name(self.model_name.underscore)
-
+
action_name = template.params[:action].to_s rescue ''
attribute_name = key.to_s
@@ -57,14 +57,14 @@ def localized_string(key, value, type, options = {}) #:nodoc:
default_key = defaults.shift
i18n_value = Formtastic::I18n.t(default_key,
options.merge(:default => defaults, :scope => type.to_s.pluralize.to_sym))
+ i18n_value = i18n_value.is_a?(::String) ? i18n_value : nil
if i18n_value.blank? && type == :label
# This is effectively what Rails label helper does for i18n lookup
options[:scope] = [:helpers, type]
options[:default] = defaults
i18n_value = ::I18n.t(default_key, options)
end
- i18n_value = escape_html_entities(i18n_value) if i18n_value.is_a?(::String)
- i18n_value.blank? ? nil : i18n_value
+ (i18n_value.is_a?(::String) && i18n_value.present?) ? escape_html_entities(i18n_value) : nil
end
end
end
@@ -88,18 +88,18 @@ def normalize_model_name(name)
[name]
end
end
-
+
def escape_html_entities(string) #:nodoc:
- if (respond_to?(:builder) && builder.escape_html_entities_in_hints_and_labels) ||
+ if (respond_to?(:builder) && builder.escape_html_entities_in_hints_and_labels) ||
(self.respond_to?(:escape_html_entities_in_hints_and_labels) && escape_html_entities_in_hints_and_labels)
string = template.escape_once(string) unless string.respond_to?(:html_safe?) && string.html_safe? == true # Acceppt html_safe flag as indicator to skip escaping
end
string
end
-
+
def i18n_lookups_by_default
respond_to?(:builder) ? builder.i18n_lookups_by_default : i18n_lookups_by_default
end
-
+
end
end
View
@@ -130,6 +130,15 @@
end
end
+ it "should be able to translate when method name is same as model" do
+ with_config :i18n_lookups_by_default, true do
+ concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
+ concat(builder.input(:author))
+ end)
+ output_buffer.should have_tag("form label", /Author/)
+ end
+ end
+
it 'should be able to translate nested objects with nested translations' do
with_config :i18n_lookups_by_default, true do
concat(semantic_form_for(@new_post) do |builder|

0 comments on commit 43153bc

Please sign in to comment.