Skip to content
This repository
Browse code

Merge pull request #1562 from sikachu/3-1-i18n-convention

Brought back alternative convention for namespaced models in i18n.
  • Loading branch information...
commit a40833cad601f866b05e308b87fc12965e1f343d 2 parents abc029d + 3451bcb
José Valim authored
7  activemodel/lib/active_model/naming.rb
@@ -21,7 +21,7 @@ def initialize(klass, namespace = nil, name = nil)
21 21
       @partial_path = "#{@collection}/#{@element}".freeze
22 22
       @param_key = (namespace ? _singularize(@unnamespaced) : @singular).freeze
23 23
       @route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural).freeze
24  
-      @i18n_key = self.underscore.to_sym
  24
+      @i18n_key = self.underscore.tr('/', '.').to_sym
25 25
     end
26 26
 
27 27
     # Transform the model name into a more humane format, using I18n. By default,
@@ -35,8 +35,9 @@ def human(options={})
35 35
                            @klass.respond_to?(:i18n_scope)
36 36
 
37 37
       defaults = @klass.lookup_ancestors.map do |klass|
38  
-        klass.model_name.i18n_key
39  
-      end
  38
+        [klass.model_name.i18n_key,
  39
+         klass.model_name.i18n_key.to_s.tr('.', '/').to_sym]
  40
+      end.flatten
40 41
 
41 42
       defaults << options[:default] if options[:default]
42 43
       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
@@ -76,5 +76,15 @@ def test_human_does_not_modify_options
76 76
     Person.model_name.human(options)
77 77
     assert_equal({:default => 'person model'}, options)
78 78
   end
  79
+  
  80
+  def test_alternate_namespaced_model_attribute_translation
  81
+    I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:gender => {:attribute => 'person gender attribute'}}}}
  82
+    assert_equal 'person gender attribute', Person::Gender.human_attribute_name('attribute')
  83
+  end
  84
+
  85
+  def test_alternate_namespaced_model_translation
  86
+    I18n.backend.store_translations 'en', :activemodel => {:models => {:person => {:gender => 'person gender model'}}}
  87
+    assert_equal 'person gender model', Person::Gender.model_name.human
  88
+  end
79 89
 end
80 90
 

0 notes on commit a40833c

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