Permalink
Browse files

fallback on helpers.label if formtastic i18n not found

  • Loading branch information...
1 parent 7ef2cb1 commit 41b7b3b1c1d0582e76b985cdfaeb6b6897d8a0a0 Aditya Sanghi committed with Oct 13, 2010
Showing with 25 additions and 5 deletions.
  1. +11 −2 lib/formtastic.rb
  2. +14 −3 spec/i18n_spec.rb
View
@@ -1746,7 +1746,7 @@ def localized_string(key, value, type, options = {}) #:nodoc:
attribute_name = key.to_s
defaults = ::Formtastic::I18n::SCOPES.reject do |i18n_scope|
- nested_model_name.nil? && i18n_scope.to_s.match(/\{nested_model\}/)
+ nested_model_name.nil? && i18n_scope.match(/nested_model/)
end.collect do |i18n_scope|
i18n_path = i18n_scope.dup
i18n_path.gsub!('%{action}', action_name)
@@ -1758,8 +1758,17 @@ def localized_string(key, value, type, options = {}) #:nodoc:
end
defaults << ''
- i18n_value = ::Formtastic::I18n.t(defaults.shift,
+ defaults.uniq!
+
+ default_key = defaults.shift
+ i18n_value = ::Formtastic::I18n.t(default_key,
options.merge(:default => defaults, :scope => type.to_s.pluralize.to_sym))
+ 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
end
View
@@ -85,14 +85,18 @@
@output_buffer = ''
mock_everything
- ::I18n.backend.store_translations :en, :formtastic => {
+ ::I18n.backend.store_translations :en, {:formtastic => {
:labels => {
:title => "Hello world!",
:post => {:title => "Hello post!"},
:project => {:title => "Hello project!", :task => {:name => "Hello task name!"}},
:line_item => {:name => "Hello line item name!"}
}
- }
+ }, :helpers => {
+ :label => {
+ :post => {:author => "Written by"}
+ }
+ }}
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
@new_post.stub!(:title)
@@ -134,7 +138,7 @@
output_buffer.should have_tag("form label", /Hello task name!/)
end
- it 'should be able to translated nested objects with top level translations' do
+ it 'should be able to translate nested objects with top level translations' do
form = semantic_form_for(:order, :url => 'http://test.host') do |builder|
builder.semantic_fields_for(:line_item) do |f|
concat(f.input(:name))
@@ -144,6 +148,13 @@
output_buffer.should have_tag("form label", /Hello line item name!/)
end
+ it 'should be able to translate helper label as Rails does' do
+ form = semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:author))
+ end
+ output_buffer.concat(form) if Formtastic::Util.rails3?
+ output_buffer.should have_tag("form label", /Written by/)
+ end
# TODO: Add spec for namespaced models?

0 comments on commit 41b7b3b

Please sign in to comment.