Skip to content

Commit 0307c53

Browse files
John Firebaughtenderlove
authored andcommitted
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. [rails#6448, rails#5572]
1 parent d6dbd54 commit 0307c53

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

activemodel/lib/active_model/naming.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def initialize(klass)
1414
@human = ActiveSupport::Inflector.humanize(@element).freeze
1515
@collection = ActiveSupport::Inflector.tableize(self).freeze
1616
@partial_path = "#{@collection}/#{@element}".freeze
17-
@i18n_key = self.underscore.to_sym
17+
@i18n_key = ActiveSupport::Inflector.underscore(self).tr('/', '.').to_sym
1818
end
1919

2020
# Transform the model name into a more humane format, using I18n. By default,
@@ -28,8 +28,9 @@ def human(options={})
2828
@klass.respond_to?(:i18n_scope)
2929

3030
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
3334

3435
defaults << options.delete(:default) if options[:default]
3536
defaults << @human

activemodel/lib/active_model/translation.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ def lookup_ancestors
4444
# Specify +options+ with additional translating options.
4545
def human_attribute_name(attribute, options = {})
4646
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
4950

5051
defaults << :"attributes.#{attribute}"
5152
defaults << options.delete(:default) if options[:default]

activemodel/test/cases/translation_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,15 @@ def test_translated_model_names_with_ancestors_fallback
5353
I18n.backend.store_translations 'en', :activemodel => {:models => {:person => 'person model'} }
5454
assert_equal 'person model', Child.model_name.human
5555
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
5666
end
5767

0 commit comments

Comments
 (0)