aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-01-07 02:45:50 +0100
committerJosé Valim <jose.valim@gmail.com>2010-01-07 15:31:50 +0100
commita25ef06956725f5c4c8833f86f61f1f12c4323e2 (patch)
tree0b5e91a6f83950e142758fe57ce768af057725b9 /activemodel
parentb0572ab2b670f00c96226d8ba29f4f153771e086 (diff)
downloadrails-a25ef06956725f5c4c8833f86f61f1f12c4323e2.tar.gz
rails-a25ef06956725f5c4c8833f86f61f1f12c4323e2.tar.bz2
rails-a25ef06956725f5c4c8833f86f61f1f12c4323e2.zip
Allow to specify default attributes names translation in I18n yml files.
For example, you could easily specify :created_at and :updated_at translations as: en: attributes: created_at: "Created at" updated_at: "Updated at" This configuration is built on ActiveModel, so it means those translations are shared between different ORMs as well (but always as a fallback).
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/translation.rb5
-rw-r--r--activemodel/test/cases/translation_test.rb5
2 files changed, 8 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/translation.rb b/activemodel/lib/active_model/translation.rb
index e5ef1e6114..2d2df269d0 100644
--- a/activemodel/lib/active_model/translation.rb
+++ b/activemodel/lib/active_model/translation.rb
@@ -25,13 +25,14 @@ module ActiveModel
# Specify +options+ with additional translating options.
def human_attribute_name(attribute, options = {})
defaults = lookup_ancestors.map do |klass|
- :"#{klass.model_name.underscore}.#{attribute}"
+ :"#{self.i18n_scope}.attributes.#{klass.model_name.underscore}.#{attribute}"
end
+ defaults << :"attributes.#{attribute}"
defaults << options.delete(:default) if options[:default]
defaults << attribute.to_s.humanize
- options.reverse_merge! :scope => [self.i18n_scope, :attributes], :count => 1, :default => defaults
+ options.reverse_merge! :count => 1, :default => defaults
I18n.translate(defaults.shift, options)
end
diff --git a/activemodel/test/cases/translation_test.rb b/activemodel/test/cases/translation_test.rb
index bfc1ca12e6..e25d308ca1 100644
--- a/activemodel/test/cases/translation_test.rb
+++ b/activemodel/test/cases/translation_test.rb
@@ -11,6 +11,11 @@ class ActiveModelI18nTests < ActiveModel::TestCase
I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } }
assert_equal 'person name attribute', Person.human_attribute_name('name')
end
+
+ def test_translated_model_attributes_with_default
+ I18n.backend.store_translations 'en', :attributes => { :name => 'name default attribute' }
+ assert_equal 'name default attribute', Person.human_attribute_name('name')
+ end
def test_translated_model_attributes_with_symbols
I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } }