Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
John Firebaugh authored tenderlove committed
View
7 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
View
5 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]
View
10 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
Please sign in to comment.
Something went wrong with that request. Please try again.