From 660f78b8e9c89ab92b92c3b7767d693ec4ee2eb2 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 26 Aug 2009 19:22:56 +0200 Subject: [PATCH] [i18n] allow ActiveRecord#RecordInvalid exception message to be localized --- activerecord/lib/active_record/locale/en.yml | 1 + activerecord/lib/active_record/validations.rb | 3 ++- activerecord/test/cases/validations_i18n_test.rb | 10 ++++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/locale/en.yml b/activerecord/lib/active_record/locale/en.yml index bf8a71d..092f5f0 100644 --- a/activerecord/lib/active_record/locale/en.yml +++ b/activerecord/lib/active_record/locale/en.yml @@ -23,6 +23,7 @@ en: less_than_or_equal_to: "must be less than or equal to {{count}}" odd: "must be odd" even: "must be even" + record_invalid: "Validation failed: {{errors}}" # Append your own errors here or at the model/attributes scope. # You can define own errors for models or model attributes. diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 7a7d0ab..a23bb35 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -10,7 +10,8 @@ module ActiveRecord attr_reader :record def initialize(record) @record = record - super("Validation failed: #{@record.errors.full_messages.join(", ")}") + errors = @record.errors.full_messages.join(I18n.t('support.array.words_connector', :default => ', ')) + super(I18n.t('activerecord.errors.messages.record_invalid', :errors => errors)) end end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index 1585554..5ddb0df 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -736,10 +736,9 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase @topic.valid? assert_equal "I am a custom error", @topic.errors.on(:title) end - end -class ActiveRecordValidationsGenerateMessageI18nTests < Test::Unit::TestCase +class ActiveRecordValidationsGenerateMessageI18nTests < ActiveSupport::TestCase def setup reset_callbacks Topic @topic = Topic.new @@ -917,5 +916,12 @@ 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 + # ActiveRecord#RecordInvalid exception + test "RecordInvalid exception can be localized" do + topic = Topic.new + topic.errors.add(:title, :invalid) + topic.errors.add(:title, :blank) + assert_equal "Validation failed: Title is invalid, Title can't be blank", ActiveRecord::RecordInvalid.new(topic).message + end end -- 1.6.0.5