Skip to content
This repository
Browse code

Support both conventions for translations for namespaced models.

3.0.0 - 3.0.1 required 'namespace/model'
3.0.2 - 3.0.5 required 'namespace.model' (nested). It has the advantage of
keeping the i18n file DRY when multiple models are in the same namespace,
but can lead to translation key conflicts if models are nested within
models.

[#6448, #5572]
  • Loading branch information...
commit 5b8dbb0eee0a3277ef51e4bf31555ef27410272f 1 parent 1f38cd7
authored March 30, 2011 tenderlove committed March 30, 2011
7  activemodel/lib/active_model/naming.rb
@@ -14,7 +14,7 @@ def initialize(klass)
14 14
       @human = ActiveSupport::Inflector.humanize(@element).freeze
15 15
       @collection = ActiveSupport::Inflector.tableize(self).freeze
16 16
       @partial_path = "#{@collection}/#{@element}".freeze
17  
-      @i18n_key = self.underscore.to_sym
  17
+      @i18n_key = ActiveSupport::Inflector.underscore(self).tr('/', '.').to_sym
18 18
     end
19 19
 
20 20
     # Transform the model name into a more humane format, using I18n. By default,
@@ -28,8 +28,9 @@ def human(options={})
28 28
                            @klass.respond_to?(:i18n_scope)
29 29
 
30 30
       defaults = @klass.lookup_ancestors.map do |klass|
31  
-        klass.model_name.i18n_key
32  
-      end
  31
+        [klass.model_name.i18n_key,
  32
+         klass.model_name.i18n_key.to_s.tr('.', '/').to_sym]
  33
+      end.flatten
33 34
 
34 35
       defaults << options.delete(:default) if options[:default]
35 36
       defaults << @human
5  activemodel/lib/active_model/translation.rb
@@ -44,8 +44,9 @@ def lookup_ancestors
44 44
     # Specify +options+ with additional translating options.
45 45
     def human_attribute_name(attribute, options = {})
46 46
       defaults = lookup_ancestors.map do |klass|
47  
-        :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}"
48  
-      end
  47
+        [:"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}",
  48
+         :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key.to_s.tr('.', '/')}.#{attribute}"]
  49
+      end.flatten
49 50
 
50 51
       defaults << :"attributes.#{attribute}"
51 52
       defaults << options.delete(:default) if options[:default]
10  activemodel/test/cases/translation_test.rb
@@ -53,5 +53,15 @@ def test_translated_model_names_with_ancestors_fallback
53 53
     I18n.backend.store_translations 'en', :activemodel => {:models => {:person => 'person model'} }
54 54
     assert_equal 'person model', Child.model_name.human
55 55
   end
  56
+
  57
+  def test_alternate_namespaced_model_attribute_translation
  58
+    I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:gender => {:attribute => 'person gender attribute'}}}}
  59
+    assert_equal 'person gender attribute', Person::Gender.human_attribute_name('attribute')
  60
+  end
  61
+
  62
+  def test_alternate_namespaced_model_translation
  63
+    I18n.backend.store_translations 'en', :activemodel => {:models => {:person => {:gender => 'person gender model'}}}
  64
+    assert_equal 'person gender model', Person::Gender.model_name.human
  65
+  end
56 66
 end
57 67
 

0 notes on commit 5b8dbb0

Please sign in to comment.
Something went wrong with that request. Please try again.