Permalink
Browse files

Translations for nested models.

  • Loading branch information...
tjtuom authored and mjonuschat committed May 23, 2010
1 parent 0a71ef6 commit 6b53be05e0ec77d2c1798f3a7cd14f3e48a13aed
Showing with 34 additions and 2 deletions.
  1. +10 −1 lib/formtastic.rb
  2. +3 −0 lib/formtastic/i18n.rb
  3. +21 −1 spec/i18n_spec.rb
View
@@ -1616,14 +1616,15 @@ def localized_string(key, value, type, options = {}) #:nodoc:
use_i18n = value.nil? ? @@i18n_lookups_by_default : (value != false)
if use_i18n
- model_name = self.model_name.underscore
+ 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
defaults = ::Formtastic::I18n::SCOPES.collect do |i18n_scope|
i18n_path = i18n_scope.dup
i18n_path.gsub!('{{action}}', action_name)
i18n_path.gsub!('{{model}}', model_name)
+ i18n_path.gsub!('{{nested_model}}', nested_model_name) unless nested_model_name.nil?
i18n_path.gsub!('{{attribute}}', attribute_name)
i18n_path.gsub!('..', '.')
i18n_path.to_sym
@@ -1641,6 +1642,14 @@ def model_name
@object.present? ? @object.class.name : @object_name.to_s.classify
end
+ def normalize_model_name(name)
+ if name =~ /(.+)\[(.+)\]/
+ [$1, $2]
+ else
+ [name]
+ end
+ end
+
def send_or_call(duck, object)
if duck.is_a?(Proc)
duck.call(object)
View
@@ -11,8 +11,11 @@ module I18n
:update => 'Update {{model}}'
}.freeze
SCOPES = [
+ '{{model}}.{{nested_model}}.{{action}}.{{attribute}}',
'{{model}}.{{action}}.{{attribute}}',
+ '{{model}}.{{nested_model}}.{{attribute}}',
'{{model}}.{{attribute}}',
+ '{{nested_model}}.{{attribute}}',
'{{attribute}}'
]
View
@@ -89,7 +89,8 @@
:labels => {
:title => "Hello world!",
:post => {:title => "Hello post!"},
- :project => {:title => "Hello project!"}
+ :project => {:title => "Hello project!", :task => {:name => "Hello task name!"}},
+ :line_item => {:name => "Hello line item name!"}
}
}
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
@@ -121,6 +122,25 @@
output_buffer.should have_tag("form label", /Hello project!/)
end
+ it 'should be able to translate nested objects with nested translations' do
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
+ builder.semantic_fields_for(:task) do |f|
+ concat(f.input(:name))
+ end
+ end
+ output_buffer.should have_tag("form label", /Hello task name!/)
+ end
+
+ it 'should be able to translated nested objects with top level translations' do
+ semantic_form_for(:order, :url => 'http://test.host') do |builder|
+ builder.semantic_fields_for(:line_item) do |f|
+ concat(f.input(:name))
+ end
+ end
+ output_buffer.should have_tag("form label", /Hello line item name!/)
+ end
+
+
# TODO: Add spec for namespaced models?
end

0 comments on commit 6b53be0

Please sign in to comment.