Skip to content

Commit

Permalink
Merge pull request formtastic#586 from asanghi/i18n_584
Browse files Browse the repository at this point in the history
Fixes formtastic#584 . Retry if value received from i18n is not a string
  • Loading branch information
justinfrench committed Jun 1, 2011
2 parents 382b283 + 43153bc commit 8f1e885
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/formtastic/localized_string.rb
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
9 changes: 9 additions & 0 deletions spec/i18n_spec.rb
Expand Up @@ -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|
Expand Down

0 comments on commit 8f1e885

Please sign in to comment.