Navigation Menu

Skip to content

Commit

Permalink
Support both conventions for translations for namespaced models.
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
John Firebaugh authored and tenderlove committed Mar 31, 2011
1 parent 1f38cd7 commit 5b8dbb0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
7 changes: 4 additions & 3 deletions activemodel/lib/active_model/naming.rb
Expand Up @@ -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,
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions activemodel/lib/active_model/translation.rb
Expand Up @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions activemodel/test/cases/translation_test.rb
Expand Up @@ -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

0 comments on commit 5b8dbb0

Please sign in to comment.