From bdf783b5a8b83b1d565027130de4743fda336523 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Sun, 10 Aug 2008 21:25:24 +0200 Subject: update i18n usage for pluralization hashes (api change) --- actionpack/lib/action_view/locale/en-US.rb | 57 +++++-- actionpack/test/template/date_helper_i18n_test.rb | 22 +++ activerecord/lib/active_record/validations.rb | 2 +- activerecord/test/cases/validations_i18n_test.rb | 198 ++++++++++++++++++++-- 4 files changed, 256 insertions(+), 23 deletions(-) diff --git a/actionpack/lib/action_view/locale/en-US.rb b/actionpack/lib/action_view/locale/en-US.rb index 2c3676dca8..0119b3d9c1 100644 --- a/actionpack/lib/action_view/locale/en-US.rb +++ b/actionpack/lib/action_view/locale/en-US.rb @@ -1,17 +1,47 @@ I18n.backend.store_translations :'en-US', { :datetime => { :distance_in_words => { - :half_a_minute => 'half a minute', - :less_than_x_seconds => ['less than 1 second', 'less than {{count}} seconds'], - :x_seconds => ['1 second', '{{count}} seconds'], - :less_than_x_minutes => ['less than a minute', 'less than {{count}} minutes'], - :x_minutes => ['1 minute', '{{count}} minutes'], - :about_x_hours => ['about 1 hour', 'about {{count}} hours'], - :x_days => ['1 day', '{{count}} days'], - :about_x_months => ['about 1 month', 'about {{count}} months'], - :x_months => ['1 month', '{{count}} months'], - :about_x_years => ['about 1 year', 'about {{count}} year'], - :over_x_years => ['over 1 year', 'over {{count}} years'] + :half_a_minute => 'half a minute', + :less_than_x_seconds => { + :one => 'less than 1 second', + :many => 'less than {{count}} seconds' + }, + :x_seconds => { + :one => '1 second', + :many => '{{count}} seconds' + }, + :less_than_x_minutes => { + :one => 'less than a minute', + :many => 'less than {{count}} minutes' + }, + :x_minutes => { + :one => '1 minute', + :many => '{{count}} minutes' + }, + :about_x_hours => { + :one => 'about 1 hour', + :many => 'about {{count}} hours' + }, + :x_days => { + :one => '1 day', + :many => '{{count}} days' + }, + :about_x_months => { + :one => 'about 1 month', + :many => 'about {{count}} months' + }, + :x_months => { + :one => '1 month', + :many => '{{count}} months' + }, + :about_x_years => { + :one => 'about 1 year', + :many => 'about {{count}} years' + }, + :over_x_years => { + :one => 'over 1 year', + :many => 'over {{count}} years' + } } }, :number => { @@ -46,7 +76,10 @@ I18n.backend.store_translations :'en-US', { }, :active_record => { :error => { - :header_message => ["1 error prohibited this {{object_name}} from being saved", "{{count}} errors prohibited this {{object_name}} from being saved"], + :header_message => { + :one => "1 error prohibited this {{object_name}} from being saved", + :many => "{{count}} errors prohibited this {{object_name}} from being saved" + }, :message => "There were problems with the following fields:" } } diff --git a/actionpack/test/template/date_helper_i18n_test.rb b/actionpack/test/template/date_helper_i18n_test.rb index 2b40074498..bf3b2588c8 100644 --- a/actionpack/test/template/date_helper_i18n_test.rb +++ b/actionpack/test/template/date_helper_i18n_test.rb @@ -47,6 +47,28 @@ class DateHelperDistanceOfTimeInWordsI18nTests < Test::Unit::TestCase I18n.expects(:t).with(key, options) distance_of_time_in_words(@from, to, include_seconds, :locale => 'en-US') end + + def test_distance_of_time_pluralizations + { [:'less_than_x_seconds', 1] => 'less than 1 second', + [:'less_than_x_seconds', 2] => 'less than 2 seconds', + [:'less_than_x_minutes', 1] => 'less than a minute', + [:'less_than_x_minutes', 2] => 'less than 2 minutes', + [:'x_minutes', 1] => '1 minute', + [:'x_minutes', 2] => '2 minutes', + [:'about_x_hours', 1] => 'about 1 hour', + [:'about_x_hours', 2] => 'about 2 hours', + [:'x_days', 1] => '1 day', + [:'x_days', 2] => '2 days', + [:'about_x_years', 1] => 'about 1 year', + [:'about_x_years', 2] => 'about 2 years', + [:'over_x_years', 1] => 'over 1 year', + [:'over_x_years', 2] => 'over 2 years' + + }.each do |args, expected| + key, count = *args + assert_equal expected, I18n.t(key, :count => count, :scope => 'datetime.distance_in_words') + end + end end end diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index e7a9676394..0de430567c 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -662,7 +662,7 @@ module ActiveRecord finder_class.with_exclusive_scope do if finder_class.exists?([condition_sql, *condition_params]) - message = record.errors.generate_message(attr_name, :taken, :default => configuration[:message]) + message = record.errors.generate_message(attr_name, :taken, :default => configuration[:message], :value => value) record.errors.add(attr_name, message) end end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index 86834fe920..89ca61a220 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -45,38 +45,38 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase def test_errors_generate_message_translates_custom_model_attribute_key global_scope = [:active_record, :error_messages] custom_scope = global_scope + [:custom, 'topic', :title] - + I18n.expects(:t).with nil, :scope => [:active_record, :error_messages], :default => [:"custom.topic.title.invalid", 'default from class def', :invalid] @topic.errors.generate_message :title, :invalid, :default => 'default from class def' end - + def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti custom_scope = [:active_record, :error_messages, :custom, 'topic', :title] - + I18n.expects(:t).with nil, :scope => [:active_record, :error_messages], :default => [:"custom.reply.title.invalid", :"custom.topic.title.invalid", 'default from class def', :invalid] Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def' end - + def test_errors_add_on_empty_generates_message @topic.errors.expects(:generate_message).with(:title, :empty, {:default => nil}) @topic.errors.add_on_empty :title end - + def test_errors_add_on_empty_generates_message_with_custom_default_message @topic.errors.expects(:generate_message).with(:title, :empty, {:default => 'custom'}) @topic.errors.add_on_empty :title, 'custom' end - + def test_errors_add_on_blank_generates_message @topic.errors.expects(:generate_message).with(:title, :blank, {:default => nil}) @topic.errors.add_on_blank :title end - + def test_errors_add_on_blank_generates_message_with_custom_default_message @topic.errors.expects(:generate_message).with(:title, :blank, {:default => 'custom'}) @topic.errors.add_on_blank :title, 'custom' end - + def test_errors_full_messages_translates_human_attribute_name_for_model_attributes @topic.errors.instance_variable_set :@errors, { 'title' => 'empty' } I18n.expects(:translate).with(:"active_record.human_attribute_names.topic.title", :locale => 'en-US', :default => 'Title').returns('Title') @@ -203,14 +203,14 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase def test_validates_uniqueness_of_generates_message Topic.validates_uniqueness_of :title @topic.title = unique_topic.title - @topic.errors.expects(:generate_message).with(:title, :taken, {:default => nil}) + @topic.errors.expects(:generate_message).with(:title, :taken, {:default => nil, :value => 'unique!'}) @topic.valid? end def test_validates_uniqueness_of_generates_message_with_custom_default_message Topic.validates_uniqueness_of :title, :message => 'custom' @topic.title = unique_topic.title - @topic.errors.expects(:generate_message).with(:title, :taken, {:default => 'custom'}) + @topic.errors.expects(:generate_message).with(:title, :taken, {:default => 'custom', :value => 'unique!'}) @topic.valid? end @@ -620,4 +620,182 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase replied_topic.valid? assert_equal 'global message', replied_topic.errors.on(:replies) end +end + +class ActiveRecordValidationsGenerateMessageI18nTests < Test::Unit::TestCase + def setup + reset_callbacks Topic + @topic = Topic.new + I18n.backend.store_translations :'en-US', { + :active_record => { + :error_messages => { + :inclusion => "is not included in the list", + :exclusion => "is reserved", + :invalid => "is invalid", + :confirmation => "doesn't match confirmation", + :accepted => "must be accepted", + :empty => "can't be empty", + :blank => "can't be blank", + :too_long => "is too long (maximum is {{count}} characters)", + :too_short => "is too short (minimum is {{count}} characters)", + :wrong_length => "is the wrong length (should be {{count}} characters)", + :taken => "has already been taken", + :not_a_number => "is not a number", + :greater_than => "must be greater than {{count}}", + :greater_than_or_equal_to => "must be greater than or equal to {{count}}", + :equal_to => "must be equal to {{count}}", + :less_than => "must be less than {{count}}", + :less_than_or_equal_to => "must be less than or equal to {{count}}", + :odd => "must be odd", + :even => "must be even" + } + } + } + end + + def reset_callbacks(*models) + models.each do |model| + model.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new) + model.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new) + model.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new) + end + end + + # validates_inclusion_of: generate_message(attr_name, :inclusion, :default => configuration[:message], :value => value) + def test_generate_message_inclusion_with_default_message + assert_equal 'is not included in the list', @topic.errors.generate_message(:title, :inclusion, :default => nil, :value => 'title') + end + + def test_generate_message_inclusion_with_custom_message + assert_equal 'custom message title', @topic.errors.generate_message(:title, :inclusion, :default => 'custom message {{value}}', :value => 'title') + end + + # validates_exclusion_of: generate_message(attr_name, :exclusion, :default => configuration[:message], :value => value) + def test_generate_message_exclusion_with_default_message + assert_equal 'is reserved', @topic.errors.generate_message(:title, :exclusion, :default => nil, :value => 'title') + end + + def test_generate_message_exclusion_with_custom_message + assert_equal 'custom message title', @topic.errors.generate_message(:title, :exclusion, :default => 'custom message {{value}}', :value => 'title') + end + + # validates_associated: generate_message(attr_name, :invalid, :default => configuration[:message], :value => value) + # validates_format_of: generate_message(attr_name, :invalid, :default => configuration[:message], :value => value) + def test_generate_message_invalid_with_default_message + assert_equal 'is invalid', @topic.errors.generate_message(:title, :invalid, :default => nil, :value => 'title') + end + + def test_generate_message_invalid_with_custom_message + assert_equal 'custom message title', @topic.errors.generate_message(:title, :invalid, :default => 'custom message {{value}}', :value => 'title') + end + + # validates_confirmation_of: generate_message(attr_name, :confirmation, :default => configuration[:message]) + def test_generate_message_confirmation_with_default_message + assert_equal "doesn't match confirmation", @topic.errors.generate_message(:title, :confirmation, :default => nil) + end + + def test_generate_message_confirmation_with_custom_message + assert_equal 'custom message', @topic.errors.generate_message(:title, :confirmation, :default => 'custom message') + end + + # validates_acceptance_of: generate_message(attr_name, :accepted, :default => configuration[:message]) + def test_generate_message_accepted_with_default_message + assert_equal "must be accepted", @topic.errors.generate_message(:title, :accepted, :default => nil) + end + + def test_generate_message_accepted_with_custom_message + assert_equal 'custom message', @topic.errors.generate_message(:title, :accepted, :default => 'custom message') + end + + # add_on_empty: generate_message(attr, :empty, :default => custom_message) + def test_generate_message_empty_with_default_message + assert_equal "can't be empty", @topic.errors.generate_message(:title, :empty, :default => nil) + end + + def test_generate_message_empty_with_custom_message + assert_equal 'custom message', @topic.errors.generate_message(:title, :empty, :default => 'custom message') + end + + # add_on_blank: generate_message(attr, :blank, :default => custom_message) + def test_generate_message_blank_with_default_message + assert_equal "can't be blank", @topic.errors.generate_message(:title, :blank, :default => nil) + end + + def test_generate_message_blank_with_custom_message + assert_equal 'custom message', @topic.errors.generate_message(:title, :blank, :default => 'custom message') + end + + # validates_length_of: generate_message(attr, :too_long, :default => options[:too_long], :count => option_value.end) + def test_generate_message_too_long_with_default_message + assert_equal "is too long (maximum is 10 characters)", @topic.errors.generate_message(:title, :too_long, :default => nil, :count => 10) + end + + def test_generate_message_too_long_with_custom_message + assert_equal 'custom message 10', @topic.errors.generate_message(:title, :too_long, :default => 'custom message {{count}}', :count => 10) + end + + # validates_length_of: generate_message(attr, :too_short, :default => options[:too_short], :count => option_value.begin) + def test_generate_message_too_short_with_default_message + assert_equal "is too short (minimum is 10 characters)", @topic.errors.generate_message(:title, :too_short, :default => nil, :count => 10) + end + + def test_generate_message_too_short_with_custom_message + assert_equal 'custom message 10', @topic.errors.generate_message(:title, :too_short, :default => 'custom message {{count}}', :count => 10) + end + + # validates_length_of: generate_message(attr, key, :default => custom_message, :count => option_value) + def test_generate_message_wrong_length_with_default_message + assert_equal "is the wrong length (should be 10 characters)", @topic.errors.generate_message(:title, :wrong_length, :default => nil, :count => 10) + end + + def test_generate_message_wrong_length_with_custom_message + assert_equal 'custom message 10', @topic.errors.generate_message(:title, :wrong_length, :default => 'custom message {{count}}', :count => 10) + end + + # validates_uniqueness_of: generate_message(attr_name, :taken, :default => configuration[:message]) + def test_generate_message_taken_with_default_message + assert_equal "has already been taken", @topic.errors.generate_message(:title, :taken, :default => nil, :value => 'title') + end + + def test_generate_message_taken_with_custom_message + assert_equal 'custom message title', @topic.errors.generate_message(:title, :taken, :default => 'custom message {{value}}', :value => 'title') + end + + # validates_numericality_of: generate_message(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message]) + def test_generate_message_not_a_number_with_default_message + assert_equal "is not a number", @topic.errors.generate_message(:title, :not_a_number, :default => nil, :value => 'title') + end + + def test_generate_message_not_a_number_with_custom_message + assert_equal 'custom message title', @topic.errors.generate_message(:title, :not_a_number, :default => 'custom message {{value}}', :value => 'title') + end + + # validates_numericality_of: generate_message(attr_name, option, :value => raw_value, :default => configuration[:message]) + def test_generate_message_greater_than_with_default_message + assert_equal "must be greater than 10", @topic.errors.generate_message(:title, :greater_than, :default => nil, :value => 'title', :count => 10) + end + + def test_generate_message_greater_than_or_equal_to_with_default_message + assert_equal "must be greater than or equal to 10", @topic.errors.generate_message(:title, :greater_than_or_equal_to, :default => nil, :value => 'title', :count => 10) + end + + def test_generate_message_equal_to_with_default_message + assert_equal "must be equal to 10", @topic.errors.generate_message(:title, :equal_to, :default => nil, :value => 'title', :count => 10) + end + + def test_generate_message_less_than_with_default_message + assert_equal "must be less than 10", @topic.errors.generate_message(:title, :less_than, :default => nil, :value => 'title', :count => 10) + end + + def test_generate_message_less_than_or_equal_to_with_default_message + assert_equal "must be less than or equal to 10", @topic.errors.generate_message(:title, :less_than_or_equal_to, :default => nil, :value => 'title', :count => 10) + end + + def test_generate_message_odd_with_default_message + assert_equal "must be odd", @topic.errors.generate_message(:title, :odd, :default => nil, :value => 'title', :count => 10) + end + + def test_generate_message_even_with_default_message + assert_equal "must be even", @topic.errors.generate_message(:title, :even, :default => nil, :value => 'title', :count => 10) + end end \ No newline at end of file -- cgit v1.2.3 From 572f73fae62972cc5aebba24bed45a127f2a4cac Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Sun, 10 Aug 2008 21:44:35 +0200 Subject: update i18n gem and clean up dir structure within i18n-0.0.1 --- .../active_support/vendor/i18n-0.0.1/MIT-LICENSE | 20 -- .../vendor/i18n-0.0.1/README.textile | 18 - .../active_support/vendor/i18n-0.0.1/i18n.gemspec | 24 -- .../lib/active_support/vendor/i18n-0.0.1/i18n.rb | 186 ++++++++++ .../vendor/i18n-0.0.1/i18n/backend/simple.rb | 178 ++++++++++ .../vendor/i18n-0.0.1/i18n/exceptions.rb | 53 +++ .../active_support/vendor/i18n-0.0.1/lib/i18n.rb | 182 ---------- .../vendor/i18n-0.0.1/lib/i18n/backend/simple.rb | 154 --------- .../vendor/i18n-0.0.1/lib/i18n/exceptions.rb | 45 --- .../active_support/vendor/i18n-0.0.1/test/all.rb | 4 - .../vendor/i18n-0.0.1/test/i18n_exceptions_test.rb | 100 ------ .../vendor/i18n-0.0.1/test/i18n_test.rb | 141 -------- .../vendor/i18n-0.0.1/test/simple_backend_test.rb | 376 --------------------- 13 files changed, 417 insertions(+), 1064 deletions(-) delete mode 100755 activesupport/lib/active_support/vendor/i18n-0.0.1/MIT-LICENSE delete mode 100644 activesupport/lib/active_support/vendor/i18n-0.0.1/README.textile delete mode 100644 activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.gemspec create mode 100755 activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb create mode 100644 activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb create mode 100644 activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/exceptions.rb delete mode 100755 activesupport/lib/active_support/vendor/i18n-0.0.1/lib/i18n.rb delete mode 100644 activesupport/lib/active_support/vendor/i18n-0.0.1/lib/i18n/backend/simple.rb delete mode 100644 activesupport/lib/active_support/vendor/i18n-0.0.1/lib/i18n/exceptions.rb delete mode 100644 activesupport/lib/active_support/vendor/i18n-0.0.1/test/all.rb delete mode 100644 activesupport/lib/active_support/vendor/i18n-0.0.1/test/i18n_exceptions_test.rb delete mode 100644 activesupport/lib/active_support/vendor/i18n-0.0.1/test/i18n_test.rb delete mode 100644 activesupport/lib/active_support/vendor/i18n-0.0.1/test/simple_backend_test.rb diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/MIT-LICENSE b/activesupport/lib/active_support/vendor/i18n-0.0.1/MIT-LICENSE deleted file mode 100755 index ed8e9ee66d..0000000000 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/MIT-LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -Copyright (c) 2008 The Ruby I18n team - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/README.textile b/activesupport/lib/active_support/vendor/i18n-0.0.1/README.textile deleted file mode 100644 index 2b14a99c0f..0000000000 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/README.textile +++ /dev/null @@ -1,18 +0,0 @@ -h1. Ruby I18n gem - -I18n and localization solution for Ruby. - -h2. Authors - -* "Matt Aimonetti":http://railsontherun.com -* "Sven Fuchs":http://www.artweb-design.de -* "Joshua Harvey":http://www.workingwithrails.com/person/759-joshua-harvey -* "Saimon Moore":http://saimonmoore.net -* "Stephan Soller":http://www.arkanis-development.de - -h2. License - -MIT License. See the included MIT-LICENCE file. - - - diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.gemspec b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.gemspec deleted file mode 100644 index 81ad0b598d..0000000000 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.gemspec +++ /dev/null @@ -1,24 +0,0 @@ -Gem::Specification.new do |s| - s.name = "i18n" - s.version = "0.0.1" - s.date = "2008-06-13" - s.summary = "Internationalization for Ruby" - s.email = "rails-patch-i18n@googlegroups.com" - s.homepage = "http://groups.google.com/group/rails-patch-i18n" - s.description = "Add Internationalization to your Ruby application." - s.has_rdoc = false - s.authors = ['Sven Fuchs', 'Matt Aimonetti', 'Stephan Soller', 'Saimon Moore'] - s.files = [ - "lib/i18n/backend/minimal.rb", - "lib/i18n/core_ext.rb", - "lib/i18n/localization.rb", - "lib/i18n/translation.rb", - "lib/i18n.rb", - "LICENSE", - "README", - "spec/core_ext_spec.rb", - "spec/i18n_spec.rb", - "spec/spec.opts", - "spec/spec_helper.rb" - ] -end \ No newline at end of file diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb new file mode 100755 index 0000000000..86005553c0 --- /dev/null +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb @@ -0,0 +1,186 @@ +# Authors:: Matt Aimonetti (http://railsontherun.com/), +# Sven Fuchs (http://www.artweb-design.de), +# Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey), +# Saimon Moore (http://saimonmoore.net), +# Stephan Soller (http://www.arkanis-development.de/) +# Copyright:: Copyright (c) 2008 The Ruby i18n Team +# License:: MIT +require 'i18n/backend/simple' +require 'i18n/exceptions' + +module I18n + @@backend = Backend::Simple + @@default_locale = 'en-US' + @@exception_handler = :default_exception_handler + + class << self + # Returns the current backend. Defaults to +Backend::Simple+. + def backend + @@backend + end + + # Sets the current backend. Used to set a custom backend. + def backend=(backend) + @@backend = backend + end + + # Returns the current default locale. Defaults to 'en-US' + def default_locale + @@default_locale + end + + # Sets the current default locale. Used to set a custom default locale. + def default_locale=(locale) + @@default_locale = locale + end + + # Returns the current locale. Defaults to I18n.default_locale. + def locale + Thread.current[:locale] ||= default_locale + end + + # Sets the current locale pseudo-globally, i.e. in the Thread.current hash. + def locale=(locale) + Thread.current[:locale] = locale + end + + # Sets the exception handler. + def exception_handler=(exception_handler) + @@exception_handler = exception_handler + end + + # Allow client libraries to pass a block that populates the translation + # storage. Decoupled for backends like a db backend that persist their + # translations, so the backend can decide whether/when to yield or not. + def populate(&block) + backend.populate(&block) + end + + def load_translations(filename) + backend.load_translations filename + end + + # Stores translations for the given locale in the backend. + def store_translations(locale, data) + backend.store_translations locale, data + end + + # Translates, pluralizes and interpolates a given key using a given locale, + # scope, and default, as well as interpolation values. + # + # *LOOKUP* + # + # Translation data is organized as a nested hash using the upper-level keys + # as namespaces. E.g., ActionView ships with the translation: + # :date => {:formats => {:short => "%b %d"}}. + # + # Translations can be looked up at any level of this hash using the key argument + # and the scope option. E.g., in this example I18n.t :date + # returns the whole translations hash {:formats => {:short => "%b %d"}}. + # + # Key can be either a single key or a dot-separated key (both Strings and Symbols + # work). E.g., the short format can be looked up using both: + # I18n.t 'date.formats.short' + # I18n.t :'date.formats.short' + # + # Scope can be either a single key, a dot-separated key or an array of keys + # or dot-separated keys. Keys and scopes can be combined freely. So these + # examples will all look up the same short date format: + # I18n.t 'date.formats.short' + # I18n.t 'formats.short', :scope => 'date' + # I18n.t 'short', :scope => 'date.formats' + # I18n.t 'short', :scope => %w(date formats) + # + # *INTERPOLATION* + # + # Translations can contain interpolation variables which will be replaced by + # values passed to #translate as part of the options hash, with the keys matching + # the interpolation variable names. + # + # E.g., with a translation :foo => "foo {{bar}}" the option + # value for the key +bar+ will be interpolated into the translation: + # I18n.t :foo, :bar => 'baz' # => 'foo baz' + # + # *PLURALIZATION* + # + # Translation data can contain pluralized translations. Pluralized translations + # are arrays of singluar/plural versions of translations like ['Foo', 'Foos']. + # + # Note that I18n::Backend::Simple only supports an algorithm for English + # pluralization rules. Other algorithms can be supported by custom backends. + # + # This returns the singular version of a pluralized translation: + # I18n.t :foo, :count => 1 # => 'Foo' + # + # These both return the plural version of a pluralized translation: + # I18n.t :foo, :count => 0 # => 'Foos' + # I18n.t :foo, :count => 2 # => 'Foos' + # + # The :count option can be used both for pluralization and interpolation. + # E.g., with the translation + # :foo => ['{{count}} foo', '{{count}} foos'], count will + # be interpolated to the pluralized translation: + # I18n.t :foo, :count => 1 # => '1 foo' + # + # *DEFAULTS* + # + # This returns the translation for :foo or default if no translation was found: + # I18n.t :foo, :default => 'default' + # + # This returns the translation for :foo or the translation for :bar if no + # translation for :foo was found: + # I18n.t :foo, :default => :bar + # + # Returns the translation for :foo or the translation for :bar + # or default if no translations for :foo and :bar were found. + # I18n.t :foo, :default => [:bar, 'default'] + # + # BULK LOOKUP + # + # This returns an array with the translations for :foo and :bar. + # I18n.t [:foo, :bar] + # + # Can be used with dot-separated nested keys: + # I18n.t [:'baz.foo', :'baz.bar'] + # + # Which is the same as using a scope option: + # I18n.t [:foo, :bar], :scope => :baz + def translate(key, options = {}) + locale = options.delete(:locale) || I18n.locale + backend.translate locale, key, options + rescue I18n::ArgumentError => e + raise e if options[:raise] + send @@exception_handler, e, locale, key, options + end + alias :t :translate + + # Localizes certain objects, such as dates and numbers to local formatting. + def localize(object, options = {}) + locale = options[:locale] || I18n.locale + format = options[:format] || :default + backend.localize(locale, object, format) + end + alias :l :localize + + protected + # Handles exceptions raised in the backend. All exceptions except for + # MissingTranslationData exceptions are re-raised. When a MissingTranslationData + # was caught and the option :raise is not set the handler returns an error + # message string containing the key/scope. + def default_exception_handler(exception, locale, key, options) + return exception.message if MissingTranslationData === exception + raise exception + end + + # Merges the given locale, key and scope into a single array of keys. + # Splits keys that contain dots into multiple keys. Makes sure all + # keys are Symbols. + def normalize_translation_keys(locale, key, scope) + keys = [locale] + Array(scope) + [key] + keys = keys.map{|key| key.to_s.split(/\./) } + keys.flatten.map{|key| key.to_sym} + end + end +end + + diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb new file mode 100644 index 0000000000..98c7964b21 --- /dev/null +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb @@ -0,0 +1,178 @@ +require 'strscan' + +module I18n + module Backend + module Simple + @@translations = {} + + class << self + # Allow client libraries to pass a block that populates the translation + # storage. Decoupled for backends like a db backend that persist their + # translations, so the backend can decide whether/when to yield or not. + def populate(&block) + yield + end + + def load_translations(*filenames) + filenames.each {|filename| load_file filename } + end + + # Stores translations for the given locale in memory. + # This uses a deep merge for the translations hash, so existing + # translations will be overwritten by new ones only at the deepest + # level of the hash. + def store_translations(locale, data) + merge_translations(locale, data) + end + + def translate(locale, key, options = {}) + raise InvalidLocale.new(locale) if locale.nil? + return key.map{|key| translate locale, key, options } if key.is_a? Array + + reserved = :scope, :default + count, scope, default = options.values_at(:count, *reserved) + options.delete(:default) + values = options.reject{|name, value| reserved.include? name } + + entry = lookup(locale, key, scope) || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options)) + entry = pluralize locale, entry, count + entry = interpolate locale, entry, values + entry + end + + # Acts the same as +strftime+, but returns a localized version of the + # formatted date string. Takes a key from the date/time formats + # translations as a format argument (e.g., :short in :'date.formats'). + def localize(locale, object, format = :default) + raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime) + + type = object.respond_to?(:sec) ? 'time' : 'date' + formats = translate(locale, :"#{type}.formats") + format = formats[format.to_sym] if formats && formats[format.to_sym] + # TODO raise exception unless format found? + format = format.to_s.dup + + format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday]) + format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday]) + format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon]) + format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon]) + format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour + object.strftime(format) + end + + protected + + # Looks up a translation from the translations hash. Returns nil if + # eiher key is nil, or locale, scope or key do not exist as a key in the + # nested translations hash. Splits keys or scopes containing dots + # into multiple keys, i.e. currency.format is regarded the same as + # %w(currency format). + def lookup(locale, key, scope = []) + return unless key + keys = I18n.send :normalize_translation_keys, locale, key, scope + keys.inject(@@translations){|result, key| result[key.to_sym] or return nil } + end + + # Evaluates a default translation. + # If the given default is a String it is used literally. If it is a Symbol + # it will be translated with the given options. If it is an Array the first + # translation yielded will be returned. + # + # I.e., default(locale, [:foo, 'default']) will return +default+ if + # translate(locale, :foo) does not yield a result. + def default(locale, default, options = {}) + case default + when String then default + when Symbol then translate locale, default, options + when Array then default.each do |obj| + result = default(locale, obj, options.dup) and return result + end + end + rescue MissingTranslationData + nil + end + + # Picks a translation from an array according to English pluralization + # rules. It will pick the first translation if count is not equal to 1 + # and the second translation if it is equal to 1. Other backends can + # implement more flexible or complex pluralization rules. + def pluralize(locale, entry, count) + return entry unless entry.is_a?(Hash) and count + # raise InvalidPluralizationData.new(entry, count) unless entry.is_a?(Hash) + key = :zero if count == 0 && entry.has_key?(:zero) + key ||= count == 1 ? :one : :many + raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key) + entry[key] + end + + # Interpolates values into a given string. + # + # interpolate "file {{file}} opened by \\{{user}}", :file => 'test.txt', :user => 'Mr. X' + # # => "file test.txt opened by {{user}}" + # + # Note that you have to double escape the \\ when you want to escape + # the {{...}} key in a string (once for the string and once for the + # interpolation). + def interpolate(locale, string, values = {}) + return string if !string.is_a?(String) + + map = {'%d' => '{{count}}', '%s' => '{{value}}'} # TODO deprecate this? + string.gsub!(/#{map.keys.join('|')}/){|key| map[key]} + + s = StringScanner.new string.dup + while s.skip_until(/\{\{/) + s.string[s.pos - 3, 1] = '' and next if s.pre_match[-1, 1] == '\\' + start_pos = s.pos - 2 + key = s.scan_until(/\}\}/)[0..-3] + end_pos = s.pos - 1 + + raise ReservedInterpolationKey.new(key, string) if %w(scope default).include?(key) + raise MissingInterpolationArgument.new(key, string) unless values.has_key? key.to_sym + + s.string[start_pos..end_pos] = values[key.to_sym].to_s + s.unscan + end + s.string + end + + def load_file(filename) + type = File.extname(filename).tr('.', '').downcase + raise UnknownFileType.new(type, filename) unless respond_to? :"load_#{type}" + data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash + data.each do |locale, data| + merge_translations locale, data + end + end + + def load_rb(filename) + eval IO.read(filename), binding, filename + end + + def load_yml(filename) + YAML::load IO.read(filename) + end + + # Deep merges the given translations hash with the existing translations + # for the given locale + def merge_translations(locale, data) + locale = locale.to_sym + @@translations[locale] ||= {} + data = deep_symbolize_keys data + + # deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809 + merger = proc{|key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 } + @@translations[locale].merge! data, &merger + end + + # Return a new hash with all keys and nested keys converted to symbols. + def deep_symbolize_keys(hash) + hash.inject({}){|result, (key, value)| + value = deep_symbolize_keys(value) if value.is_a? Hash + result[(key.to_sym rescue key) || key] = value + result + } + end + end + end + end +end diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/exceptions.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/exceptions.rb new file mode 100644 index 0000000000..0f3eff1071 --- /dev/null +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/exceptions.rb @@ -0,0 +1,53 @@ +module I18n + class ArgumentError < ::ArgumentError; end + + class InvalidLocale < ArgumentError + attr_reader :locale + def initialize(locale) + @locale = locale + super "#{locale.inspect} is not a valid locale" + end + end + + class MissingTranslationData < ArgumentError + attr_reader :locale, :key, :options + def initialize(locale, key, options) + @key, @locale, @options = key, locale, options + keys = I18n.send(:normalize_translation_keys, locale, key, options[:scope]) + keys << 'no key' if keys.size < 2 + super "translation missing: #{keys.join(', ')}" + end + end + + class InvalidPluralizationData < ArgumentError + attr_reader :entry, :count + def initialize(entry, count) + @entry, @count = entry, count + super "translation data #{entry.inspect} can not be used with :count => #{count}" + end + end + + class MissingInterpolationArgument < ArgumentError + attr_reader :key, :string + def initialize(key, string) + @key, @string = key, string + super "interpolation argument #{key} missing in #{string.inspect}" + end + end + + class ReservedInterpolationKey < ArgumentError + attr_reader :key, :string + def initialize(key, string) + @key, @string = key, string + super "reserved key #{key.inspect} used in #{string.inspect}" + end + end + + class UnknownFileType < ArgumentError + attr_reader :type, :filename + def initialize(type, filename) + @type, @filename = type, filename + super "can not load translations from #{filename}, the file type #{type} is not known" + end + end +end \ No newline at end of file diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/lib/i18n.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/lib/i18n.rb deleted file mode 100755 index 1bb65263a3..0000000000 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/lib/i18n.rb +++ /dev/null @@ -1,182 +0,0 @@ -# Authors:: Matt Aimonetti (http://railsontherun.com/), -# Sven Fuchs (http://www.artweb-design.de), -# Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey), -# Saimon Moore (http://saimonmoore.net), -# Stephan Soller (http://www.arkanis-development.de/) -# Copyright:: Copyright (c) 2008 The Ruby i18n Team -# License:: MIT -require 'i18n/backend/simple' -require 'i18n/exceptions' - -module I18n - @@backend = Backend::Simple - @@default_locale = 'en-US' - @@exception_handler = :default_exception_handler - - class << self - # Returns the current backend. Defaults to +Backend::Simple+. - def backend - @@backend - end - - # Sets the current backend. Used to set a custom backend. - def backend=(backend) - @@backend = backend - end - - # Returns the current default locale. Defaults to 'en-US' - def default_locale - @@default_locale - end - - # Sets the current default locale. Used to set a custom default locale. - def default_locale=(locale) - @@default_locale = locale - end - - # Returns the current locale. Defaults to I18n.default_locale. - def locale - Thread.current[:locale] ||= default_locale - end - - # Sets the current locale pseudo-globally, i.e. in the Thread.current hash. - def locale=(locale) - Thread.current[:locale] = locale - end - - # Sets the exception handler. - def exception_handler=(exception_handler) - @@exception_handler = exception_handler - end - - # Allow client libraries to pass a block that populates the translation - # storage. Decoupled for backends like a db backend that persist their - # translations, so the backend can decide whether/when to yield or not. - def populate(&block) - backend.populate(&block) - end - - # Stores translations for the given locale in the backend. - def store_translations(locale, data) - backend.store_translations locale, data - end - - # Translates, pluralizes and interpolates a given key using a given locale, - # scope, and default, as well as interpolation values. - # - # *LOOKUP* - # - # Translation data is organized as a nested hash using the upper-level keys - # as namespaces. E.g., ActionView ships with the translation: - # :date => {:formats => {:short => "%b %d"}}. - # - # Translations can be looked up at any level of this hash using the key argument - # and the scope option. E.g., in this example I18n.t :date - # returns the whole translations hash {:formats => {:short => "%b %d"}}. - # - # Key can be either a single key or a dot-separated key (both Strings and Symbols - # work). E.g., the short format can be looked up using both: - # I18n.t 'date.formats.short' - # I18n.t :'date.formats.short' - # - # Scope can be either a single key, a dot-separated key or an array of keys - # or dot-separated keys. Keys and scopes can be combined freely. So these - # examples will all look up the same short date format: - # I18n.t 'date.formats.short' - # I18n.t 'formats.short', :scope => 'date' - # I18n.t 'short', :scope => 'date.formats' - # I18n.t 'short', :scope => %w(date formats) - # - # *INTERPOLATION* - # - # Translations can contain interpolation variables which will be replaced by - # values passed to #translate as part of the options hash, with the keys matching - # the interpolation variable names. - # - # E.g., with a translation :foo => "foo {{bar}}" the option - # value for the key +bar+ will be interpolated into the translation: - # I18n.t :foo, :bar => 'baz' # => 'foo baz' - # - # *PLURALIZATION* - # - # Translation data can contain pluralized translations. Pluralized translations - # are arrays of singluar/plural versions of translations like ['Foo', 'Foos']. - # - # Note that I18n::Backend::Simple only supports an algorithm for English - # pluralization rules. Other algorithms can be supported by custom backends. - # - # This returns the singular version of a pluralized translation: - # I18n.t :foo, :count => 1 # => 'Foo' - # - # These both return the plural version of a pluralized translation: - # I18n.t :foo, :count => 0 # => 'Foos' - # I18n.t :foo, :count => 2 # => 'Foos' - # - # The :count option can be used both for pluralization and interpolation. - # E.g., with the translation - # :foo => ['{{count}} foo', '{{count}} foos'], count will - # be interpolated to the pluralized translation: - # I18n.t :foo, :count => 1 # => '1 foo' - # - # *DEFAULTS* - # - # This returns the translation for :foo or default if no translation was found: - # I18n.t :foo, :default => 'default' - # - # This returns the translation for :foo or the translation for :bar if no - # translation for :foo was found: - # I18n.t :foo, :default => :bar - # - # Returns the translation for :foo or the translation for :bar - # or default if no translations for :foo and :bar were found. - # I18n.t :foo, :default => [:bar, 'default'] - # - # BULK LOOKUP - # - # This returns an array with the translations for :foo and :bar. - # I18n.t [:foo, :bar] - # - # Can be used with dot-separated nested keys: - # I18n.t [:'baz.foo', :'baz.bar'] - # - # Which is the same as using a scope option: - # I18n.t [:foo, :bar], :scope => :baz - def translate(key, options = {}) - locale = options.delete(:locale) || I18n.locale - backend.translate locale, key, options - rescue I18n::ArgumentError => e - raise e if options[:raise] - send @@exception_handler, e, locale, key, options - end - alias :t :translate - - # Localizes certain objects, such as dates and numbers to local formatting. - def localize(object, options = {}) - locale = options[:locale] || I18n.locale - format = options[:format] || :default - backend.localize(locale, object, format) - end - alias :l :localize - - protected - # Handles exceptions raised in the backend. All exceptions except for - # MissingTranslationData exceptions are re-raised. When a MissingTranslationData - # was caught and the option :raise is not set the handler returns an error - # message string containing the key/scope. - def default_exception_handler(exception, locale, key, options) - return exception.message if MissingTranslationData === exception - raise exception - end - - # Merges the given locale, key and scope into a single array of keys. - # Splits keys that contain dots into multiple keys. Makes sure all - # keys are Symbols. - def normalize_translation_keys(locale, key, scope) - keys = [locale] + Array(scope) + [key] - keys = keys.map{|k| k.to_s.split(/\./) } - keys.flatten.map{|k| k.to_sym} - end - end -end - - diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/lib/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/lib/i18n/backend/simple.rb deleted file mode 100644 index b8be1cecfb..0000000000 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/lib/i18n/backend/simple.rb +++ /dev/null @@ -1,154 +0,0 @@ -require 'strscan' - -module I18n - module Backend - module Simple - @@translations = {} - - class << self - # Allow client libraries to pass a block that populates the translation - # storage. Decoupled for backends like a db backend that persist their - # translations, so the backend can decide whether/when to yield or not. - def populate(&block) - yield - end - - # Stores translations for the given locale in memory. - # This uses a deep merge for the translations hash, so existing - # translations will be overwritten by new ones only at the deepest - # level of the hash. - def store_translations(locale, data) - merge_translations(locale, data) - end - - def translate(locale, key, options = {}) - raise InvalidLocale.new(locale) if locale.nil? - return key.map{|k| translate locale, k, options } if key.is_a? Array - - reserved = :scope, :default - count, scope, default = options.values_at(:count, *reserved) - options.delete(:default) - values = options.reject{|name, value| reserved.include? name } - - entry = lookup(locale, key, scope) || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options)) - entry = pluralize entry, count - entry = interpolate entry, values - entry - end - - # Acts the same as +strftime+, but returns a localized version of the - # formatted date string. Takes a key from the date/time formats - # translations as a format argument (e.g., :short in :'date.formats'). - def localize(locale, object, format = :default) - raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime) - - type = object.respond_to?(:sec) ? 'time' : 'date' - formats = translate(locale, :"#{type}.formats") - format = formats[format.to_sym] if formats && formats[format.to_sym] - # TODO raise exception unless format found? - format = format.to_s.dup - - format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday]) - format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday]) - format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon]) - format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon]) - format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour - object.strftime(format) - end - - protected - - # Looks up a translation from the translations hash. Returns nil if - # eiher key is nil, or locale, scope or key do not exist as a key in the - # nested translations hash. Splits keys or scopes containing dots - # into multiple keys, i.e. currency.format is regarded the same as - # %w(currency format). - def lookup(locale, key, scope = []) - return unless key - keys = I18n.send :normalize_translation_keys, locale, key, scope - keys.inject(@@translations){|result, k| result[k.to_sym] or return nil } - end - - # Evaluates a default translation. - # If the given default is a String it is used literally. If it is a Symbol - # it will be translated with the given options. If it is an Array the first - # translation yielded will be returned. - # - # I.e., default(locale, [:foo, 'default']) will return +default+ if - # translate(locale, :foo) does not yield a result. - def default(locale, default, options = {}) - case default - when String then default - when Symbol then translate locale, default, options - when Array then default.each do |obj| - result = default(locale, obj, options.dup) and return result - end - end - rescue MissingTranslationData - nil - end - - # Picks a translation from an array according to English pluralization - # rules. It will pick the first translation if count is not equal to 1 - # and the second translation if it is equal to 1. Other backends can - # implement more flexible or complex pluralization rules. - def pluralize(entry, count) - return entry unless entry.is_a?(Array) and count - raise InvalidPluralizationData.new(entry, count) unless entry.size == 2 - entry[count == 1 ? 0 : 1] - end - - # Interpolates values into a given string. - # - # interpolate "file {{file}} opened by \\{{user}}", :file => 'test.txt', :user => 'Mr. X' - # # => "file test.txt opened by {{user}}" - # - # Note that you have to double escape the \\ when you want to escape - # the {{...}} key in a string (once for the string and once for the - # interpolation). - def interpolate(string, values = {}) - return string if !string.is_a?(String) - - map = {'%d' => '{{count}}', '%s' => '{{value}}'} # TODO deprecate this? - string.gsub!(/#{map.keys.join('|')}/){|key| map[key]} - - s = StringScanner.new string.dup - while s.skip_until(/\{\{/) - s.string[s.pos - 3, 1] = '' and next if s.pre_match[-1, 1] == '\\' - start_pos = s.pos - 2 - key = s.scan_until(/\}\}/)[0..-3] - end_pos = s.pos - 1 - - raise ReservedInterpolationKey.new(key, string) if %w(scope default).include?(key) - raise MissingInterpolationArgument.new(key, string) unless values.has_key? key.to_sym - - s.string[start_pos..end_pos] = values[key.to_sym].to_s - s.unscan - end - s.string - end - - # Deep merges the given translations hash with the existing translations - # for the given locale - def merge_translations(locale, data) - locale = locale.to_sym - @@translations[locale] ||= {} - data = deep_symbolize_keys data - - # deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809 - merger = proc{|key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 } - @@translations[locale].merge! data, &merger - end - - # Return a new hash with all keys and nested keys converted to symbols. - def deep_symbolize_keys(hash) - hash.inject({}){|result, (key, value)| - value = deep_symbolize_keys(value) if value.is_a? Hash - result[(key.to_sym rescue key) || key] = value - result - } - end - end - end - end -end diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/lib/i18n/exceptions.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/lib/i18n/exceptions.rb deleted file mode 100644 index 6c1fc6d9b6..0000000000 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/lib/i18n/exceptions.rb +++ /dev/null @@ -1,45 +0,0 @@ -module I18n - class ArgumentError < ::ArgumentError; end - - class InvalidLocale < ArgumentError - attr_reader :locale - def initialize(locale) - @locale = locale - super "#{locale.inspect} is not a valid locale" - end - end - - class MissingTranslationData < ArgumentError - attr_reader :locale, :key, :options - def initialize(locale, key, options) - @key, @locale, @options = key, locale, options - keys = I18n.send(:normalize_translation_keys, locale, key, options[:scope]) - keys << 'no key' if keys.size < 2 - super "translation missing: #{keys.join(', ')}" - end - end - - class InvalidPluralizationData < ArgumentError - attr_reader :entry, :count - def initialize(entry, count) - @entry, @count = entry, count - super "translation data #{entry.inspect} can not be used with :count => #{count}" - end - end - - class MissingInterpolationArgument < ArgumentError - attr_reader :key, :string - def initialize(key, string) - @key, @string = key, string - super "interpolation argument #{key} missing in #{string.inspect}" - end - end - - class ReservedInterpolationKey < ArgumentError - attr_reader :key, :string - def initialize(key, string) - @key, @string = key, string - super "reserved key #{key.inspect} used in #{string.inspect}" - end - end -end \ No newline at end of file diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/test/all.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/test/all.rb deleted file mode 100644 index 048b62f8c3..0000000000 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/test/all.rb +++ /dev/null @@ -1,4 +0,0 @@ -dir = File.dirname(__FILE__) -require dir + '/i18n_test.rb' -require dir + '/simple_backend_test.rb' -require dir + '/i18n_exceptions_test.rb' diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/test/i18n_exceptions_test.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/test/i18n_exceptions_test.rb deleted file mode 100644 index 1ea1601681..0000000000 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/test/i18n_exceptions_test.rb +++ /dev/null @@ -1,100 +0,0 @@ -$:.unshift "lib" - -require 'rubygems' -require 'test/unit' -require 'mocha' -require 'i18n' -require 'active_support' - -class I18nExceptionsTest < Test::Unit::TestCase - def test_invalid_locale_stores_locale - force_invalid_locale - rescue I18n::ArgumentError => e - assert_nil e.locale - end - - def test_invalid_locale_message - force_invalid_locale - rescue I18n::ArgumentError => e - assert_equal 'nil is not a valid locale', e.message - end - - def test_missing_translation_data_stores_locale_key_and_options - force_missing_translation_data - rescue I18n::ArgumentError => e - options = {:scope => :bar} - assert_equal 'de-DE', e.locale - assert_equal :foo, e.key - assert_equal options, e.options - end - - def test_missing_translation_data_message - force_missing_translation_data - rescue I18n::ArgumentError => e - assert_equal 'translation missing: de-DE, bar, foo', e.message - end - - def test_invalid_pluralization_data_stores_entry_and_count - force_invalid_pluralization_data - rescue I18n::ArgumentError => e - assert_equal [:bar], e.entry - assert_equal 1, e.count - end - - def test_invalid_pluralization_data_message - force_invalid_pluralization_data - rescue I18n::ArgumentError => e - assert_equal 'translation data [:bar] can not be used with :count => 1', e.message - end - - def test_missing_interpolation_argument_stores_key_and_string - force_missing_interpolation_argument - rescue I18n::ArgumentError => e - assert_equal 'bar', e.key - assert_equal "{{bar}}", e.string - end - - def test_missing_interpolation_argument_message - force_missing_interpolation_argument - rescue I18n::ArgumentError => e - assert_equal 'interpolation argument bar missing in "{{bar}}"', e.message - end - - def test_reserved_interpolation_key_stores_key_and_string - force_reserved_interpolation_key - rescue I18n::ArgumentError => e - assert_equal 'scope', e.key - assert_equal "{{scope}}", e.string - end - - def test_reserved_interpolation_key_message - force_reserved_interpolation_key - rescue I18n::ArgumentError => e - assert_equal 'reserved key "scope" used in "{{scope}}"', e.message - end - - private - def force_invalid_locale - I18n.backend.translate nil, :foo - end - - def force_missing_translation_data - I18n.store_translations 'de-DE', :bar => nil - I18n.backend.translate 'de-DE', :foo, :scope => :bar - end - - def force_invalid_pluralization_data - I18n.store_translations 'de-DE', :foo => [:bar] - I18n.backend.translate 'de-DE', :foo, :count => 1 - end - - def force_missing_interpolation_argument - I18n.store_translations 'de-DE', :foo => "{{bar}}" - I18n.backend.translate 'de-DE', :foo, :baz => 'baz' - end - - def force_reserved_interpolation_key - I18n.store_translations 'de-DE', :foo => "{{scope}}" - I18n.backend.translate 'de-DE', :foo, :baz => 'baz' - end -end \ No newline at end of file diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/test/i18n_test.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/test/i18n_test.rb deleted file mode 100644 index bbb1316b49..0000000000 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/test/i18n_test.rb +++ /dev/null @@ -1,141 +0,0 @@ -$:.unshift "lib" - -require 'rubygems' -require 'test/unit' -require 'mocha' -require 'i18n' -require 'active_support' - -class I18nTest < Test::Unit::TestCase - def setup - I18n.store_translations :'en-US', { - :currency => { - :format => { - :separator => '.', - :delimiter => ',', - } - } - } - end - - def test_uses_simple_backend_set_by_default - assert_equal I18n::Backend::Simple, I18n.backend - end - - def test_can_set_backend - assert_nothing_raised{ I18n.backend = self } - assert_equal self, I18n.backend - I18n.backend = I18n::Backend::Simple - end - - def test_uses_en_us_as_default_locale_by_default - assert_equal 'en-US', I18n.default_locale - end - - def test_can_set_default_locale - assert_nothing_raised{ I18n.default_locale = 'de-DE' } - assert_equal 'de-DE', I18n.default_locale - I18n.default_locale = 'en-US' - end - - def test_uses_default_locale_as_locale_by_default - assert_equal I18n.default_locale, I18n.locale - end - - def test_can_set_locale_to_thread_current - assert_nothing_raised{ I18n.locale = 'de-DE' } - assert_equal 'de-DE', I18n.locale - assert_equal 'de-DE', Thread.current[:locale] - I18n.locale = 'en-US' - end - - def test_can_set_exception_handler - assert_nothing_raised{ I18n.exception_handler = :custom_exception_handler } - I18n.exception_handler = :default_exception_handler # revert it - end - - def test_uses_custom_exception_handler - I18n.exception_handler = :custom_exception_handler - I18n.expects(:custom_exception_handler) - I18n.translate :bogus - I18n.exception_handler = :default_exception_handler # revert it - end - - def test_delegates_translate_to_backend - I18n.backend.expects(:translate).with 'de-DE', :foo, {} - I18n.translate :foo, :locale => 'de-DE' - end - - def test_delegates_localize_to_backend - I18n.backend.expects(:localize).with 'de-DE', :whatever, :default - I18n.localize :whatever, :locale => 'de-DE' - end - - def test_delegates_store_translations_to_backend - I18n.backend.expects(:store_translations).with 'de-DE', {:foo => :bar} - I18n.store_translations 'de-DE', {:foo => :bar} - end - - def test_delegates_populate_to_backend - I18n.backend.expects(:populate) # can't specify a block here as an expected argument - I18n.populate{ } - end - - def test_populate_yields_the_block - tmp = nil - I18n.populate do tmp = 'yielded' end - assert_equal 'yielded', tmp - end - - def test_translate_given_no_locale_uses_i18n_locale - I18n.backend.expects(:translate).with 'en-US', :foo, {} - I18n.translate :foo - end - - def test_translate_on_nested_symbol_keys_works - assert_equal ".", I18n.t(:'currency.format.separator') - end - - def test_translate_with_nested_string_keys_works - assert_equal ".", I18n.t('currency.format.separator') - end - - def test_translate_with_array_as_scope_works - assert_equal ".", I18n.t(:separator, :scope => ['currency.format']) - end - - def test_translate_with_array_containing_dot_separated_strings_as_scope_works - assert_equal ".", I18n.t(:separator, :scope => ['currency.format']) - end - - def test_translate_with_key_array_and_dot_separated_scope_works - assert_equal [".", ","], I18n.t(%w(separator delimiter), :scope => 'currency.format') - end - - def test_translate_with_dot_separated_key_array_and_scope_works - assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency') - end - - def test_translate_with_options_using_scope_works - I18n.backend.expects(:translate).with('de-DE', :precision, :scope => :"currency.format") - I18n.with_options :locale => 'de-DE', :scope => :'currency.format' do |locale| - locale.t :precision - end - end - - # def test_translate_given_no_args_raises_missing_translation_data - # assert_equal "translation missing: en-US, no key", I18n.t - # end - - def test_translate_given_a_bogus_key_raises_missing_translation_data - assert_equal "translation missing: en-US, bogus", I18n.t(:bogus) - end - - def test_localize_nil_raises_argument_error - assert_raises(I18n::ArgumentError) { I18n.l nil } - end - - def test_localize_object_raises_argument_error - assert_raises(I18n::ArgumentError) { I18n.l Object.new } - end -end diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/test/simple_backend_test.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/test/simple_backend_test.rb deleted file mode 100644 index c94d742e2d..0000000000 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/test/simple_backend_test.rb +++ /dev/null @@ -1,376 +0,0 @@ -$:.unshift "lib" - -require 'rubygems' -require 'test/unit' -require 'mocha' -require 'i18n' - -module I18nSimpleBackendTestSetup - def setup_backend - @backend = I18n::Backend::Simple - @backend.send :class_variable_set, :@@translations, {} - @backend.store_translations 'en-US', :foo => {:bar => 'bar', :baz => 'baz'} - end - alias :setup :setup_backend - - def add_datetime_translations - @backend.store_translations :'de-DE', { - :date => { - :formats => { - :default => "%d.%m.%Y", - :short => "%d. %b", - :long => "%d. %B %Y", - }, - :day_names => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag), - :abbr_day_names => %w(So Mo Di Mi Do Fr Sa), - :month_names => %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember).unshift(nil), - :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil), - :order => [:day, :month, :year] - }, - :time => { - :formats => { - :default => "%a, %d. %b %Y %H:%M:%S %z", - :short => "%d. %b %H:%M", - :long => "%d. %B %Y %H:%M", - }, - :am => 'am', - :pm => 'pm' - }, - :datetime => { - :distance_in_words => { - :half_a_minute => 'half a minute', - :less_than_x_seconds => ['less than 1 second', 'less than {{count}} seconds'], - :x_seconds => ['1 second', '{{count}} seconds'], - :less_than_x_minutes => ['less than a minute', 'less than {{count}} minutes'], - :x_minutes => ['1 minute', '{{count}} minutes'], - :about_x_hours => ['about 1 hour', 'about {{count}} hours'], - :x_days => ['1 day', '{{count}} days'], - :about_x_months => ['about 1 month', 'about {{count}} months'], - :x_months => ['1 month', '{{count}} months'], - :about_x_years => ['about 1 year', 'about {{count}} year'], - :over_x_years => ['over 1 year', 'over {{count}} years'] - } - } - } - end -end - -class I18nSimpleBackendTranslationsTest < Test::Unit::TestCase - include I18nSimpleBackendTestSetup - - def test_store_translations_adds_translations # no, really :-) - @backend.store_translations :'en-US', :foo => 'bar' - assert_equal Hash[:'en-US', {:foo => 'bar'}], @backend.send(:class_variable_get, :@@translations) - end - - def test_store_translations_deep_merges_translations - @backend.store_translations :'en-US', :foo => {:bar => 'bar'} - @backend.store_translations :'en-US', :foo => {:baz => 'baz'} - assert_equal Hash[:'en-US', {:foo => {:bar => 'bar', :baz => 'baz'}}], @backend.send(:class_variable_get, :@@translations) - end - - def test_store_translations_forces_locale_to_sym - @backend.store_translations 'en-US', :foo => 'bar' - assert_equal Hash[:'en-US', {:foo => 'bar'}], @backend.send(:class_variable_get, :@@translations) - end - - def test_store_translations_covert_key_symbols - @backend.send :class_variable_set, :@@translations, {} # reset translations - @backend.store_translations :'en-US', 'foo' => {'bar' => 'baz'} - assert_equal Hash[:'en-US', {:foo => {:bar => 'baz'}}], - @backend.send(:class_variable_get, :@@translations) - end -end - -class I18nSimpleBackendTranslateTest < Test::Unit::TestCase - include I18nSimpleBackendTestSetup - - def test_translate_calls_lookup_with_locale_given - @backend.expects(:lookup).with('de-DE', :bar, [:foo]).returns 'bar' - @backend.translate 'de-DE', :bar, :scope => [:foo] - end - - def test_translate_given_a_symbol_as_a_default_translates_the_symbol - assert_equal 'bar', @backend.translate('en-US', nil, :scope => [:foo], :default => :bar) - end - - def test_translate_given_an_array_as_default_uses_the_first_match - assert_equal 'bar', @backend.translate('en-US', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :bar]) - end - - def test_translate_an_array_of_keys_translates_all_of_them - assert_equal %w(bar baz), @backend.translate('en-US', [:bar, :baz], :scope => [:foo]) - end - - def test_translate_calls_pluralize - @backend.expects(:pluralize).with 'bar', 1 - @backend.translate 'en-US', :bar, :scope => [:foo], :count => 1 - end - - def test_translate_calls_interpolate - @backend.expects(:interpolate).with 'bar', {} - @backend.translate 'en-US', :bar, :scope => [:foo] - end - - def test_translate_calls_interpolate_including_count_as_a_value - @backend.expects(:interpolate).with 'bar', {:count => 1} - @backend.translate 'en-US', :bar, :scope => [:foo], :count => 1 - end - - def test_given_no_keys_it_returns_the_default - assert_equal 'default', @backend.translate('en-US', nil, :default => 'default') - end - - def test_translate_given_nil_as_a_locale_raises_an_argument_error - assert_raises(I18n::InvalidLocale){ @backend.translate nil, :bar } - end - - def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_data - assert_raises(I18n::MissingTranslationData){ @backend.translate 'de-DE', :bogus } - end -end - -class I18nSimpleBackendLookupTest < Test::Unit::TestCase - include I18nSimpleBackendTestSetup - - # useful because this way we can use the backend with no key for interpolation/pluralization - def test_lookup_given_nil_as_a_key_returns_nil - assert_nil @backend.send(:lookup, 'en-US', nil) - end - - def test_lookup_given_nested_keys_looks_up_a_nested_hash_value - assert_equal 'bar', @backend.send(:lookup, 'en-US', :bar, [:foo]) - end -end - -class I18nSimpleBackendPluralizeTest < Test::Unit::TestCase - include I18nSimpleBackendTestSetup - - def test_pluralize_given_nil_returns_the_given_entry - assert_equal ['bar', 'bars'], @backend.send(:pluralize, ['bar', 'bars'], nil) - end - - def test_pluralize_given_0_returns_plural_string - assert_equal 'bars', @backend.send(:pluralize, ['bar', 'bars'], 0) - end - - def test_pluralize_given_1_returns_singular_string - assert_equal 'bar', @backend.send(:pluralize, ['bar', 'bars'], 1) - end - - def test_pluralize_given_2_returns_plural_string - assert_equal 'bars', @backend.send(:pluralize, ['bar', 'bars'], 2) - end - - def test_pluralize_given_3_returns_plural_string - assert_equal 'bars', @backend.send(:pluralize, ['bar', 'bars'], 3) - end - - def test_interpolate_given_invalid_pluralization_data_raises_invalid_pluralization_data - assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, ['bar'], 2) } - end -end - -class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase - include I18nSimpleBackendTestSetup - - def test_interpolate_given_a_value_hash_interpolates_the_values_to_the_string - assert_equal 'Hi David!', @backend.send(:interpolate, 'Hi {{name}}!', :name => 'David') - end - - def test_interpolate_given_nil_as_a_string_returns_nil - assert_nil @backend.send(:interpolate, nil, :name => 'David') - end - - def test_interpolate_given_an_non_string_as_a_string_returns_nil - assert_equal [], @backend.send(:interpolate, [], :name => 'David') - end - - def test_interpolate_given_a_values_hash_with_nil_values_interpolates_the_string - assert_equal 'Hi !', @backend.send(:interpolate, 'Hi {{name}}!', {:name => nil}) - end - - def test_interpolate_given_an_empty_values_hash_raises_missing_interpolation_argument - assert_raises(I18n::MissingInterpolationArgument) { @backend.send(:interpolate, 'Hi {{name}}!', {}) } - end - - def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key - assert_raises(I18n::ReservedInterpolationKey) { @backend.send(:interpolate, '{{default}}', {:default => nil}) } - end -end - -class I18nSimpleBackendLocalizeDateTest < Test::Unit::TestCase - include I18nSimpleBackendTestSetup - - def setup - @backend = I18n::Backend::Simple - add_datetime_translations - @date = Date.new 2008, 1, 1 - end - - def test_translate_given_the_short_format_it_uses_it - assert_equal '01. Jan', @backend.localize('de-DE', @date, :short) - end - - def test_translate_given_the_long_format_it_uses_it - assert_equal '01. Januar 2008', @backend.localize('de-DE', @date, :long) - end - - def test_translate_given_the_default_format_it_uses_it - assert_equal '01.01.2008', @backend.localize('de-DE', @date, :default) - end - - def test_translate_given_a_day_name_format_it_returns_a_day_name - assert_equal 'Dienstag', @backend.localize('de-DE', @date, '%A') - end - - def test_translate_given_an_abbr_day_name_format_it_returns_an_abbrevated_day_name - assert_equal 'Di', @backend.localize('de-DE', @date, '%a') - end - - def test_translate_given_a_month_name_format_it_returns_a_month_name - assert_equal 'Januar', @backend.localize('de-DE', @date, '%B') - end - - def test_translate_given_an_abbr_month_name_format_it_returns_an_abbrevated_month_name - assert_equal 'Jan', @backend.localize('de-DE', @date, '%b') - end - - def test_translate_given_no_format_it_does_not_fail - assert_nothing_raised{ @backend.localize 'de-DE', @date } - end - - def test_translate_given_an_unknown_format_it_does_not_fail - assert_nothing_raised{ @backend.localize 'de-DE', @date, '%x' } - end - - def test_localize_nil_raises_argument_error - assert_raises(I18n::ArgumentError) { @backend.localize 'de-DE', nil } - end - - def test_localize_object_raises_argument_error - assert_raises(I18n::ArgumentError) { @backend.localize 'de-DE', Object.new } - end -end - -class I18nSimpleBackendLocalizeDateTimeTest < Test::Unit::TestCase - include I18nSimpleBackendTestSetup - - def setup - @backend = I18n::Backend::Simple - add_datetime_translations - @morning = DateTime.new 2008, 1, 1, 6 - @evening = DateTime.new 2008, 1, 1, 18 - end - - def test_translate_given_the_short_format_it_uses_it - assert_equal '01. Jan 06:00', @backend.localize('de-DE', @morning, :short) - end - - def test_translate_given_the_long_format_it_uses_it - assert_equal '01. Januar 2008 06:00', @backend.localize('de-DE', @morning, :long) - end - - def test_translate_given_the_default_format_it_uses_it - assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de-DE', @morning, :default) - end - - def test_translate_given_a_day_name_format_it_returns_the_correct_day_name - assert_equal 'Dienstag', @backend.localize('de-DE', @morning, '%A') - end - - def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name - assert_equal 'Di', @backend.localize('de-DE', @morning, '%a') - end - - def test_translate_given_a_month_name_format_it_returns_the_correct_month_name - assert_equal 'Januar', @backend.localize('de-DE', @morning, '%B') - end - - def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name - assert_equal 'Jan', @backend.localize('de-DE', @morning, '%b') - end - - def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator - assert_equal 'am', @backend.localize('de-DE', @morning, '%p') - assert_equal 'pm', @backend.localize('de-DE', @evening, '%p') - end - - def test_translate_given_no_format_it_does_not_fail - assert_nothing_raised{ @backend.localize 'de-DE', @morning } - end - - def test_translate_given_an_unknown_format_it_does_not_fail - assert_nothing_raised{ @backend.localize 'de-DE', @morning, '%x' } - end -end - -class I18nSimpleBackendLocalizeTimeTest < Test::Unit::TestCase - include I18nSimpleBackendTestSetup - - def setup - @old_timezone, ENV['TZ'] = ENV['TZ'], 'UTC' - @backend = I18n::Backend::Simple - add_datetime_translations - @morning = Time.parse '2008-01-01 6:00 UTC' - @evening = Time.parse '2008-01-01 18:00 UTC' - end - - def teardown - ENV['TZ'] = @old_timezone - end - - def test_translate_given_the_short_format_it_uses_it - assert_equal '01. Jan 06:00', @backend.localize('de-DE', @morning, :short) - end - - def test_translate_given_the_long_format_it_uses_it - assert_equal '01. Januar 2008 06:00', @backend.localize('de-DE', @morning, :long) - end - - # TODO Seems to break on Windows because ENV['TZ'] is ignored. What's a better way to do this? - # def test_translate_given_the_default_format_it_uses_it - # assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de-DE', @morning, :default) - # end - - def test_translate_given_a_day_name_format_it_returns_the_correct_day_name - assert_equal 'Dienstag', @backend.localize('de-DE', @morning, '%A') - end - - def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name - assert_equal 'Di', @backend.localize('de-DE', @morning, '%a') - end - - def test_translate_given_a_month_name_format_it_returns_the_correct_month_name - assert_equal 'Januar', @backend.localize('de-DE', @morning, '%B') - end - - def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name - assert_equal 'Jan', @backend.localize('de-DE', @morning, '%b') - end - - def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator - assert_equal 'am', @backend.localize('de-DE', @morning, '%p') - assert_equal 'pm', @backend.localize('de-DE', @evening, '%p') - end - - def test_translate_given_no_format_it_does_not_fail - assert_nothing_raised{ @backend.localize 'de-DE', @morning } - end - - def test_translate_given_an_unknown_format_it_does_not_fail - assert_nothing_raised{ @backend.localize 'de-DE', @morning, '%x' } - end -end - -class I18nSimpleBackendHelperMethodsTest < Test::Unit::TestCase - def setup - @backend = I18n::Backend::Simple - end - - def test_deep_symbolize_keys_works - result = @backend.send :deep_symbolize_keys, 'foo' => {'bar' => {'baz' => 'bar'}} - expected = {:foo => {:bar => {:baz => 'bar'}}} - assert_equal expected, result - end -end -- cgit v1.2.3 From bfa143fd4d15544e788d25ec163f9cc57a1e2a03 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 13 Aug 2008 09:44:46 +0200 Subject: fix require path in vendor.rb for fixed vendor/gem dir structure --- activesupport/lib/active_support/vendor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/vendor.rb b/activesupport/lib/active_support/vendor.rb index 381471b833..acd94af783 100644 --- a/activesupport/lib/active_support/vendor.rb +++ b/activesupport/lib/active_support/vendor.rb @@ -29,6 +29,6 @@ end # begin # gem 'i18n', '~> 0.0.1' # rescue Gem::LoadError - $:.unshift "#{File.dirname(__FILE__)}/vendor/i18n-0.0.1/lib" + $:.unshift "#{File.dirname(__FILE__)}/vendor/i18n-0.0.1" require 'i18n' # end \ No newline at end of file -- cgit v1.2.3 From f26380b7757666fa793c150538e8444a640d29aa Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 13 Aug 2008 09:53:25 +0200 Subject: switch to using I18n.load_translations instead of requiring plain ruby files --- actionpack/lib/action_view.rb | 2 +- actionpack/lib/action_view/locale/en-US.rb | 155 ++++++++++++----------- activerecord/lib/active_record.rb | 2 +- activerecord/lib/active_record/locale/en-US.rb | 47 +++---- activesupport/lib/active_support.rb | 4 +- activesupport/lib/active_support/locale/en-US.rb | 49 +++---- 6 files changed, 131 insertions(+), 128 deletions(-) diff --git a/actionpack/lib/action_view.rb b/actionpack/lib/action_view.rb index dd555b3792..7e9ae9432d 100644 --- a/actionpack/lib/action_view.rb +++ b/actionpack/lib/action_view.rb @@ -35,7 +35,7 @@ require 'action_view/partials' require 'action_view/template_error' I18n.backend.populate do - require 'action_view/locale/en-US.rb' + I18n.load_translations File.dirname(__FILE__) + '/action_view/locale/en-US.rb' end ActionView::Base.class_eval do diff --git a/actionpack/lib/action_view/locale/en-US.rb b/actionpack/lib/action_view/locale/en-US.rb index 0119b3d9c1..8cce874700 100644 --- a/actionpack/lib/action_view/locale/en-US.rb +++ b/actionpack/lib/action_view/locale/en-US.rb @@ -1,86 +1,87 @@ -I18n.backend.store_translations :'en-US', { - :datetime => { - :distance_in_words => { - :half_a_minute => 'half a minute', - :less_than_x_seconds => { - :one => 'less than 1 second', - :many => 'less than {{count}} seconds' - }, - :x_seconds => { - :one => '1 second', - :many => '{{count}} seconds' - }, - :less_than_x_minutes => { - :one => 'less than a minute', - :many => 'less than {{count}} minutes' - }, - :x_minutes => { - :one => '1 minute', - :many => '{{count}} minutes' - }, - :about_x_hours => { - :one => 'about 1 hour', - :many => 'about {{count}} hours' - }, - :x_days => { - :one => '1 day', - :many => '{{count}} days' +{ :'en-US' => { + :datetime => { + :distance_in_words => { + :half_a_minute => 'half a minute', + :less_than_x_seconds => { + :one => 'less than 1 second', + :many => 'less than {{count}} seconds' + }, + :x_seconds => { + :one => '1 second', + :many => '{{count}} seconds' + }, + :less_than_x_minutes => { + :one => 'less than a minute', + :many => 'less than {{count}} minutes' + }, + :x_minutes => { + :one => '1 minute', + :many => '{{count}} minutes' + }, + :about_x_hours => { + :one => 'about 1 hour', + :many => 'about {{count}} hours' + }, + :x_days => { + :one => '1 day', + :many => '{{count}} days' + }, + :about_x_months => { + :one => 'about 1 month', + :many => 'about {{count}} months' + }, + :x_months => { + :one => '1 month', + :many => '{{count}} months' + }, + :about_x_years => { + :one => 'about 1 year', + :many => 'about {{count}} years' + }, + :over_x_years => { + :one => 'over 1 year', + :many => 'over {{count}} years' + } + } + }, + :number => { + :format => { + :precision => 3, + :separator => '.', + :delimiter => ',' }, - :about_x_months => { - :one => 'about 1 month', - :many => 'about {{count}} months' + :currency => { + :format => { + :unit => '$', + :precision => 2, + :format => '%u%n' + } }, - :x_months => { - :one => '1 month', - :many => '{{count}} months' + :human => { + :format => { + :precision => 1, + :delimiter => '' + } }, - :about_x_years => { - :one => 'about 1 year', - :many => 'about {{count}} years' + :percentage => { + :format => { + :delimiter => '' + } }, - :over_x_years => { - :one => 'over 1 year', - :many => 'over {{count}} years' + :precision => { + :format => { + :delimiter => '' + } } - } - }, - :number => { - :format => { - :precision => 3, - :separator => '.', - :delimiter => ',' }, - :currency => { - :format => { - :unit => '$', - :precision => 2, - :format => '%u%n' + :active_record => { + :error => { + :header_message => { + :one => "1 error prohibited this {{object_name}} from being saved", + :many => "{{count}} errors prohibited this {{object_name}} from being saved" + }, + :message => "There were problems with the following fields:" } - }, - :human => { - :format => { - :precision => 1, - :delimiter => '' - } - }, - :percentage => { - :format => { - :delimiter => '' - } - }, - :precision => { - :format => { - :delimiter => '' - } - } - }, - :active_record => { - :error => { - :header_message => { - :one => "1 error prohibited this {{object_name}} from being saved", - :many => "{{count}} errors prohibited this {{object_name}} from being saved" - }, - :message => "There were problems with the following fields:" } } -} +} \ No newline at end of file diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 17a7949959..08b6b19f7f 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -82,6 +82,6 @@ require 'active_record/connection_adapters/abstract_adapter' require 'active_record/schema_dumper' I18n.backend.populate do - require 'active_record/locale/en-US.rb' + I18n.load_translations File.dirname(__FILE__) + '/active_record/locale/en-US.rb' end diff --git a/activerecord/lib/active_record/locale/en-US.rb b/activerecord/lib/active_record/locale/en-US.rb index b31e13ed3a..4057a467e3 100644 --- a/activerecord/lib/active_record/locale/en-US.rb +++ b/activerecord/lib/active_record/locale/en-US.rb @@ -1,25 +1,26 @@ -I18n.backend.store_translations :'en-US', { - :active_record => { - :error_messages => { - :inclusion => "is not included in the list", - :exclusion => "is reserved", - :invalid => "is invalid", - :confirmation => "doesn't match confirmation", - :accepted => "must be accepted", - :empty => "can't be empty", - :blank => "can't be blank", - :too_long => "is too long (maximum is {{count}} characters)", - :too_short => "is too short (minimum is {{count}} characters)", - :wrong_length => "is the wrong length (should be {{count}} characters)", - :taken => "has already been taken", - :not_a_number => "is not a number", - :greater_than => "must be greater than {{count}}", - :greater_than_or_equal_to => "must be greater than or equal to {{count}}", - :equal_to => "must be equal to {{count}}", - :less_than => "must be less than {{count}}", - :less_than_or_equal_to => "must be less than or equal to {{count}}", - :odd => "must be odd", - :even => "must be even" - } +{ :'en-US' => { + :active_record => { + :error_messages => { + :inclusion => "is not included in the list", + :exclusion => "is reserved", + :invalid => "is invalid", + :confirmation => "doesn't match confirmation", + :accepted => "must be accepted", + :empty => "can't be empty", + :blank => "can't be blank", + :too_long => "is too long (maximum is {{count}} characters)", + :too_short => "is too short (minimum is {{count}} characters)", + :wrong_length => "is the wrong length (should be {{count}} characters)", + :taken => "has already been taken", + :not_a_number => "is not a number", + :greater_than => "must be greater than {{count}}", + :greater_than_or_equal_to => "must be greater than or equal to {{count}}", + :equal_to => "must be equal to {{count}}", + :less_than => "must be less than {{count}}", + :less_than_or_equal_to => "must be less than or equal to {{count}}", + :odd => "must be odd", + :even => "must be even" + } + } } } \ No newline at end of file diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index 51067e910e..6056067efd 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -58,8 +58,8 @@ require 'active_support/base64' require 'active_support/time_with_zone' -I18n.backend.populate do - require 'active_support/locale/en-US.rb' +I18n.populate do + I18n.load_translations File.dirname(__FILE__) + '/active_support/locale/en-US.rb' end Inflector = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Inflector', 'ActiveSupport::Inflector') diff --git a/activesupport/lib/active_support/locale/en-US.rb b/activesupport/lib/active_support/locale/en-US.rb index 51324a90bf..ed457d6f4f 100644 --- a/activesupport/lib/active_support/locale/en-US.rb +++ b/activesupport/lib/active_support/locale/en-US.rb @@ -1,28 +1,29 @@ -I18n.backend.store_translations :'en-US', { - :support => { - :array => { - :sentence_connector => 'and' - } - }, - :date => { - :formats => { - :default => "%Y-%m-%d", - :short => "%b %d", - :long => "%B %d, %Y", +{ :'en-US' => { + :support => { + :array => { + :sentence_connector => 'and' + } }, - :day_names => Date::DAYNAMES, - :abbr_day_names => Date::ABBR_DAYNAMES, - :month_names => Date::MONTHNAMES, - :abbr_month_names => Date::ABBR_MONTHNAMES, - :order => [:year, :month, :day] - }, - :time => { - :formats => { - :default => "%a, %d %b %Y %H:%M:%S %z", - :short => "%d %b %H:%M", - :long => "%B %d, %Y %H:%M", + :date => { + :formats => { + :default => "%Y-%m-%d", + :short => "%b %d", + :long => "%B %d, %Y", + }, + :day_names => Date::DAYNAMES, + :abbr_day_names => Date::ABBR_DAYNAMES, + :month_names => Date::MONTHNAMES, + :abbr_month_names => Date::ABBR_MONTHNAMES, + :order => [:year, :month, :day] }, - :am => 'am', - :pm => 'pm' + :time => { + :formats => { + :default => "%a, %d %b %Y %H:%M:%S %z", + :short => "%d %b %H:%M", + :long => "%B %d, %Y %H:%M", + }, + :am => 'am', + :pm => 'pm' + } } } \ No newline at end of file -- cgit v1.2.3 From aae9f916590b8ce2c84a9e404584de95b1705766 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 13 Aug 2008 10:00:54 +0200 Subject: fix validations_i18n tests for changed locale file format --- activerecord/test/cases/validations_i18n_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index 89ca61a220..bf0a4c9829 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -11,7 +11,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase def teardown reset_callbacks Topic - load 'active_record/locale/en-US.rb' + I18n.load_translations File.dirname(__FILE__) + '/../../lib/active_record/locale/en-US.rb' end def unique_topic -- cgit v1.2.3 From 78677cf6af339a6e23a93a45c905d6d1545ccc33 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 13 Aug 2008 11:42:02 +0200 Subject: update activesupport/vendor i18n gem --- activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb | 10 ++++++++-- .../active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb | 13 ++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb index 86005553c0..5c6c4eb783 100755 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb @@ -56,8 +56,14 @@ module I18n backend.populate(&block) end - def load_translations(filename) - backend.load_translations filename + # Allows client libraries to pass arguments that specify a source for + # translation data to be loaded by the backend. The backend defines + # acceptable sources. + # E.g. the provided SimpleBackend accepts a list of paths to translation + # files which are either named *.rb and contain plain Ruby Hashes or are + # named *.yml and contain YAML data.) + def load_translations(*args) + backend.load_translations *args end # Stores translations for the given locale in the backend. diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb index 98c7964b21..ba7e11210a 100644 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb @@ -12,7 +12,10 @@ module I18n def populate(&block) yield end - + + # Accepts a list of paths to translation files. Loads translations from + # plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml + # for details. def load_translations(*filenames) filenames.each {|filename| load_file filename } end @@ -135,6 +138,10 @@ module I18n s.string end + # Loads a single translations file by delegating to #load_rb or + # #load_yml depending on the file extension and directly merges the + # data to the existing translations. Raises I18n::UnknownFileType + # for all other file extensions. def load_file(filename) type = File.extname(filename).tr('.', '').downcase raise UnknownFileType.new(type, filename) unless respond_to? :"load_#{type}" @@ -144,10 +151,14 @@ module I18n end end + # Loads a plain Ruby translations file. eval'ing the file must yield + # a Hash containing translation data with locales as toplevel keys. def load_rb(filename) eval IO.read(filename), binding, filename end + # Loads a YAML translations file. The data must have locales as + # toplevel keys. def load_yml(filename) YAML::load IO.read(filename) end -- cgit v1.2.3 From 906e79396badfe9fb6b7f0457e835e5f1a9b09b2 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 13 Aug 2008 13:43:02 +0200 Subject: fix bug in I18n::Backend::Simple (A default array of non-existant keys returns the default array) --- .../lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb index ba7e11210a..530e9eca50 100644 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb @@ -89,7 +89,7 @@ module I18n when Symbol then translate locale, default, options when Array then default.each do |obj| result = default(locale, obj, options.dup) and return result - end + end and nil end rescue MissingTranslationData nil -- cgit v1.2.3 From e3ecc3375fef4faa807638ff9e9cf309094272bd Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 13 Aug 2008 13:52:07 +0200 Subject: provide more useful feedback on missing translations for validation error messages --- activerecord/lib/active_record/validations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 0de430567c..85b8e6232b 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -70,7 +70,7 @@ module ActiveRecord msgs << options[:default] if options[:default] msgs << key - I18n.t nil, options.merge(:default => msgs, :scope => [:active_record, :error_messages]) + I18n.t msgs.shift, options.merge(:default => msgs, :scope => [:active_record, :error_messages]) end # Returns true if the specified +attribute+ has errors associated with it. -- cgit v1.2.3 From d0ee883e7c01dabf039525b80b7f43673e987265 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 13 Aug 2008 14:02:26 +0200 Subject: fixing unit tests for active_record validations_i18n --- activerecord/test/cases/validations_i18n_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index bf0a4c9829..a0beb8be1b 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -46,14 +46,14 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase global_scope = [:active_record, :error_messages] custom_scope = global_scope + [:custom, 'topic', :title] - I18n.expects(:t).with nil, :scope => [:active_record, :error_messages], :default => [:"custom.topic.title.invalid", 'default from class def', :invalid] + I18n.expects(:t).with :"custom.topic.title.invalid", :scope => [:active_record, :error_messages], :default => ['default from class def', :invalid] @topic.errors.generate_message :title, :invalid, :default => 'default from class def' end def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti custom_scope = [:active_record, :error_messages, :custom, 'topic', :title] - I18n.expects(:t).with nil, :scope => [:active_record, :error_messages], :default => [:"custom.reply.title.invalid", :"custom.topic.title.invalid", 'default from class def', :invalid] + I18n.expects(:t).with :"custom.reply.title.invalid", :scope => [:active_record, :error_messages], :default => [:"custom.topic.title.invalid", 'default from class def', :invalid] Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def' end -- cgit v1.2.3 From ffeab4e0c171aecced4ddbe29b82aed064be9bdb Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Thu, 14 Aug 2008 01:28:31 +0200 Subject: Cleaned up ActiveRecord i18n scoping --- .../action_view/helpers/active_record_helper.rb | 8 +- actionpack/lib/action_view/locale/en-US.rb | 18 +-- .../template/active_record_helper_i18n_test.rb | 28 ++-- activerecord/lib/active_record/base.rb | 30 +++- activerecord/lib/active_record/locale/en-US.rb | 48 ++++--- activerecord/lib/active_record/validations.rb | 120 ++++++++-------- activerecord/test/cases/validations_i18n_test.rb | 155 +++++++++++---------- 7 files changed, 224 insertions(+), 183 deletions(-) diff --git a/actionpack/lib/action_view/helpers/active_record_helper.rb b/actionpack/lib/action_view/helpers/active_record_helper.rb index fce03ff605..c339e10701 100644 --- a/actionpack/lib/action_view/helpers/active_record_helper.rb +++ b/actionpack/lib/action_view/helpers/active_record_helper.rb @@ -189,15 +189,15 @@ module ActionView end options[:object_name] ||= params.first - I18n.with_options :locale => options[:locale], :scope => [:active_record, :error] do |locale| + I18n.with_options :locale => options[:locale], :scope => [:activerecord, :errors, :template] do |locale| header_message = if options.include?(:header_message) options[:header_message] else object_name = options[:object_name].to_s.gsub('_', ' ') - object_name = I18n.t(object_name, :default => object_name) - locale.t :header_message, :count => count, :object_name => object_name + object_name = I18n.t(object_name, :default => object_name, :scope => [:activerecord, :models], :count => 1) + locale.t :header, :count => count, :model => object_name end - message = options.include?(:message) ? options[:message] : locale.t(:message) + message = options.include?(:message) ? options[:message] : locale.t(:body) error_messages = objects.sum {|object| object.errors.full_messages.map {|msg| content_tag(:li, msg) } }.join contents = '' diff --git a/actionpack/lib/action_view/locale/en-US.rb b/actionpack/lib/action_view/locale/en-US.rb index 8cce874700..d4647b5a48 100644 --- a/actionpack/lib/action_view/locale/en-US.rb +++ b/actionpack/lib/action_view/locale/en-US.rb @@ -74,14 +74,16 @@ } } }, - :active_record => { - :error => { - :header_message => { - :one => "1 error prohibited this {{object_name}} from being saved", - :many => "{{count}} errors prohibited this {{object_name}} from being saved" - }, - :message => "There were problems with the following fields:" + :activerecord => { + :errors => { + :template => { + :header => { + :one => "1 error prohibited this {{model}} from being saved", + :many => "{{count}} errors prohibited this {{model}} from being saved" + }, + :body => "There were problems with the following fields:" + } } } } -} \ No newline at end of file +} diff --git a/actionpack/test/template/active_record_helper_i18n_test.rb b/actionpack/test/template/active_record_helper_i18n_test.rb index feec64aa30..7ba9659814 100644 --- a/actionpack/test/template/active_record_helper_i18n_test.rb +++ b/actionpack/test/template/active_record_helper_i18n_test.rb @@ -10,37 +10,37 @@ class ActiveRecordHelperI18nTest < Test::Unit::TestCase @object_name = 'book' stubs(:content_tag).returns 'content_tag' - I18n.stubs(:t).with(:'header_message', :locale => 'en-US', :scope => [:active_record, :error], :count => 1, :object_name => '').returns "1 error prohibited this from being saved" - I18n.stubs(:t).with(:'message', :locale => 'en-US', :scope => [:active_record, :error]).returns 'There were problems with the following fields:' + I18n.stubs(:t).with(:'header', :locale => 'en-US', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').returns "1 error prohibited this from being saved" + I18n.stubs(:t).with(:'body', :locale => 'en-US', :scope => [:activerecord, :errors, :template]).returns 'There were problems with the following fields:' end - def test_error_messages_for_given_a_header_message_option_it_does_not_translate_header_message - I18n.expects(:translate).with(:'header_message', :locale => 'en-US', :scope => [:active_record, :error], :count => 1, :object_name => '').never + def test_error_messages_for_given_a_header_option_it_does_not_translate_header_message + I18n.expects(:translate).with(:'header', :locale => 'en-US', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').never error_messages_for(:object => @object, :header_message => 'header message', :locale => 'en-US') end - def test_error_messages_for_given_no_header_message_option_it_translates_header_message - I18n.expects(:t).with(:'header_message', :locale => 'en-US', :scope => [:active_record, :error], :count => 1, :object_name => '').returns 'header message' - I18n.expects(:t).with('', :default => '').once.returns '' + def test_error_messages_for_given_no_header_option_it_translates_header_message + I18n.expects(:t).with(:'header', :locale => 'en-US', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').returns 'header message' + I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns '' error_messages_for(:object => @object, :locale => 'en-US') end def test_error_messages_for_given_a_message_option_it_does_not_translate_message - I18n.expects(:t).with(:'message', :locale => 'en-US', :scope => [:active_record, :error]).never - I18n.expects(:t).with('', :default => '').once.returns '' + I18n.expects(:t).with(:'body', :locale => 'en-US', :scope => [:activerecord, :errors, :template]).never + I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns '' error_messages_for(:object => @object, :message => 'message', :locale => 'en-US') end def test_error_messages_for_given_no_message_option_it_translates_message - I18n.expects(:t).with(:'message', :locale => 'en-US', :scope => [:active_record, :error]).returns 'There were problems with the following fields:' - I18n.expects(:t).with('', :default => '').once.returns '' + I18n.expects(:t).with(:'body', :locale => 'en-US', :scope => [:activerecord, :errors, :template]).returns 'There were problems with the following fields:' + I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns '' error_messages_for(:object => @object, :locale => 'en-US') end def test_error_messages_for_given_object_name_it_translates_object_name - I18n.expects(:t).with(:header_message, :locale => 'en-US', :scope => [:active_record, :error], :count => 1, :object_name => @object_name).returns "1 error prohibited this #{@object_name} from being saved" - I18n.expects(:t).with(@object_name, :default => @object_name).once.returns @object_name + I18n.expects(:t).with(:header, :locale => 'en-US', :scope => [:activerecord, :errors, :template], :count => 1, :model => @object_name).returns "1 error prohibited this #{@object_name} from being saved" + I18n.expects(:t).with(@object_name, :default => @object_name, :count => 1, :scope => [:activerecord, :models]).once.returns @object_name error_messages_for(:object => @object, :locale => 'en-US', :object_name => @object_name) end end -end \ No newline at end of file +end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 29c2995334..02a0ca7e44 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1220,13 +1220,35 @@ module ActiveRecord #:nodoc: subclasses.each { |klass| klass.reset_inheritable_attributes; klass.reset_column_information } end + def self_and_descendents_from_active_record#nodoc: + klass = self + classes = [klass] + while klass != klass.base_class + classes << klass = klass.superclass + end + classes + rescue + # OPTIMIZE this rescue is to fix this test: ./test/cases/reflection_test.rb:56:in `test_human_name_for_column' + # Appearantly the method base_class causes some trouble. + # It now works for sure. + [self] + end + # Transforms attribute key names into a more humane format, such as "First name" instead of "first_name". Example: # Person.human_attribute_name("first_name") # => "First name" - # Deprecated in favor of just calling "first_name".humanize - def human_attribute_name(attribute_key_name) #:nodoc: - attribute_key_name.humanize + # This used to be depricated in favor of humanize, but is now preferred, because it automatically uses the I18n + # module now. + # Specify +options+ with additional translating options. + def human_attribute_name(attribute_key_name, options = {}) + defaults = self_and_descendents_from_active_record.map do |klass| + :"#{klass.name.underscore}.#{attribute_key_name}" + end + defaults << options[:default] if options[:default] + defaults.flatten! + defaults << attribute_key_name.humanize + options[:count] ||= 1 + I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:activerecord, :attributes])) end - # True if this isn't a concrete subclass needing a STI type condition. def descends_from_active_record? if superclass.abstract_class? diff --git a/activerecord/lib/active_record/locale/en-US.rb b/activerecord/lib/active_record/locale/en-US.rb index 4057a467e3..89a5baba06 100644 --- a/activerecord/lib/active_record/locale/en-US.rb +++ b/activerecord/lib/active_record/locale/en-US.rb @@ -1,26 +1,28 @@ { :'en-US' => { - :active_record => { - :error_messages => { - :inclusion => "is not included in the list", - :exclusion => "is reserved", - :invalid => "is invalid", - :confirmation => "doesn't match confirmation", - :accepted => "must be accepted", - :empty => "can't be empty", - :blank => "can't be blank", - :too_long => "is too long (maximum is {{count}} characters)", - :too_short => "is too short (minimum is {{count}} characters)", - :wrong_length => "is the wrong length (should be {{count}} characters)", - :taken => "has already been taken", - :not_a_number => "is not a number", - :greater_than => "must be greater than {{count}}", - :greater_than_or_equal_to => "must be greater than or equal to {{count}}", - :equal_to => "must be equal to {{count}}", - :less_than => "must be less than {{count}}", - :less_than_or_equal_to => "must be less than or equal to {{count}}", - :odd => "must be odd", - :even => "must be even" - } + :activerecord => { + :errors => { + :messages => { + :inclusion => "is not included in the list", + :exclusion => "is reserved", + :invalid => "is invalid", + :confirmation => "doesn't match confirmation", + :accepted => "must be accepted", + :empty => "can't be empty", + :blank => "can't be blank", + :too_long => "is too long (maximum is {{count}} characters)", + :too_short => "is too short (minimum is {{count}} characters)", + :wrong_length => "is the wrong length (should be {{count}} characters)", + :taken => "has already been taken", + :not_a_number => "is not a number", + :greater_than => "must be greater than {{count}}", + :greater_than_or_equal_to => "must be greater than or equal to {{count}}", + :equal_to => "must be equal to {{count}}", + :less_than => "must be less than {{count}}", + :less_than_or_equal_to => "must be less than or equal to {{count}}", + :odd => "must be odd", + :even => "must be even" + } + } } } -} \ No newline at end of file +} diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 85b8e6232b..92b3e9a597 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -21,8 +21,8 @@ module ActiveRecord class << self def default_error_messages - ActiveSupport::Deprecation.warn("ActiveRecord::Errors.default_error_messages has been deprecated. Please use I18n.translate('active_record.error_messages').") - I18n.translate 'active_record.error_messages' + ActiveSupport::Deprecation.warn("ActiveRecord::Errors.default_error_messages has been deprecated. Please use I18n.translate('activerecord.errors.messages').") + I18n.translate 'activerecord.errors.messages' end end @@ -38,22 +38,24 @@ module ActiveRecord add(:base, msg) end - # Adds an error message (+msg+) to the +attribute+, which will be returned on a call to on(attribute) + # Adds an error message (+messsage+) to the +attribute+, which will be returned on a call to on(attribute) # for the same attribute and ensure that this error object returns false when asked if empty?. More than one # error can be added to the same +attribute+ in which case an array will be returned on a call to on(attribute). - # If no +msg+ is supplied, "invalid" is assumed. - def add(attribute, message = nil) - message ||= I18n.translate :"active_record.error_messages.invalid" + # If no +messsage+ is supplied, :invalid is assumed. + # If +message+ is a Symbol, it will be translated, using the appropriate scope (see translate_error). + def add(attribute, message = nil, options = {}) + message ||= :invalid + message = generate_message(attribute, message, options) if message.is_a?(Symbol) @errors[attribute.to_s] ||= [] @errors[attribute.to_s] << message - end + end # Will add an error message to each of the attributes in +attributes+ that is empty. def add_on_empty(attributes, custom_message = nil) for attr in [attributes].flatten value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] is_empty = value.respond_to?("empty?") ? value.empty? : false - add(attr, generate_message(attr, :empty, :default => custom_message)) unless !value.nil? && !is_empty + add(attr, :empty, :default => custom_message) unless !value.nil? && !is_empty end end @@ -61,16 +63,48 @@ module ActiveRecord def add_on_blank(attributes, custom_message = nil) for attr in [attributes].flatten value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] - add(attr, generate_message(attr, :blank, :default => custom_message)) if value.blank? + add(attr, :blank, :default => custom_message) if value.blank? end end - def generate_message(attr, key, options = {}) - msgs = base_classes(@base.class).map{|klass| :"custom.#{klass.name.underscore}.#{attr}.#{key}"} - msgs << options[:default] if options[:default] - msgs << key + # Translates an error message in it's default scope (activerecord.errrors.messages). + # Error messages are first looked up in custom.MODEL.ATTRIBUTE.MESSAGE, if it's not there, it's looked up + # in custom.MODEL.ATTRIBUTE and if that is not there it returns the translation of the default message + # (e.g. activerecord.errors.messages.MESSAGE). Both the model name and the attribute name are available for + # interpolation. + # + # When using inheritence in your models, it will check all the inherited models too, but only if the model itself + # hasn't been found. Say you have class Admin < User; end and you wanted the translation for the :blank + # error +message+ for the title +attribute+, it looks for these translations: + # + #
    + #
  1. activerecord.errors.messages.custom.admin.title.blank
  2. + #
  3. activerecord.errors.messages.custom.admin.blank
  4. + #
  5. activerecord.errors.messages.custom.user.title.blank
  6. + #
  7. activerecord.errors.messages.custom.user.blank
  8. + #
  9. activerecord.errors.messages.blank
  10. + #
  11. any default you provided through the +options+ hash
  12. + #
+ def generate_message(attribute, message = :invalid, options = {}) + + defaults = @base.class.self_and_descendents_from_active_record.map do |klass| + [ :"custom.#{klass.name.underscore}.#{attribute}.#{message}", + :"custom.#{klass.name.underscore}.#{message}" ] + end + + defaults << options[:default] if options[:default] + defaults.flatten! << message + + model_name = @base.class.name + key = defaults.shift - I18n.t msgs.shift, options.merge(:default => msgs, :scope => [:active_record, :error_messages]) + options.merge!({ + :default => defaults, + :model => I18n.translate(model_name.underscore, :default => model_name.humanize, :scope => [:activerecord, :models], :count => 1), + :attribute => @base.class.human_attribute_name(attribute.to_s), + :scope => [:activerecord, :errors, :messages] }) + + I18n.translate(key, options) end # Returns true if the specified +attribute+ has errors associated with it. @@ -166,9 +200,9 @@ module ActiveRecord if attr == "base" full_messages << message else - key = :"active_record.human_attribute_names.#{@base.class.name.underscore.to_sym}.#{attr}" - attr_name = I18n.translate(key, :locale => options[:locale], :default => @base.class.human_attribute_name(attr)) - full_messages << attr_name + " " + message + #key = :"activerecord.att.#{@base.class.name.underscore.to_sym}.#{attr}" + attr_name = @base.class.human_attribute_name(attr) + full_messages << attr_name + ' ' + message end end end @@ -219,16 +253,6 @@ module ActiveRecord end end - protected - - # TODO maybe this should be on ActiveRecord::Base, maybe #self_and_descendents_from_active_record - def base_classes(klass) - classes = [klass] - while klass != klass.base_class - classes << klass = klass.superclass - end - classes - end end @@ -398,8 +422,7 @@ module ActiveRecord validates_each(attr_names, configuration) do |record, attr_name, value| unless record.send("#{attr_name}_confirmation").nil? or value == record.send("#{attr_name}_confirmation") - message = record.errors.generate_message(attr_name, :confirmation, :default => configuration[:message]) - record.errors.add(attr_name, message) + record.errors.add(attr_name, :confirmation, :default => configuration[:message]) end end end @@ -441,8 +464,7 @@ module ActiveRecord validates_each(attr_names,configuration) do |record, attr_name, value| unless value == configuration[:accept] - message = record.errors.generate_message(attr_name, :accepted, :default => configuration[:message]) - record.errors.add(attr_name, message) + record.errors.add(attr_name, :accepted, :default => configuration[:message]) end end end @@ -544,11 +566,9 @@ module ActiveRecord validates_each(attrs, options) do |record, attr, value| value = options[:tokenizer].call(value) if value.kind_of?(String) if value.nil? or value.size < option_value.begin - message = record.errors.generate_message(attr, :too_short, :default => options[:too_short], :count => option_value.begin) - record.errors.add(attr, message) + record.errors.add(attr, :too_short, :default => options[:too_short], :count => option_value.begin) elsif value.size > option_value.end - message = record.errors.generate_message(attr, :too_long, :default => options[:too_long], :count => option_value.end) - record.errors.add(attr, message) + record.errors.add(attr, :too_long, :default => options[:too_long], :count => option_value.end) end end when :is, :minimum, :maximum @@ -563,8 +583,7 @@ module ActiveRecord unless !value.nil? and value.size.method(validity_checks[option])[option_value] key = message_options[option] custom_message = options[:message] || options[key] - message = record.errors.generate_message(attr, key, :default => custom_message, :count => option_value) - record.errors.add(attr, message) + record.errors.add(attr, key, :default => custom_message, :count => option_value) end end end @@ -662,8 +681,7 @@ module ActiveRecord finder_class.with_exclusive_scope do if finder_class.exists?([condition_sql, *condition_params]) - message = record.errors.generate_message(attr_name, :taken, :default => configuration[:message], :value => value) - record.errors.add(attr_name, message) + record.errors.add(attr_name, :taken, :default => configuration[:message], :value => value) end end end @@ -701,8 +719,7 @@ module ActiveRecord validates_each(attr_names, configuration) do |record, attr_name, value| unless value.to_s =~ configuration[:with] - message = record.errors.generate_message(attr_name, :invalid, :default => configuration[:message], :value => value) - record.errors.add(attr_name, message) + record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) end end end @@ -736,8 +753,7 @@ module ActiveRecord validates_each(attr_names, configuration) do |record, attr_name, value| unless enum.include?(value) - message = record.errors.generate_message(attr_name, :inclusion, :default => configuration[:message], :value => value) - record.errors.add(attr_name, message) + record.errors.add(attr_name, :inclusion, :default => configuration[:message], :value => value) end end end @@ -771,8 +787,7 @@ module ActiveRecord validates_each(attr_names, configuration) do |record, attr_name, value| if enum.include?(value) - message = record.errors.generate_message(attr_name, :exclusion, :default => configuration[:message], :value => value) - record.errors.add(attr_name, message) + record.errors.add(attr_name, :exclusion, :default => configuration[:message], :value => value) end end end @@ -814,8 +829,7 @@ module ActiveRecord validates_each(attr_names, configuration) do |record, attr_name, value| unless (value.is_a?(Array) ? value : [value]).inject(true) { |v, r| (r.nil? || r.valid?) && v } - message = record.errors.generate_message(attr_name, :invalid, :default => configuration[:message], :value => value) - record.errors.add(attr_name, message) + record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) end end end @@ -864,8 +878,7 @@ module ActiveRecord if configuration[:only_integer] unless raw_value.to_s =~ /\A[+-]?\d+\Z/ - message = record.errors.generate_message(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message]) - record.errors.add(attr_name, message) + record.errors.add(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message]) next end raw_value = raw_value.to_i @@ -873,8 +886,7 @@ module ActiveRecord begin raw_value = Kernel.Float(raw_value) rescue ArgumentError, TypeError - message = record.errors.generate_message(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message]) - record.errors.add(attr_name, message) + record.errors.add(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message]) next end end @@ -883,12 +895,10 @@ module ActiveRecord case option when :odd, :even unless raw_value.to_i.method(ALL_NUMERICALITY_CHECKS[option])[] - message = record.errors.generate_message(attr_name, option, :value => raw_value, :default => configuration[:message]) - record.errors.add(attr_name, message) + record.errors.add(attr_name, option, :value => raw_value, :default => configuration[:message]) end else - message = record.errors.generate_message(attr_name, option, :default => configuration[:message], :value => raw_value, :count => configuration[option]) - record.errors.add(attr_name, message) unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]] + record.errors.add(attr_name, option, :default => configuration[:message], :value => raw_value, :count => configuration[option]) unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]] end end end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index a0beb8be1b..e110595437 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -6,7 +6,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase def setup reset_callbacks Topic @topic = Topic.new - I18n.backend.store_translations('en-US', :active_record => {:error_messages => {:custom => nil}}) + I18n.backend.store_translations('en-US', :activerecord => {:errors => {:messages => {:custom => nil}}}) end def teardown @@ -43,20 +43,23 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # ActiveRecord::Errors uses_mocha 'ActiveRecord::Errors' do def test_errors_generate_message_translates_custom_model_attribute_key - global_scope = [:active_record, :error_messages] + global_scope = [:activerecord, :errors, :messages] custom_scope = global_scope + [:custom, 'topic', :title] - - I18n.expects(:t).with :"custom.topic.title.invalid", :scope => [:active_record, :error_messages], :default => ['default from class def', :invalid] - @topic.errors.generate_message :title, :invalid, :default => 'default from class def' + + I18n.expects(:translate).with('topic', {:count => 1, :default => 'Topic', :scope => [:activerecord, :models]}).returns('Topic') + I18n.expects(:translate).with(:'topic.title', {:count => 1, :default => ['Title'], :scope => [:activerecord, :attributes]}).returns('Title') + I18n.expects(:translate).with(:"custom.topic.title.invalid", :scope => global_scope, :default => [:"custom.topic.invalid", 'default from class def error 1', :invalid], :attribute => "Title", :model => "Topic").returns('default from class def error 1') + @topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1' end - + def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti - custom_scope = [:active_record, :error_messages, :custom, 'topic', :title] - - I18n.expects(:t).with :"custom.reply.title.invalid", :scope => [:active_record, :error_messages], :default => [:"custom.topic.title.invalid", 'default from class def', :invalid] + custom_scope = [:activerecord, :errors, :custom, 'topic', :title] + I18n.expects(:translate).with('reply', {:count => 1, :default => 'Reply', :scope => [:activerecord, :models]}).returns('Reply') + I18n.expects(:translate).with(:'reply.title', {:count => 1, :default => [:'topic.title', 'Title'], :scope => [:activerecord, :attributes]}).returns('Title') + I18n.expects(:translate).with(:"custom.reply.title.invalid", :scope => [:activerecord, :errors, :messages], :default => [:"custom.reply.invalid", :"custom.topic.title.invalid", :"custom.topic.invalid", 'default from class def', :invalid], :model => 'Reply', :attribute => 'Title').returns("default from class def") Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def' end - + def test_errors_add_on_empty_generates_message @topic.errors.expects(:generate_message).with(:title, :empty, {:default => nil}) @topic.errors.add_on_empty :title @@ -79,7 +82,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase def test_errors_full_messages_translates_human_attribute_name_for_model_attributes @topic.errors.instance_variable_set :@errors, { 'title' => 'empty' } - I18n.expects(:translate).with(:"active_record.human_attribute_names.topic.title", :locale => 'en-US', :default => 'Title').returns('Title') + I18n.expects(:translate).with(:"topic.title", :default => ['Title'], :scope => [:activerecord, :attributes], :count => 1).returns('Title') @topic.errors.full_messages :locale => 'en-US' end end @@ -344,8 +347,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_confirmation_of w/o mocha def test_validates_confirmation_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:confirmation => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:confirmation => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:confirmation => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}} Topic.validates_confirmation_of :title @topic.title_confirmation = 'foo' @@ -354,7 +357,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_confirmation_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:confirmation => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}} Topic.validates_confirmation_of :title @topic.title_confirmation = 'foo' @@ -365,8 +368,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_acceptance_of w/o mocha def test_validates_acceptance_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:accepted => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:accepted => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:accepted => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}} Topic.validates_acceptance_of :title, :allow_nil => false @topic.valid? @@ -374,7 +377,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_acceptance_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:accepted => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}} Topic.validates_acceptance_of :title, :allow_nil => false @topic.valid? @@ -384,8 +387,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_presence_of w/o mocha def test_validates_presence_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:blank => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:blank => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:blank => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:blank => 'global message'}}} Topic.validates_presence_of :title @topic.valid? @@ -393,7 +396,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_presence_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:blank => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:blank => 'global message'}}} Topic.validates_presence_of :title @topic.valid? @@ -403,8 +406,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_length_of :within w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:too_short => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:too_short => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:too_short => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}} Topic.validates_length_of :title, :within => 3..5 @topic.valid? @@ -412,7 +415,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_length_of_within_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:too_short => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}} Topic.validates_length_of :title, :within => 3..5 @topic.valid? @@ -422,8 +425,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_length_of :is w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:wrong_length => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:wrong_length => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:wrong_length => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @topic.valid? @@ -431,7 +434,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_length_of_within_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:wrong_length => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @topic.valid? @@ -441,8 +444,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_uniqueness_of w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:wrong_length => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:wrong_length => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:wrong_length => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @topic.valid? @@ -450,7 +453,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_length_of_within_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:wrong_length => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @topic.valid? @@ -461,8 +464,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_format_of w/o mocha def test_validates_format_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:invalid => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:invalid => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:invalid => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/ @topic.valid? @@ -470,7 +473,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_format_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:invalid => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/ @topic.valid? @@ -480,8 +483,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_inclusion_of w/o mocha def test_validates_inclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:inclusion => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:inclusion => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:inclusion => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}} Topic.validates_inclusion_of :title, :in => %w(a b c) @topic.valid? @@ -489,7 +492,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_inclusion_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:inclusion => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}} Topic.validates_inclusion_of :title, :in => %w(a b c) @topic.valid? @@ -499,8 +502,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_exclusion_of w/o mocha def test_validates_exclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:exclusion => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:exclusion => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:exclusion => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}} Topic.validates_exclusion_of :title, :in => %w(a b c) @topic.title = 'a' @@ -509,7 +512,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_exclusion_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:exclusion => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}} Topic.validates_exclusion_of :title, :in => %w(a b c) @topic.title = 'a' @@ -520,8 +523,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_numericality_of without :only_integer w/o mocha def test_validates_numericality_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:not_a_number => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:not_a_number => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:not_a_number => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title @topic.title = 'a' @@ -530,7 +533,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_numericality_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:not_a_number => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true @topic.title = 'a' @@ -541,8 +544,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_numericality_of with :only_integer w/o mocha def test_validates_numericality_of_only_integer_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:not_a_number => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:not_a_number => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:not_a_number => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true @topic.title = 'a' @@ -551,7 +554,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_numericality_of_only_integer_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:not_a_number => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true @topic.title = 'a' @@ -562,8 +565,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_numericality_of :odd w/o mocha def test_validates_numericality_of_odd_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:odd => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:odd => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:odd => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:odd => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :odd => true @topic.title = 0 @@ -572,7 +575,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_numericality_of_odd_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:odd => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:odd => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :odd => true @topic.title = 0 @@ -583,8 +586,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_numericality_of :less_than w/o mocha def test_validates_numericality_of_less_than_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:less_than => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:less_than => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:less_than => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0 @topic.title = 1 @@ -593,7 +596,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_numericality_of_less_than_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:less_than => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0 @topic.title = 1 @@ -605,8 +608,8 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_associated w/o mocha def test_validates_associated_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:replies => {:invalid => 'custom message'}}}}} - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:invalid => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:replies => {:invalid => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_associated :replies replied_topic.valid? @@ -614,7 +617,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase end def test_validates_associated_finds_global_default_translation - I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:invalid => 'global message'}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_associated :replies replied_topic.valid? @@ -627,27 +630,29 @@ class ActiveRecordValidationsGenerateMessageI18nTests < Test::Unit::TestCase reset_callbacks Topic @topic = Topic.new I18n.backend.store_translations :'en-US', { - :active_record => { - :error_messages => { - :inclusion => "is not included in the list", - :exclusion => "is reserved", - :invalid => "is invalid", - :confirmation => "doesn't match confirmation", - :accepted => "must be accepted", - :empty => "can't be empty", - :blank => "can't be blank", - :too_long => "is too long (maximum is {{count}} characters)", - :too_short => "is too short (minimum is {{count}} characters)", - :wrong_length => "is the wrong length (should be {{count}} characters)", - :taken => "has already been taken", - :not_a_number => "is not a number", - :greater_than => "must be greater than {{count}}", - :greater_than_or_equal_to => "must be greater than or equal to {{count}}", - :equal_to => "must be equal to {{count}}", - :less_than => "must be less than {{count}}", - :less_than_or_equal_to => "must be less than or equal to {{count}}", - :odd => "must be odd", - :even => "must be even" + :activerecord => { + :errors => { + :messages => { + :inclusion => "is not included in the list", + :exclusion => "is reserved", + :invalid => "is invalid", + :confirmation => "doesn't match confirmation", + :accepted => "must be accepted", + :empty => "can't be empty", + :blank => "can't be blank", + :too_long => "is too long (maximum is {{count}} characters)", + :too_short => "is too short (minimum is {{count}} characters)", + :wrong_length => "is the wrong length (should be {{count}} characters)", + :taken => "has already been taken", + :not_a_number => "is not a number", + :greater_than => "must be greater than {{count}}", + :greater_than_or_equal_to => "must be greater than or equal to {{count}}", + :equal_to => "must be equal to {{count}}", + :less_than => "must be less than {{count}}", + :less_than_or_equal_to => "must be less than or equal to {{count}}", + :odd => "must be odd", + :even => "must be even" + } } } } @@ -798,4 +803,4 @@ class ActiveRecordValidationsGenerateMessageI18nTests < Test::Unit::TestCase def test_generate_message_even_with_default_message assert_equal "must be even", @topic.errors.generate_message(:title, :even, :default => nil, :value => 'title', :count => 10) end -end \ No newline at end of file +end -- cgit v1.2.3 From c379582064b4f345ecb6bab999e2f1c5685313a8 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 19 Aug 2008 14:25:51 +0200 Subject: switch to class and instance instead of a module for the simple backend --- .../lib/active_support/vendor/i18n-0.0.1/i18n.rb | 4 +- .../vendor/i18n-0.0.1/i18n/backend/simple.rb | 334 ++++++++++----------- 2 files changed, 169 insertions(+), 169 deletions(-) diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb index 5c6c4eb783..145840aa36 100755 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb @@ -9,14 +9,14 @@ require 'i18n/backend/simple' require 'i18n/exceptions' module I18n - @@backend = Backend::Simple + @@backend = nil @@default_locale = 'en-US' @@exception_handler = :default_exception_handler class << self # Returns the current backend. Defaults to +Backend::Simple+. def backend - @@backend + @@backend ||= Backend::Simple.new end # Sets the current backend. Used to set a custom backend. diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb index 530e9eca50..63ef55631f 100644 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb @@ -2,188 +2,188 @@ require 'strscan' module I18n module Backend - module Simple - @@translations = {} + class Simple + # Allow client libraries to pass a block that populates the translation + # storage. Decoupled for backends like a db backend that persist their + # translations, so the backend can decide whether/when to yield or not. + def populate(&block) + yield + end + + # Accepts a list of paths to translation files. Loads translations from + # plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml + # for details. + def load_translations(*filenames) + filenames.each {|filename| load_file filename } + end - class << self - # Allow client libraries to pass a block that populates the translation - # storage. Decoupled for backends like a db backend that persist their - # translations, so the backend can decide whether/when to yield or not. - def populate(&block) - yield - end + # Stores translations for the given locale in memory. + # This uses a deep merge for the translations hash, so existing + # translations will be overwritten by new ones only at the deepest + # level of the hash. + def store_translations(locale, data) + merge_translations(locale, data) + end - # Accepts a list of paths to translation files. Loads translations from - # plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml - # for details. - def load_translations(*filenames) - filenames.each {|filename| load_file filename } + def translate(locale, key, options = {}) + raise InvalidLocale.new(locale) if locale.nil? + return key.map{|key| translate locale, key, options } if key.is_a? Array + + reserved = :scope, :default + count, scope, default = options.values_at(:count, *reserved) + options.delete(:default) + values = options.reject{|name, value| reserved.include? name } + + entry = lookup(locale, key, scope) || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options)) + entry = pluralize locale, entry, count + entry = interpolate locale, entry, values + entry + end + + # Acts the same as +strftime+, but returns a localized version of the + # formatted date string. Takes a key from the date/time formats + # translations as a format argument (e.g., :short in :'date.formats'). + def localize(locale, object, format = :default) + raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime) + + type = object.respond_to?(:sec) ? 'time' : 'date' + formats = translate(locale, :"#{type}.formats") + format = formats[format.to_sym] if formats && formats[format.to_sym] + # TODO raise exception unless format found? + format = format.to_s.dup + + format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday]) + format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday]) + format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon]) + format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon]) + format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour + object.strftime(format) + end + + protected + + def translations + @translations ||= {} end - # Stores translations for the given locale in memory. - # This uses a deep merge for the translations hash, so existing - # translations will be overwritten by new ones only at the deepest - # level of the hash. - def store_translations(locale, data) - merge_translations(locale, data) + # Looks up a translation from the translations hash. Returns nil if + # eiher key is nil, or locale, scope or key do not exist as a key in the + # nested translations hash. Splits keys or scopes containing dots + # into multiple keys, i.e. currency.format is regarded the same as + # %w(currency format). + def lookup(locale, key, scope = []) + return unless key + keys = I18n.send :normalize_translation_keys, locale, key, scope + keys.inject(translations){|result, key| result[key.to_sym] or return nil } + end + + # Evaluates a default translation. + # If the given default is a String it is used literally. If it is a Symbol + # it will be translated with the given options. If it is an Array the first + # translation yielded will be returned. + # + # I.e., default(locale, [:foo, 'default']) will return +default+ if + # translate(locale, :foo) does not yield a result. + def default(locale, default, options = {}) + case default + when String then default + when Symbol then translate locale, default, options + when Array then default.each do |obj| + result = default(locale, obj, options.dup) and return result + end and nil + end + rescue MissingTranslationData + nil + end + + # Picks a translation from an array according to English pluralization + # rules. It will pick the first translation if count is not equal to 1 + # and the second translation if it is equal to 1. Other backends can + # implement more flexible or complex pluralization rules. + def pluralize(locale, entry, count) + return entry unless entry.is_a?(Hash) and count + # raise InvalidPluralizationData.new(entry, count) unless entry.is_a?(Hash) + key = :zero if count == 0 && entry.has_key?(:zero) + key ||= count == 1 ? :one : :many + raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key) + entry[key] end + + # Interpolates values into a given string. + # + # interpolate "file {{file}} opened by \\{{user}}", :file => 'test.txt', :user => 'Mr. X' + # # => "file test.txt opened by {{user}}" + # + # Note that you have to double escape the \\ when you want to escape + # the {{...}} key in a string (once for the string and once for the + # interpolation). + def interpolate(locale, string, values = {}) + return string if !string.is_a?(String) + + map = {'%d' => '{{count}}', '%s' => '{{value}}'} # TODO deprecate this? + string.gsub!(/#{map.keys.join('|')}/){|key| map[key]} - def translate(locale, key, options = {}) - raise InvalidLocale.new(locale) if locale.nil? - return key.map{|key| translate locale, key, options } if key.is_a? Array + s = StringScanner.new string.dup + while s.skip_until(/\{\{/) + s.string[s.pos - 3, 1] = '' and next if s.pre_match[-1, 1] == '\\' + start_pos = s.pos - 2 + key = s.scan_until(/\}\}/)[0..-3] + end_pos = s.pos - 1 - reserved = :scope, :default - count, scope, default = options.values_at(:count, *reserved) - options.delete(:default) - values = options.reject{|name, value| reserved.include? name } + raise ReservedInterpolationKey.new(key, string) if %w(scope default).include?(key) + raise MissingInterpolationArgument.new(key, string) unless values.has_key? key.to_sym - entry = lookup(locale, key, scope) || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options)) - entry = pluralize locale, entry, count - entry = interpolate locale, entry, values - entry + s.string[start_pos..end_pos] = values[key.to_sym].to_s + s.unscan + end + s.string end - # Acts the same as +strftime+, but returns a localized version of the - # formatted date string. Takes a key from the date/time formats - # translations as a format argument (e.g., :short in :'date.formats'). - def localize(locale, object, format = :default) - raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime) - - type = object.respond_to?(:sec) ? 'time' : 'date' - formats = translate(locale, :"#{type}.formats") - format = formats[format.to_sym] if formats && formats[format.to_sym] - # TODO raise exception unless format found? - format = format.to_s.dup - - format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday]) - format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday]) - format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon]) - format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon]) - format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour - object.strftime(format) + # Loads a single translations file by delegating to #load_rb or + # #load_yml depending on the file extension and directly merges the + # data to the existing translations. Raises I18n::UnknownFileType + # for all other file extensions. + def load_file(filename) + type = File.extname(filename).tr('.', '').downcase + raise UnknownFileType.new(type, filename) unless respond_to? :"load_#{type}" + data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash + data.each do |locale, data| + merge_translations locale, data + end end - protected - - # Looks up a translation from the translations hash. Returns nil if - # eiher key is nil, or locale, scope or key do not exist as a key in the - # nested translations hash. Splits keys or scopes containing dots - # into multiple keys, i.e. currency.format is regarded the same as - # %w(currency format). - def lookup(locale, key, scope = []) - return unless key - keys = I18n.send :normalize_translation_keys, locale, key, scope - keys.inject(@@translations){|result, key| result[key.to_sym] or return nil } - end + # Loads a plain Ruby translations file. eval'ing the file must yield + # a Hash containing translation data with locales as toplevel keys. + def load_rb(filename) + eval IO.read(filename), binding, filename + end - # Evaluates a default translation. - # If the given default is a String it is used literally. If it is a Symbol - # it will be translated with the given options. If it is an Array the first - # translation yielded will be returned. - # - # I.e., default(locale, [:foo, 'default']) will return +default+ if - # translate(locale, :foo) does not yield a result. - def default(locale, default, options = {}) - case default - when String then default - when Symbol then translate locale, default, options - when Array then default.each do |obj| - result = default(locale, obj, options.dup) and return result - end and nil - end - rescue MissingTranslationData - nil - end + # Loads a YAML translations file. The data must have locales as + # toplevel keys. + def load_yml(filename) + YAML::load IO.read(filename) + end - # Picks a translation from an array according to English pluralization - # rules. It will pick the first translation if count is not equal to 1 - # and the second translation if it is equal to 1. Other backends can - # implement more flexible or complex pluralization rules. - def pluralize(locale, entry, count) - return entry unless entry.is_a?(Hash) and count - # raise InvalidPluralizationData.new(entry, count) unless entry.is_a?(Hash) - key = :zero if count == 0 && entry.has_key?(:zero) - key ||= count == 1 ? :one : :many - raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key) - entry[key] - end - - # Interpolates values into a given string. - # - # interpolate "file {{file}} opened by \\{{user}}", :file => 'test.txt', :user => 'Mr. X' - # # => "file test.txt opened by {{user}}" - # - # Note that you have to double escape the \\ when you want to escape - # the {{...}} key in a string (once for the string and once for the - # interpolation). - def interpolate(locale, string, values = {}) - return string if !string.is_a?(String) - - map = {'%d' => '{{count}}', '%s' => '{{value}}'} # TODO deprecate this? - string.gsub!(/#{map.keys.join('|')}/){|key| map[key]} - - s = StringScanner.new string.dup - while s.skip_until(/\{\{/) - s.string[s.pos - 3, 1] = '' and next if s.pre_match[-1, 1] == '\\' - start_pos = s.pos - 2 - key = s.scan_until(/\}\}/)[0..-3] - end_pos = s.pos - 1 - - raise ReservedInterpolationKey.new(key, string) if %w(scope default).include?(key) - raise MissingInterpolationArgument.new(key, string) unless values.has_key? key.to_sym - - s.string[start_pos..end_pos] = values[key.to_sym].to_s - s.unscan - end - s.string - end - - # Loads a single translations file by delegating to #load_rb or - # #load_yml depending on the file extension and directly merges the - # data to the existing translations. Raises I18n::UnknownFileType - # for all other file extensions. - def load_file(filename) - type = File.extname(filename).tr('.', '').downcase - raise UnknownFileType.new(type, filename) unless respond_to? :"load_#{type}" - data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash - data.each do |locale, data| - merge_translations locale, data - end - end - - # Loads a plain Ruby translations file. eval'ing the file must yield - # a Hash containing translation data with locales as toplevel keys. - def load_rb(filename) - eval IO.read(filename), binding, filename - end - - # Loads a YAML translations file. The data must have locales as - # toplevel keys. - def load_yml(filename) - YAML::load IO.read(filename) - end - - # Deep merges the given translations hash with the existing translations - # for the given locale - def merge_translations(locale, data) - locale = locale.to_sym - @@translations[locale] ||= {} - data = deep_symbolize_keys data + # Deep merges the given translations hash with the existing translations + # for the given locale + def merge_translations(locale, data) + locale = locale.to_sym + translations[locale] ||= {} + data = deep_symbolize_keys data - # deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809 - merger = proc{|key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 } - @@translations[locale].merge! data, &merger - end - - # Return a new hash with all keys and nested keys converted to symbols. - def deep_symbolize_keys(hash) - hash.inject({}){|result, (key, value)| - value = deep_symbolize_keys(value) if value.is_a? Hash - result[(key.to_sym rescue key) || key] = value - result - } - end - end + # deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809 + merger = proc{|key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 } + translations[locale].merge! data, &merger + end + + # Return a new hash with all keys and nested keys converted to symbols. + def deep_symbolize_keys(hash) + hash.inject({}){|result, (key, value)| + value = deep_symbolize_keys(value) if value.is_a? Hash + result[(key.to_sym rescue key) || key] = value + result + } + end end end end -- cgit v1.2.3 From ae8a35d8f6d5db6ae9a1877918d45c15d21e24fe Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Sat, 16 Aug 2008 20:01:42 +0200 Subject: Added Base.human_name method --- activerecord/lib/active_record/base.rb | 13 ++++++++++ activerecord/test/cases/i18n_test.rb | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 activerecord/test/cases/i18n_test.rb diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 02a0ca7e44..edab017d26 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1249,6 +1249,19 @@ module ActiveRecord #:nodoc: options[:count] ||= 1 I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:activerecord, :attributes])) end + + # Transform the modelname into a more humane format, using I18n. + # Defaults to the basic humanize method. + # Default scope of the translation is activerecord.models + # Specify +options+ with additional translating options. + def human_name(options = {}) + defaults = self_and_descendents_from_active_record.map do |klass| + :"#{klass.name.underscore}" + end + defaults << self.name.humanize + I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options)) + end + # True if this isn't a concrete subclass needing a STI type condition. def descends_from_active_record? if superclass.abstract_class? diff --git a/activerecord/test/cases/i18n_test.rb b/activerecord/test/cases/i18n_test.rb new file mode 100644 index 0000000000..3527644f0d --- /dev/null +++ b/activerecord/test/cases/i18n_test.rb @@ -0,0 +1,46 @@ +require "cases/helper" +require 'models/topic' +require 'models/reply' + +class ActiveRecordI18nTests < Test::Unit::TestCase + + def setup + reset_translations + end + + def test_translated_model_attributes + I18n.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } + assert_equal 'topic title attribute', Topic.human_attribute_name('title') + end + + def test_translated_model_attributes_with_sti + I18n.store_translations 'en-US', :activerecord => {:attributes => {:reply => {:title => 'reply title attribute'} } } + assert_equal 'reply title attribute', Reply.human_attribute_name('title') + end + + def test_translated_model_attributes_with_sti_fallback + I18n.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } + assert_equal 'topic title attribute', Reply.human_attribute_name('title') + end + + def test_translated_model_names + I18n.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} } + assert_equal 'topic model', Topic.human_name + end + + def test_translated_model_names_with_sti + I18n.store_translations 'en-US', :activerecord => {:models => {:reply => 'reply model'} } + assert_equal 'reply model', Reply.human_name + end + + def test_translated_model_names_with_sti_fallback + I18n.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} } + assert_equal 'topic model', Reply.human_name + end + + private + def reset_translations + I18n.backend.send(:class_variable_set, :@@translations, {}) + end +end + -- cgit v1.2.3 From c531248938302477c5e52138d59a6c3d1527d963 Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Sat, 16 Aug 2008 20:22:40 +0200 Subject: Introduced AR::Base.human_name to validations --- activerecord/lib/active_record/validations.rb | 2 +- activerecord/test/cases/validations_i18n_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 92b3e9a597..040681a09c 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -100,7 +100,7 @@ module ActiveRecord options.merge!({ :default => defaults, - :model => I18n.translate(model_name.underscore, :default => model_name.humanize, :scope => [:activerecord, :models], :count => 1), + :model => @base.class.human_name, :attribute => @base.class.human_attribute_name(attribute.to_s), :scope => [:activerecord, :errors, :messages] }) diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index e110595437..96f86fbe0e 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -46,7 +46,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase global_scope = [:activerecord, :errors, :messages] custom_scope = global_scope + [:custom, 'topic', :title] - I18n.expects(:translate).with('topic', {:count => 1, :default => 'Topic', :scope => [:activerecord, :models]}).returns('Topic') + I18n.expects(:translate).with(:topic, {:count => 1, :default => ['Topic'], :scope => [:activerecord, :models]}).returns('Topic') I18n.expects(:translate).with(:'topic.title', {:count => 1, :default => ['Title'], :scope => [:activerecord, :attributes]}).returns('Title') I18n.expects(:translate).with(:"custom.topic.title.invalid", :scope => global_scope, :default => [:"custom.topic.invalid", 'default from class def error 1', :invalid], :attribute => "Title", :model => "Topic").returns('default from class def error 1') @topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1' @@ -54,7 +54,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti custom_scope = [:activerecord, :errors, :custom, 'topic', :title] - I18n.expects(:translate).with('reply', {:count => 1, :default => 'Reply', :scope => [:activerecord, :models]}).returns('Reply') + I18n.expects(:translate).with(:reply, {:count => 1, :default => [:topic, 'Reply'], :scope => [:activerecord, :models]}).returns('Reply') I18n.expects(:translate).with(:'reply.title', {:count => 1, :default => [:'topic.title', 'Title'], :scope => [:activerecord, :attributes]}).returns('Title') I18n.expects(:translate).with(:"custom.reply.title.invalid", :scope => [:activerecord, :errors, :messages], :default => [:"custom.reply.invalid", :"custom.topic.title.invalid", :"custom.topic.invalid", 'default from class def', :invalid], :model => 'Reply', :attribute => 'Title').returns("default from class def") Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def' -- cgit v1.2.3 From e2b191681e1c8c8b4344f1fc755e48fccdd1d603 Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Sat, 16 Aug 2008 21:45:23 +0200 Subject: Added :value as interpolation variable available to error messages --- activerecord/lib/active_record/validations.rb | 18 ++++++++++-------- activerecord/test/cases/validations_i18n_test.rb | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 040681a09c..a442d008a9 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -92,17 +92,19 @@ module ActiveRecord :"custom.#{klass.name.underscore}.#{message}" ] end - defaults << options[:default] if options[:default] - defaults.flatten! << message + defaults << options.delete(:default) + defaults = defaults.compact.flatten << message model_name = @base.class.name key = defaults.shift - - options.merge!({ - :default => defaults, - :model => @base.class.human_name, - :attribute => @base.class.human_attribute_name(attribute.to_s), - :scope => [:activerecord, :errors, :messages] }) + value = @base.send(attribute) if @base.respond_to?(attribute) + + options = { :default => defaults, + :model => @base.class.human_name, + :attribute => @base.class.human_attribute_name(attribute.to_s), + :value => value, + :scope => [:activerecord, :errors, :messages] + }.merge(options) I18n.translate(key, options) end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index 96f86fbe0e..469e9d6c03 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -48,7 +48,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase I18n.expects(:translate).with(:topic, {:count => 1, :default => ['Topic'], :scope => [:activerecord, :models]}).returns('Topic') I18n.expects(:translate).with(:'topic.title', {:count => 1, :default => ['Title'], :scope => [:activerecord, :attributes]}).returns('Title') - I18n.expects(:translate).with(:"custom.topic.title.invalid", :scope => global_scope, :default => [:"custom.topic.invalid", 'default from class def error 1', :invalid], :attribute => "Title", :model => "Topic").returns('default from class def error 1') + I18n.expects(:translate).with(:"custom.topic.title.invalid", :value => nil, :scope => global_scope, :default => [:"custom.topic.invalid", 'default from class def error 1', :invalid], :attribute => "Title", :model => "Topic").returns('default from class def error 1') @topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1' end @@ -56,7 +56,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase custom_scope = [:activerecord, :errors, :custom, 'topic', :title] I18n.expects(:translate).with(:reply, {:count => 1, :default => [:topic, 'Reply'], :scope => [:activerecord, :models]}).returns('Reply') I18n.expects(:translate).with(:'reply.title', {:count => 1, :default => [:'topic.title', 'Title'], :scope => [:activerecord, :attributes]}).returns('Title') - I18n.expects(:translate).with(:"custom.reply.title.invalid", :scope => [:activerecord, :errors, :messages], :default => [:"custom.reply.invalid", :"custom.topic.title.invalid", :"custom.topic.invalid", 'default from class def', :invalid], :model => 'Reply', :attribute => 'Title').returns("default from class def") + I18n.expects(:translate).with(:"custom.reply.title.invalid", :value => nil, :scope => [:activerecord, :errors, :messages], :default => [:"custom.reply.invalid", :"custom.topic.title.invalid", :"custom.topic.invalid", 'default from class def', :invalid], :model => 'Reply', :attribute => 'Title').returns("default from class def") Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def' end -- cgit v1.2.3 From a6a62004c93d792c230d80f8a65da0bb45cb369f Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 20 Aug 2008 17:51:42 +0200 Subject: add human_name and value to ar validation #generate_message --- activerecord/test/cases/i18n_test.rb | 6 +++--- activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/activerecord/test/cases/i18n_test.rb b/activerecord/test/cases/i18n_test.rb index 3527644f0d..06036733f5 100644 --- a/activerecord/test/cases/i18n_test.rb +++ b/activerecord/test/cases/i18n_test.rb @@ -39,8 +39,8 @@ class ActiveRecordI18nTests < Test::Unit::TestCase end private - def reset_translations - I18n.backend.send(:class_variable_set, :@@translations, {}) - end + def reset_translations + I18n.backend = I18n::Backend::Simple.new + end end diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb index 145840aa36..0e2a2d7051 100755 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb @@ -63,7 +63,7 @@ module I18n # files which are either named *.rb and contain plain Ruby Hashes or are # named *.yml and contain YAML data.) def load_translations(*args) - backend.load_translations *args + backend.load_translations(*args) end # Stores translations for the given locale in the backend. -- cgit v1.2.3 From a2d67403ac84fe6fef659ac3b17d430f141cda41 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 20 Aug 2008 17:52:54 +0200 Subject: remove warning in i18n gem --- activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb index 145840aa36..0e2a2d7051 100755 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb @@ -63,7 +63,7 @@ module I18n # files which are either named *.rb and contain plain Ruby Hashes or are # named *.yml and contain YAML data.) def load_translations(*args) - backend.load_translations *args + backend.load_translations(*args) end # Stores translations for the given locale in the backend. -- cgit v1.2.3 From 81e14fada17817dd16a69933bf688969308c22da Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Sat, 16 Aug 2008 20:01:42 +0200 Subject: Added Base.human_name method --- activerecord/lib/active_record/base.rb | 13 ++++++++++ activerecord/test/cases/i18n_test.rb | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 activerecord/test/cases/i18n_test.rb diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 02a0ca7e44..edab017d26 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1249,6 +1249,19 @@ module ActiveRecord #:nodoc: options[:count] ||= 1 I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:activerecord, :attributes])) end + + # Transform the modelname into a more humane format, using I18n. + # Defaults to the basic humanize method. + # Default scope of the translation is activerecord.models + # Specify +options+ with additional translating options. + def human_name(options = {}) + defaults = self_and_descendents_from_active_record.map do |klass| + :"#{klass.name.underscore}" + end + defaults << self.name.humanize + I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options)) + end + # True if this isn't a concrete subclass needing a STI type condition. def descends_from_active_record? if superclass.abstract_class? diff --git a/activerecord/test/cases/i18n_test.rb b/activerecord/test/cases/i18n_test.rb new file mode 100644 index 0000000000..3527644f0d --- /dev/null +++ b/activerecord/test/cases/i18n_test.rb @@ -0,0 +1,46 @@ +require "cases/helper" +require 'models/topic' +require 'models/reply' + +class ActiveRecordI18nTests < Test::Unit::TestCase + + def setup + reset_translations + end + + def test_translated_model_attributes + I18n.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } + assert_equal 'topic title attribute', Topic.human_attribute_name('title') + end + + def test_translated_model_attributes_with_sti + I18n.store_translations 'en-US', :activerecord => {:attributes => {:reply => {:title => 'reply title attribute'} } } + assert_equal 'reply title attribute', Reply.human_attribute_name('title') + end + + def test_translated_model_attributes_with_sti_fallback + I18n.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } + assert_equal 'topic title attribute', Reply.human_attribute_name('title') + end + + def test_translated_model_names + I18n.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} } + assert_equal 'topic model', Topic.human_name + end + + def test_translated_model_names_with_sti + I18n.store_translations 'en-US', :activerecord => {:models => {:reply => 'reply model'} } + assert_equal 'reply model', Reply.human_name + end + + def test_translated_model_names_with_sti_fallback + I18n.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} } + assert_equal 'topic model', Reply.human_name + end + + private + def reset_translations + I18n.backend.send(:class_variable_set, :@@translations, {}) + end +end + -- cgit v1.2.3 From 12d7872071c9d9bd0ba555cd99945690862fa483 Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Sat, 16 Aug 2008 20:22:40 +0200 Subject: Introduced AR::Base.human_name to validations --- activerecord/lib/active_record/validations.rb | 2 +- activerecord/test/cases/validations_i18n_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 92b3e9a597..040681a09c 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -100,7 +100,7 @@ module ActiveRecord options.merge!({ :default => defaults, - :model => I18n.translate(model_name.underscore, :default => model_name.humanize, :scope => [:activerecord, :models], :count => 1), + :model => @base.class.human_name, :attribute => @base.class.human_attribute_name(attribute.to_s), :scope => [:activerecord, :errors, :messages] }) diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index e110595437..96f86fbe0e 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -46,7 +46,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase global_scope = [:activerecord, :errors, :messages] custom_scope = global_scope + [:custom, 'topic', :title] - I18n.expects(:translate).with('topic', {:count => 1, :default => 'Topic', :scope => [:activerecord, :models]}).returns('Topic') + I18n.expects(:translate).with(:topic, {:count => 1, :default => ['Topic'], :scope => [:activerecord, :models]}).returns('Topic') I18n.expects(:translate).with(:'topic.title', {:count => 1, :default => ['Title'], :scope => [:activerecord, :attributes]}).returns('Title') I18n.expects(:translate).with(:"custom.topic.title.invalid", :scope => global_scope, :default => [:"custom.topic.invalid", 'default from class def error 1', :invalid], :attribute => "Title", :model => "Topic").returns('default from class def error 1') @topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1' @@ -54,7 +54,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti custom_scope = [:activerecord, :errors, :custom, 'topic', :title] - I18n.expects(:translate).with('reply', {:count => 1, :default => 'Reply', :scope => [:activerecord, :models]}).returns('Reply') + I18n.expects(:translate).with(:reply, {:count => 1, :default => [:topic, 'Reply'], :scope => [:activerecord, :models]}).returns('Reply') I18n.expects(:translate).with(:'reply.title', {:count => 1, :default => [:'topic.title', 'Title'], :scope => [:activerecord, :attributes]}).returns('Title') I18n.expects(:translate).with(:"custom.reply.title.invalid", :scope => [:activerecord, :errors, :messages], :default => [:"custom.reply.invalid", :"custom.topic.title.invalid", :"custom.topic.invalid", 'default from class def', :invalid], :model => 'Reply', :attribute => 'Title').returns("default from class def") Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def' -- cgit v1.2.3 From e43cdb93f08544304c10198211d17868f0881030 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 20 Aug 2008 17:57:46 +0200 Subject: fix activerecord i18n_test to work with backend classes --- activerecord/test/cases/i18n_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/activerecord/test/cases/i18n_test.rb b/activerecord/test/cases/i18n_test.rb index 3527644f0d..06036733f5 100644 --- a/activerecord/test/cases/i18n_test.rb +++ b/activerecord/test/cases/i18n_test.rb @@ -39,8 +39,8 @@ class ActiveRecordI18nTests < Test::Unit::TestCase end private - def reset_translations - I18n.backend.send(:class_variable_set, :@@translations, {}) - end + def reset_translations + I18n.backend = I18n::Backend::Simple.new + end end -- cgit v1.2.3 From fc0358ae16d5631dd1d996b2cf95a8987f9d0c8d Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Sat, 16 Aug 2008 21:45:23 +0200 Subject: Added :value as interpolation variable available to error messages --- activerecord/lib/active_record/validations.rb | 18 ++++++++++-------- activerecord/test/cases/validations_i18n_test.rb | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 040681a09c..a442d008a9 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -92,17 +92,19 @@ module ActiveRecord :"custom.#{klass.name.underscore}.#{message}" ] end - defaults << options[:default] if options[:default] - defaults.flatten! << message + defaults << options.delete(:default) + defaults = defaults.compact.flatten << message model_name = @base.class.name key = defaults.shift - - options.merge!({ - :default => defaults, - :model => @base.class.human_name, - :attribute => @base.class.human_attribute_name(attribute.to_s), - :scope => [:activerecord, :errors, :messages] }) + value = @base.send(attribute) if @base.respond_to?(attribute) + + options = { :default => defaults, + :model => @base.class.human_name, + :attribute => @base.class.human_attribute_name(attribute.to_s), + :value => value, + :scope => [:activerecord, :errors, :messages] + }.merge(options) I18n.translate(key, options) end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index 96f86fbe0e..469e9d6c03 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -48,7 +48,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase I18n.expects(:translate).with(:topic, {:count => 1, :default => ['Topic'], :scope => [:activerecord, :models]}).returns('Topic') I18n.expects(:translate).with(:'topic.title', {:count => 1, :default => ['Title'], :scope => [:activerecord, :attributes]}).returns('Title') - I18n.expects(:translate).with(:"custom.topic.title.invalid", :scope => global_scope, :default => [:"custom.topic.invalid", 'default from class def error 1', :invalid], :attribute => "Title", :model => "Topic").returns('default from class def error 1') + I18n.expects(:translate).with(:"custom.topic.title.invalid", :value => nil, :scope => global_scope, :default => [:"custom.topic.invalid", 'default from class def error 1', :invalid], :attribute => "Title", :model => "Topic").returns('default from class def error 1') @topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1' end @@ -56,7 +56,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase custom_scope = [:activerecord, :errors, :custom, 'topic', :title] I18n.expects(:translate).with(:reply, {:count => 1, :default => [:topic, 'Reply'], :scope => [:activerecord, :models]}).returns('Reply') I18n.expects(:translate).with(:'reply.title', {:count => 1, :default => [:'topic.title', 'Title'], :scope => [:activerecord, :attributes]}).returns('Title') - I18n.expects(:translate).with(:"custom.reply.title.invalid", :scope => [:activerecord, :errors, :messages], :default => [:"custom.reply.invalid", :"custom.topic.title.invalid", :"custom.topic.invalid", 'default from class def', :invalid], :model => 'Reply', :attribute => 'Title').returns("default from class def") + I18n.expects(:translate).with(:"custom.reply.title.invalid", :value => nil, :scope => [:activerecord, :errors, :messages], :default => [:"custom.reply.invalid", :"custom.topic.title.invalid", :"custom.topic.invalid", 'default from class def', :invalid], :model => 'Reply', :attribute => 'Title').returns("default from class def") Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def' end -- cgit v1.2.3 From febe2ea9775c863cb9744c6343291e550e4628b8 Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Tue, 19 Aug 2008 23:19:57 +0200 Subject: Locale file changed to yaml --- activerecord/lib/active_record.rb | 2 +- activerecord/lib/active_record/locale/en-US.yml | 33 ++++++++++++++++++++++++ activerecord/test/cases/validations_i18n_test.rb | 2 +- 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 activerecord/lib/active_record/locale/en-US.yml diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 08b6b19f7f..bc27d17ccd 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -82,6 +82,6 @@ require 'active_record/connection_adapters/abstract_adapter' require 'active_record/schema_dumper' I18n.backend.populate do - I18n.load_translations File.dirname(__FILE__) + '/active_record/locale/en-US.rb' + I18n.load_translations File.dirname(__FILE__) + '/active_record/locale/en-US.yml' end diff --git a/activerecord/lib/active_record/locale/en-US.yml b/activerecord/lib/active_record/locale/en-US.yml new file mode 100644 index 0000000000..8148f31a81 --- /dev/null +++ b/activerecord/lib/active_record/locale/en-US.yml @@ -0,0 +1,33 @@ +en-US: + activerecord: + errors: + # The values :model, :attribute and :value are always available for interpolation + # The value :count is available when applicable. Can be used for pluralization. + messages: + inclusion: "is not included in the list" + exclusion: "is reserved" + invalid: "is invalid" + confirmation: "doesn't match confirmation" + accepted: "must be accepted" + empty: "can't be empty" + blank: "can't be blank" + too_long: "is too long (maximum is {{count}} characters)" + too_short: "is too short (minimum is {{count}} characters)" + wrong_length: "is the wrong length (should be {{count}} characters)" + taken: "has already been taken" + not_a_number: "is not a number" + greater_than: "must be greater than {{count}}" + greater_than_or_equal_to: "must be greater than or equal to {{count}}" + equal_to: "must be equal to {{count}}" + less_than: "must be less than {{count}}" + less_than_or_equal_to: "must be less than or equal to {{count}}" + odd: "must be odd" + even: "must be even" + # Append your own errors here or at the model/attributes scope. + + models: + # Overrides default messages + + attributes: + # Overrides model and default messages. + diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index 469e9d6c03..96af3b17a6 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -11,7 +11,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase def teardown reset_callbacks Topic - I18n.load_translations File.dirname(__FILE__) + '/../../lib/active_record/locale/en-US.rb' + I18n.load_translations File.dirname(__FILE__) + '/../../lib/active_record/locale/en-US.yml' end def unique_topic -- cgit v1.2.3 From c1e0d8aa48c2f9139af42b3cf79e42bb5e0f1f03 Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Tue, 19 Aug 2008 23:21:32 +0200 Subject: Locale file changed to yaml --- activesupport/lib/active_support.rb | 2 +- activesupport/lib/active_support/locale/en-US.rb | 29 --------------------- activesupport/lib/active_support/locale/en-US.yml | 31 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 30 deletions(-) delete mode 100644 activesupport/lib/active_support/locale/en-US.rb create mode 100644 activesupport/lib/active_support/locale/en-US.yml diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index 6056067efd..46ca9f93df 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -59,7 +59,7 @@ require 'active_support/base64' require 'active_support/time_with_zone' I18n.populate do - I18n.load_translations File.dirname(__FILE__) + '/active_support/locale/en-US.rb' + I18n.load_translations File.dirname(__FILE__) + '/active_support/locale/en-US.yml' end Inflector = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Inflector', 'ActiveSupport::Inflector') diff --git a/activesupport/lib/active_support/locale/en-US.rb b/activesupport/lib/active_support/locale/en-US.rb deleted file mode 100644 index ed457d6f4f..0000000000 --- a/activesupport/lib/active_support/locale/en-US.rb +++ /dev/null @@ -1,29 +0,0 @@ -{ :'en-US' => { - :support => { - :array => { - :sentence_connector => 'and' - } - }, - :date => { - :formats => { - :default => "%Y-%m-%d", - :short => "%b %d", - :long => "%B %d, %Y", - }, - :day_names => Date::DAYNAMES, - :abbr_day_names => Date::ABBR_DAYNAMES, - :month_names => Date::MONTHNAMES, - :abbr_month_names => Date::ABBR_MONTHNAMES, - :order => [:year, :month, :day] - }, - :time => { - :formats => { - :default => "%a, %d %b %Y %H:%M:%S %z", - :short => "%d %b %H:%M", - :long => "%B %d, %Y %H:%M", - }, - :am => 'am', - :pm => 'pm' - } - } -} \ No newline at end of file diff --git a/activesupport/lib/active_support/locale/en-US.yml b/activesupport/lib/active_support/locale/en-US.yml new file mode 100644 index 0000000000..60ecb1d42a --- /dev/null +++ b/activesupport/lib/active_support/locale/en-US.yml @@ -0,0 +1,31 @@ +en-US: + date: + formats: + # Use the strftime parameters for formats. + # When no format has been given, it uses default. + # You can provide other formats here if you like! + default: "%Y-%m-%d" + short: "%b %d" + long: "%B %d, %Y" + + day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday] + abbr_day_names: [Sun, Mon, Tue, Wed, Thu, Fri, Sat] + + # Don't forget the nil at the beginning; there's no such thing as a 0th month + month_names: [~, January, February, March, April, May, June, July, August, September, October, November, December] + abbr_month_names: [~, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec] + # Used in date_select and datime_select. + order: [ :year, :month, :day ] + + time: + formats: + default: "%a, %d %b %Y %H:%M:%S %z" + short: "%d %b %H:%M" + long: "%B %d, %Y %H:%M" + am: "am" + pm: "pm" + +# Used in array.to_sentence. + support: + array: + sentence_connector: "and" -- cgit v1.2.3 From 72366398b28cec2f8b9809f463e0b1271e3d5ba0 Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Tue, 19 Aug 2008 23:22:59 +0200 Subject: Removed en-US ruby locale in favor of yaml --- activerecord/lib/active_record/locale/en-US.rb | 28 -------------------------- 1 file changed, 28 deletions(-) delete mode 100644 activerecord/lib/active_record/locale/en-US.rb diff --git a/activerecord/lib/active_record/locale/en-US.rb b/activerecord/lib/active_record/locale/en-US.rb deleted file mode 100644 index 89a5baba06..0000000000 --- a/activerecord/lib/active_record/locale/en-US.rb +++ /dev/null @@ -1,28 +0,0 @@ -{ :'en-US' => { - :activerecord => { - :errors => { - :messages => { - :inclusion => "is not included in the list", - :exclusion => "is reserved", - :invalid => "is invalid", - :confirmation => "doesn't match confirmation", - :accepted => "must be accepted", - :empty => "can't be empty", - :blank => "can't be blank", - :too_long => "is too long (maximum is {{count}} characters)", - :too_short => "is too short (minimum is {{count}} characters)", - :wrong_length => "is the wrong length (should be {{count}} characters)", - :taken => "has already been taken", - :not_a_number => "is not a number", - :greater_than => "must be greater than {{count}}", - :greater_than_or_equal_to => "must be greater than or equal to {{count}}", - :equal_to => "must be equal to {{count}}", - :less_than => "must be less than {{count}}", - :less_than_or_equal_to => "must be less than or equal to {{count}}", - :odd => "must be odd", - :even => "must be even" - } - } - } - } -} -- cgit v1.2.3 From 7ee01c8c5415579a68fe4bf073f69cf5796bf7c9 Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Tue, 19 Aug 2008 23:33:39 +0200 Subject: Changed locale file to yml for actionpack, but still broken --- actionpack/lib/action_view.rb | 2 +- actionpack/lib/action_view/locale/en-US.rb | 89 ----------------------------- actionpack/lib/action_view/locale/en-US.yml | 89 +++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 90 deletions(-) delete mode 100644 actionpack/lib/action_view/locale/en-US.rb create mode 100644 actionpack/lib/action_view/locale/en-US.yml diff --git a/actionpack/lib/action_view.rb b/actionpack/lib/action_view.rb index 7e9ae9432d..f13324a9d0 100644 --- a/actionpack/lib/action_view.rb +++ b/actionpack/lib/action_view.rb @@ -35,7 +35,7 @@ require 'action_view/partials' require 'action_view/template_error' I18n.backend.populate do - I18n.load_translations File.dirname(__FILE__) + '/action_view/locale/en-US.rb' + I18n.load_translations File.dirname(__FILE__) + '/action_view/locale/en-US.yml' end ActionView::Base.class_eval do diff --git a/actionpack/lib/action_view/locale/en-US.rb b/actionpack/lib/action_view/locale/en-US.rb deleted file mode 100644 index d4647b5a48..0000000000 --- a/actionpack/lib/action_view/locale/en-US.rb +++ /dev/null @@ -1,89 +0,0 @@ -{ :'en-US' => { - :datetime => { - :distance_in_words => { - :half_a_minute => 'half a minute', - :less_than_x_seconds => { - :one => 'less than 1 second', - :many => 'less than {{count}} seconds' - }, - :x_seconds => { - :one => '1 second', - :many => '{{count}} seconds' - }, - :less_than_x_minutes => { - :one => 'less than a minute', - :many => 'less than {{count}} minutes' - }, - :x_minutes => { - :one => '1 minute', - :many => '{{count}} minutes' - }, - :about_x_hours => { - :one => 'about 1 hour', - :many => 'about {{count}} hours' - }, - :x_days => { - :one => '1 day', - :many => '{{count}} days' - }, - :about_x_months => { - :one => 'about 1 month', - :many => 'about {{count}} months' - }, - :x_months => { - :one => '1 month', - :many => '{{count}} months' - }, - :about_x_years => { - :one => 'about 1 year', - :many => 'about {{count}} years' - }, - :over_x_years => { - :one => 'over 1 year', - :many => 'over {{count}} years' - } - } - }, - :number => { - :format => { - :precision => 3, - :separator => '.', - :delimiter => ',' - }, - :currency => { - :format => { - :unit => '$', - :precision => 2, - :format => '%u%n' - } - }, - :human => { - :format => { - :precision => 1, - :delimiter => '' - } - }, - :percentage => { - :format => { - :delimiter => '' - } - }, - :precision => { - :format => { - :delimiter => '' - } - } - }, - :activerecord => { - :errors => { - :template => { - :header => { - :one => "1 error prohibited this {{model}} from being saved", - :many => "{{count}} errors prohibited this {{model}} from being saved" - }, - :body => "There were problems with the following fields:" - } - } - } - } -} diff --git a/actionpack/lib/action_view/locale/en-US.yml b/actionpack/lib/action_view/locale/en-US.yml new file mode 100644 index 0000000000..d4039e9b12 --- /dev/null +++ b/actionpack/lib/action_view/locale/en-US.yml @@ -0,0 +1,89 @@ +"en-US": + number: + # Used in number_with_delimiter() + # These are also the defaults for 'currency', 'percentage', 'precision', and 'human' + format: + # Sets the seperator between the units, for more precision (e.g. 1.0 / 2.0 == 0.5) + seperator: "." + # Delimets thousands (e.g. 1,000,000 is a million) (always in groups of three) + delimeter: "," + # Number of decimals, behind the seperator (the number 1 with a precision of 2 gives: 1.00) + precision: 3 + + # Used in number_to_currency() + currency: + format: + # Where is the currency sign? %u is the currency unit, %n the number (default: $5.00) + format: "%u%n" + unit: "$" + # These three are to override number.format and are optional + seperator: "." + delimiter: "," + precision: 2 + + # Used in number_to_percentage() + percentage: + format: + # These three are to override number.format and are optional + seperator: "." + delimiter: "," + precision: 3 + + # Used in number_to_precision() + precision: + format: + # These three are to override number.format and are optional + seperator: "." + delimiter: "," + precision: 3 + + # Used in number_to_human_size() + human: + format: + # These three are to override number.format and are optional + seperator: "." + delimiter: "," + + # Used in distance_of_time_in_words(), distance_of_time_in_words_to_now(), time_ago_in_words() + datetime: + distance_in_words: + half_a_minute: "half a minute" + less_than_x_seconds: + one: "less than a second" + many: "less than {{count}} seconds" + x_seconds: + one: "1 second" + many: "{{count}} seconds" + less_than_x_minutes: + one: "less than 1 minutes" + many: "less than {{count}} minutes" + x_minutes: + one: "1 minute" + many: "{{count}} minutes" + about_x_hours: + one: "about 1 hour" + many: "about {{count}} hours" + x_days: + one: "1 day" + many: "{{count}} days" + about_x_months: + one: "about a month" + many: "about {{count}} months" + x_months: + one: "1 month" + many: "{{count}} months" + about_x_years: + one: "about 1 year" + many: "about {{count}} years" + over_x_years: + one: "over 1 year" + many: "over {{count}} years" + + activerecord: + errors: + header: + one: "1 error prohibited this {{model}} from being saved" + many: "{{count}} errors prohibited this {{model}} from being saved" + # The variable :count is also available + message: "There were problems with the following fields:" + -- cgit v1.2.3 From 950b9792baae14e42b894f113ae8dfc95df34da9 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 20 Aug 2008 18:44:18 +0200 Subject: fixing actionpack yml translations --- actionpack/lib/action_view/locale/en-US.yml | 44 +++++++++++++++-------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/actionpack/lib/action_view/locale/en-US.yml b/actionpack/lib/action_view/locale/en-US.yml index d4039e9b12..57987f4e02 100644 --- a/actionpack/lib/action_view/locale/en-US.yml +++ b/actionpack/lib/action_view/locale/en-US.yml @@ -3,11 +3,11 @@ # Used in number_with_delimiter() # These are also the defaults for 'currency', 'percentage', 'precision', and 'human' format: - # Sets the seperator between the units, for more precision (e.g. 1.0 / 2.0 == 0.5) - seperator: "." + # Sets the separator between the units, for more precision (e.g. 1.0 / 2.0 == 0.5) + separator: "." # Delimets thousands (e.g. 1,000,000 is a million) (always in groups of three) - delimeter: "," - # Number of decimals, behind the seperator (the number 1 with a precision of 2 gives: 1.00) + delimiter: "," + # Number of decimals, behind the separator (the number 1 with a precision of 2 gives: 1.00) precision: 3 # Used in number_to_currency() @@ -17,7 +17,7 @@ format: "%u%n" unit: "$" # These three are to override number.format and are optional - seperator: "." + separator: "." delimiter: "," precision: 2 @@ -25,37 +25,38 @@ percentage: format: # These three are to override number.format and are optional - seperator: "." - delimiter: "," - precision: 3 + # separator: + delimiter: "" + # precision: # Used in number_to_precision() precision: format: # These three are to override number.format and are optional - seperator: "." - delimiter: "," - precision: 3 + # separator: + delimiter: "" + # precision: # Used in number_to_human_size() human: format: # These three are to override number.format and are optional - seperator: "." - delimiter: "," + # separator: + delimiter: "" + precision: 1 # Used in distance_of_time_in_words(), distance_of_time_in_words_to_now(), time_ago_in_words() datetime: distance_in_words: half_a_minute: "half a minute" less_than_x_seconds: - one: "less than a second" + one: "less than 1 second" many: "less than {{count}} seconds" x_seconds: one: "1 second" many: "{{count}} seconds" less_than_x_minutes: - one: "less than 1 minutes" + one: "less than a minute" many: "less than {{count}} minutes" x_minutes: one: "1 minute" @@ -67,7 +68,7 @@ one: "1 day" many: "{{count}} days" about_x_months: - one: "about a month" + one: "about 1 month" many: "about {{count}} months" x_months: one: "1 month" @@ -81,9 +82,10 @@ activerecord: errors: - header: - one: "1 error prohibited this {{model}} from being saved" - many: "{{count}} errors prohibited this {{model}} from being saved" - # The variable :count is also available - message: "There were problems with the following fields:" + template: + header: + one: "1 error prohibited this {{model}} from being saved" + many: "{{count}} errors prohibited this {{model}} from being saved" + # The variable :count is also available + body: "There were problems with the following fields:" -- cgit v1.2.3 From cf6840773b4f5046151f4d28c286069aabaafa10 Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Wed, 20 Aug 2008 23:43:46 +0200 Subject: Custom error messages scope improved --- activerecord/lib/active_record/validations.rb | 28 +++---- activerecord/test/cases/validations_i18n_test.rb | 98 ++++++++++++++++++------ 2 files changed, 89 insertions(+), 37 deletions(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index a442d008a9..1ea520f3b6 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -68,42 +68,42 @@ module ActiveRecord end # Translates an error message in it's default scope (activerecord.errrors.messages). - # Error messages are first looked up in custom.MODEL.ATTRIBUTE.MESSAGE, if it's not there, it's looked up - # in custom.MODEL.ATTRIBUTE and if that is not there it returns the translation of the default message - # (e.g. activerecord.errors.messages.MESSAGE). Both the model name and the attribute name are available for - # interpolation. + # Error messages are first looked up in models.MODEL.attributes.ATTRIBUTE.MESSAGE, if it's not there, + # it's looked up in models.MODEL.MESSAGE and if that is not there it returns the translation of the + # default message (e.g. activerecord.errors.messages.MESSAGE). The translated model name, + # translated attribute name and the value are available for interpolation. # # When using inheritence in your models, it will check all the inherited models too, but only if the model itself # hasn't been found. Say you have class Admin < User; end and you wanted the translation for the :blank # error +message+ for the title +attribute+, it looks for these translations: # #
    - #
  1. activerecord.errors.messages.custom.admin.title.blank
  2. - #
  3. activerecord.errors.messages.custom.admin.blank
  4. - #
  5. activerecord.errors.messages.custom.user.title.blank
  6. - #
  7. activerecord.errors.messages.custom.user.blank
  8. + #
  9. activerecord.errors.models.admin.attributes.title.blank
  10. + #
  11. activerecord.errors.models.admin.blank
  12. + #
  13. activerecord.errors.models.user.attributes.title.blank
  14. + #
  15. activerecord.errors.models.user.blank
  16. #
  17. activerecord.errors.messages.blank
  18. - #
  19. any default you provided through the +options+ hash
  20. + #
  21. any default you provided through the +options+ hash (in the activerecord.errors scope)
  22. #
def generate_message(attribute, message = :invalid, options = {}) defaults = @base.class.self_and_descendents_from_active_record.map do |klass| - [ :"custom.#{klass.name.underscore}.#{attribute}.#{message}", - :"custom.#{klass.name.underscore}.#{message}" ] + [ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}", + :"models.#{klass.name.underscore}.#{message}" ] end defaults << options.delete(:default) - defaults = defaults.compact.flatten << message + defaults = defaults.compact.flatten << :"messages.#{message}" model_name = @base.class.name key = defaults.shift - value = @base.send(attribute) if @base.respond_to?(attribute) + value = @base.respond_to?(attribute) ? @base.send(attribute) : nil options = { :default => defaults, :model => @base.class.human_name, :attribute => @base.class.human_attribute_name(attribute.to_s), :value => value, - :scope => [:activerecord, :errors, :messages] + :scope => [:activerecord, :errors] }.merge(options) I18n.translate(key, options) diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index 96af3b17a6..881f219112 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -42,22 +42,74 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # ActiveRecord::Errors uses_mocha 'ActiveRecord::Errors' do + def test_errors_generate_message_translates_custom_model_attribute_key - global_scope = [:activerecord, :errors, :messages] - custom_scope = global_scope + [:custom, 'topic', :title] - I18n.expects(:translate).with(:topic, {:count => 1, :default => ['Topic'], :scope => [:activerecord, :models]}).returns('Topic') - I18n.expects(:translate).with(:'topic.title', {:count => 1, :default => ['Title'], :scope => [:activerecord, :attributes]}).returns('Title') - I18n.expects(:translate).with(:"custom.topic.title.invalid", :value => nil, :scope => global_scope, :default => [:"custom.topic.invalid", 'default from class def error 1', :invalid], :attribute => "Title", :model => "Topic").returns('default from class def error 1') + I18n.expects(:translate).with( + :topic, + { :count => 1, + :default => ['Topic'], + :scope => [:activerecord, :models] + } + ).returns('Topic') + + I18n.expects(:translate).with( + :"topic.title", + { :count => 1, + :default => ['Title'], + :scope => [:activerecord, :attributes] + } + ).returns('Title') + + I18n.expects(:translate).with( + :"models.topic.attributes.title.invalid", + :value => nil, + :scope => [:activerecord, :errors], + :default => [ + :"models.topic.invalid", + 'default from class def error 1', + :"messages.invalid"], + :attribute => "Title", + :model => "Topic" + ).returns('default from class def error 1') + @topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1' end def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti - custom_scope = [:activerecord, :errors, :custom, 'topic', :title] - I18n.expects(:translate).with(:reply, {:count => 1, :default => [:topic, 'Reply'], :scope => [:activerecord, :models]}).returns('Reply') - I18n.expects(:translate).with(:'reply.title', {:count => 1, :default => [:'topic.title', 'Title'], :scope => [:activerecord, :attributes]}).returns('Title') - I18n.expects(:translate).with(:"custom.reply.title.invalid", :value => nil, :scope => [:activerecord, :errors, :messages], :default => [:"custom.reply.invalid", :"custom.topic.title.invalid", :"custom.topic.invalid", 'default from class def', :invalid], :model => 'Reply', :attribute => 'Title').returns("default from class def") + + I18n.expects(:translate).with( + :reply, + { :count => 1, + :default => [:topic, 'Reply'], + :scope => [:activerecord, :models] + } + ).returns('Reply') + + I18n.expects(:translate).with( + :"reply.title", + { :count => 1, + :default => [:'topic.title', 'Title'], + :scope => [:activerecord, :attributes] + } + ).returns('Title') + + I18n.expects(:translate).with( + :"models.reply.attributes.title.invalid", + :value => nil, + :scope => [:activerecord, :errors], + :default => [ + :"models.reply.invalid", + :"models.topic.attributes.title.invalid", + :"models.topic.invalid", + 'default from class def', + :"messages.invalid"], + :model => 'Reply', + :attribute => 'Title' + ).returns("default from class def") + Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def' + end def test_errors_add_on_empty_generates_message @@ -347,7 +399,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_confirmation_of w/o mocha def test_validates_confirmation_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:confirmation => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:confirmation => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}} Topic.validates_confirmation_of :title @@ -368,7 +420,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_acceptance_of w/o mocha def test_validates_acceptance_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:accepted => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:accepted => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}} Topic.validates_acceptance_of :title, :allow_nil => false @@ -387,7 +439,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_presence_of w/o mocha def test_validates_presence_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:blank => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:blank => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:blank => 'global message'}}} Topic.validates_presence_of :title @@ -406,7 +458,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_length_of :within w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:too_short => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:too_short => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}} Topic.validates_length_of :title, :within => 3..5 @@ -425,7 +477,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_length_of :is w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:wrong_length => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @@ -444,7 +496,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_uniqueness_of w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:wrong_length => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @@ -464,7 +516,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_format_of w/o mocha def test_validates_format_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:invalid => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:invalid => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/ @@ -483,7 +535,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_inclusion_of w/o mocha def test_validates_inclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:inclusion => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:inclusion => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}} Topic.validates_inclusion_of :title, :in => %w(a b c) @@ -502,7 +554,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_exclusion_of w/o mocha def test_validates_exclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:exclusion => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:exclusion => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}} Topic.validates_exclusion_of :title, :in => %w(a b c) @@ -523,7 +575,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_numericality_of without :only_integer w/o mocha def test_validates_numericality_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:not_a_number => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title @@ -544,7 +596,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_numericality_of with :only_integer w/o mocha def test_validates_numericality_of_only_integer_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:not_a_number => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true @@ -565,7 +617,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_numericality_of :odd w/o mocha def test_validates_numericality_of_odd_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:odd => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:odd => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:odd => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :odd => true @@ -586,7 +638,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_numericality_of :less_than w/o mocha def test_validates_numericality_of_less_than_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:title => {:less_than => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:less_than => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0 @@ -608,7 +660,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase # validates_associated w/o mocha def test_validates_associated_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom => {:topic => {:replies => {:invalid => 'custom message'}}}}}} + I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:replies => {:invalid => 'custom message'}}}}}} I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_associated :replies -- cgit v1.2.3