From 5b8dbb0eee0a3277ef51e4bf31555ef27410272f Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 30 Mar 2011 17:50:50 -0700 Subject: [PATCH] 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] --- activemodel/lib/active_model/naming.rb | 7 ++++--- activemodel/lib/active_model/translation.rb | 5 +++-- activemodel/test/cases/translation_test.rb | 10 ++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 53d604eccf9ed..0984c4544bc03 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -14,7 +14,7 @@ def initialize(klass) @human = ActiveSupport::Inflector.humanize(@element).freeze @collection = ActiveSupport::Inflector.tableize(self).freeze @partial_path = "#{@collection}/#{@element}".freeze - @i18n_key = self.underscore.to_sym + @i18n_key = ActiveSupport::Inflector.underscore(self).tr('/', '.').to_sym end # Transform the model name into a more humane format, using I18n. By default, @@ -28,8 +28,9 @@ def human(options={}) @klass.respond_to?(:i18n_scope) defaults = @klass.lookup_ancestors.map do |klass| - klass.model_name.i18n_key - end + [klass.model_name.i18n_key, + klass.model_name.i18n_key.to_s.tr('.', '/').to_sym] + end.flatten defaults << options.delete(:default) if options[:default] defaults << @human diff --git a/activemodel/lib/active_model/translation.rb b/activemodel/lib/active_model/translation.rb index b9237143f9a62..e6978c3ff020f 100644 --- a/activemodel/lib/active_model/translation.rb +++ b/activemodel/lib/active_model/translation.rb @@ -44,8 +44,9 @@ def lookup_ancestors # Specify +options+ with additional translating options. def human_attribute_name(attribute, options = {}) defaults = lookup_ancestors.map do |klass| - :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}" - end + [:"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}", + :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key.to_s.tr('.', '/')}.#{attribute}"] + end.flatten defaults << :"attributes.#{attribute}" defaults << options.delete(:default) if options[:default] diff --git a/activemodel/test/cases/translation_test.rb b/activemodel/test/cases/translation_test.rb index 1db63e1519b26..1861f683e66cc 100644 --- a/activemodel/test/cases/translation_test.rb +++ b/activemodel/test/cases/translation_test.rb @@ -53,5 +53,15 @@ def test_translated_model_names_with_ancestors_fallback I18n.backend.store_translations 'en', :activemodel => {:models => {:person => 'person model'} } assert_equal 'person model', Child.model_name.human end + + def test_alternate_namespaced_model_attribute_translation + I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:gender => {:attribute => 'person gender attribute'}}}} + assert_equal 'person gender attribute', Person::Gender.human_attribute_name('attribute') + end + + def test_alternate_namespaced_model_translation + I18n.backend.store_translations 'en', :activemodel => {:models => {:person => {:gender => 'person gender model'}}} + assert_equal 'person gender model', Person::Gender.model_name.human + end end