Ticket #9726: i18n_agnostic_api.patch
| File i18n_agnostic_api.patch, 36.0 kB (added by saimonmoore, 1 year ago) |
|---|
-
activesupport/test/string_translation_test.rb
old new 1 #require 'rubygems' 2 #gem 'ruby-debug' 3 #require 'ruby-debug' 4 #Debugger.start 5 6 require File.dirname(__FILE__) + '/abstract_unit' 7 8 class StringTranslationTest < Test::Unit::TestCase 9 10 def test_simple_string 11 assert_equal "A string to be translated", "A string to be translated".t 12 end 13 14 def test_simple_interpolated_string 15 assert_equal 'This must be translated', "%s must be translated".t('This') 16 end 17 18 def test_simple_pluralizable_string 19 assert_equal 'They have 1 translation to do', "They have %d translations to do".t(1) 20 assert_equal 'They have 2 translations to do', "They have %d translations to do".t(2) 21 end 22 23 def test_interpolated_and_pluralizable_string 24 assert_equal "Tommy translated 1 translation", "%s translated %d translations".t('Tommy',1) 25 assert_equal "Tommy translated 2 translations", "%s translated %d translations".t('Tommy',2) 26 assert_equal "Tommy translated 1 translations", "%s translated %d translations".t('Tommy','1') 27 end 28 29 def test_multiple_interpolated_and_pluralizable_string 30 assert_equal "Tommy & Joe translated 1 translation", "%s & %s translated %d translations".t('Tommy','Joe',1) 31 assert_equal "Tommy & Joe translated 2 translations", "%s & %s translated %d translations".t('Tommy','Joe',2) 32 end 33 34 def test_error_string 35 assert_equal "1 error prohibited this pizza from being saved", "%d errors prohibited this %s from being saved".t(1,'pizza') 36 assert_equal "2 errors prohibited this bagel from being saved", "%d errors prohibited this %s from being saved".t(2, 'bagel') 37 end 38 39 def test_complex_verbal_pluralizable_string 40 assert_equal "There is 1 translation to do", "There are %d translations to do".t(1) 41 assert_equal "There are 2 translations to do", "There are %d translations to do".t(2) 42 end 43 44 def test_complex_verbal_pluralizable_string_in_question_form 45 assert_equal "Is there 1 translation to do?", "Are there %d translations to do?".t(1) 46 assert_equal "Are there 2 translations to do?", "Are there %d translations to do?".t(2) 47 end 48 49 def test_pluralizable_string_with_non_whitespace_characters_after_pluralizable_word 50 assert_equal "Omellette recipe (1 egg)", "Omellette recipe (%d eggs)".t(1) 51 assert_equal "Omellette recipe (2 eggs)", "Omellette recipe (%d eggs)".t(2) 52 end 53 54 def test_non_pluralizable_string_with_numeric_interpolation 55 assert_equal "Omellette recipe (eggs: 1)", "Omellette recipe (eggs: %d)".t(1) 56 assert_equal "Omellette recipe (eggs: 3)", "Omellette recipe (eggs: %d)".t(3) 57 end 58 59 def test_pluralizable_strings_with_adjectives_before_noun_dont_singularize 60 assert_equal("Thomas has 1 old toys", "Thomas has %d old toys".t(1)) 61 assert_equal("Thomas has 10 old toys", "Thomas has %d old toys".t(10)) 62 63 assert_equal("Thomas has 1 old scrabby toys", "Thomas has %d old scrabby toys".t(1)) 64 assert_equal("Thomas has 10 old scrabby toys", "Thomas has %d old scrabby toys".t(10)) 65 end 66 67 68 def test_ignores_namespaces 69 assert_equal "any old string", "any old string".t(:namespace) 70 assert_equal "any old string", "any old %s".t(:namespace, 'string') 71 assert_equal "any 1 string", "any %d strings".t(:namespace, 1) 72 assert_equal "any 10 strings", "any %d strings".t(:namespace, 10) 73 assert_equal "Tommy has 1 string", "%s has %d strings".t(:namespace, 1, 'Tommy') 74 assert_equal "Tommy has 10 strings", "%s has %d strings".t(:namespace, 10, 'Tommy') 75 end 76 77 end -
activesupport/test/date_translation_test.rb
old new 1 #require 'rubygems' 2 #gem 'ruby-debug' 3 #require 'ruby-debug' 4 #Debugger.start 5 6 require File.dirname(__FILE__) + '/abstract_unit' 7 8 class DateTranslationTest < Test::Unit::TestCase 9 10 def test_simple_date 11 assert_equal "Thu Jan 01 1970", Date.new(1970,1,1).t.strftime('%a %b %d %Y') 12 end 13 14 def test_custom_translatable_time_class 15 Object.class_eval <<-ENDDEF 16 class ::ActiveSupport::Translation::TranslatableDate 17 def overidden_strftime(*args) 18 'overidden' 19 end 20 alias_method :old_strftime, :strftime 21 alias_method :strftime, :overidden_strftime 22 end 23 ENDDEF 24 25 assert_equal "overidden", Date.new(1970,1,1).t.strftime('%a %b %d %Y') 26 27 Object.class_eval <<-ENDDEF 28 class ::ActiveSupport::Translation::TranslatableDate 29 alias_method :strftime, :old_strftime 30 end 31 ENDDEF 32 33 assert_equal "Thu Jan 01 1970", Date.new(1970,1,1).t.strftime('%a %b %d %Y') 34 end 35 end -
activesupport/test/date_time_translation_test.rb
old new 1 #require 'rubygems' 2 #gem 'ruby-debug' 3 #require 'ruby-debug' 4 #Debugger.start 5 6 require File.dirname(__FILE__) + '/abstract_unit' 7 8 class DateTimeTranslationTest < Test::Unit::TestCase 9 10 def test_simple_date_time 11 assert_equal "Thu Jan 01 01:00:00 1970", DateTime.new(1970,1,1,1,0,0).t.strftime('%a %b %d %H:%M:%S %Y') 12 end 13 14 def test_custom_translatable_time_class 15 Object.class_eval <<-ENDDEF 16 class ::ActiveSupport::Translation::TranslatableDateTime 17 def overidden_strftime(*args) 18 'overidden' 19 end 20 alias_method :old_strftime, :strftime 21 alias_method :strftime, :overidden_strftime 22 end 23 ENDDEF 24 25 assert_equal "overidden", DateTime.new(1970,1,1,1,0,0).t.strftime('%a %b %d %H:%M:%S %Y') 26 27 Object.class_eval <<-ENDDEF 28 class ::ActiveSupport::Translation::TranslatableDateTime 29 alias_method :strftime, :old_strftime 30 end 31 ENDDEF 32 33 assert_equal "Thu Jan 01 01:00:00 1970", DateTime.new(1970,1,1,1,0,0).t.strftime('%a %b %d %H:%M:%S %Y') 34 end 35 end -
activesupport/test/time_translation_test.rb
old new 1 #require 'rubygems' 2 #gem 'ruby-debug' 3 #require 'ruby-debug' 4 #Debugger.start 5 6 require File.dirname(__FILE__) + '/abstract_unit' 7 8 class TimeTranslationTest < Test::Unit::TestCase 9 10 def test_simple_time 11 assert_equal "Thu Jan 01 01:00:00 1970", Time.at(0).t.strftime('%a %b %d %H:%M:%S %Y') 12 end 13 14 def test_custom_translatable_time_class 15 Object.class_eval <<-ENDDEF 16 class ::ActiveSupport::Translation::TranslatableTime 17 def overidden_strftime(*args) 18 'overidden' 19 end 20 alias_method :old_strftime, :strftime 21 alias_method :strftime, :overidden_strftime 22 end 23 ENDDEF 24 25 assert_equal "overidden", Time.at(0).t.strftime('%a %b %d %H:%M:%S %Y') 26 27 Object.class_eval <<-ENDDEF 28 class ::ActiveSupport::Translation::TranslatableTime 29 alias_method :strftime, :old_strftime 30 end 31 ENDDEF 32 33 assert_equal "Thu Jan 01 01:00:00 1970", Time.at(0).t.strftime('%a %b %d %H:%M:%S %Y') 34 35 36 end 37 end -
activesupport/lib/active_support/translation/translatable_string.rb
old new 1 module ActiveSupport #:nodoc: 2 module Translation #:nodoc: 3 class TranslatableString #:nodoc: 4 5 class DefaultTranslatableString 6 7 PLURALIZATION_REGEXP = /(%d\s+[^\s\W]+)/ 8 DOWN_CASE_VERBAL_PLURALIZATION_REGEXP = /(are\s+%d\s+[^\s\W]+)/ 9 UP_CASE_VERBAL_PLURALIZATION_REGEXP = /(Are\s+(.*)\s+%d\s+[^\s\W]+)/ 10 11 # translates the supplied string 12 # 13 # Default implementation allows: 14 # 15 # * simple/multiple string interpolation via %s 16 # 17 # e.g. "%s loves rails".t('G. W. Bush') => 'G. W. Bush loves rails' 18 # "%s and %s love rails".t('G. W. Bush', 'J. Edgar Hoover') => 'G. W. Bush and J. Edgar Hoover love rails' 19 # 20 # * simple pluralization via %d (with/out string interpolation) 21 # 22 # e.g. "%d errors prohibited this pizza from being saved".t(1) => '1 error prohibited this pizza from being saved' 23 # "%d errors prohibited this pizza from being saved".t(10) => '10 errors prohibited this pizza from being saved' 24 # 25 # "%d errors prohibited this %s from being saved".t(1,'penguin') => '1 error prohibited this penguin from being saved' 26 # "%d errors prohibited this %s from being saved".t(3,'penguin') => '3 errors prohibited this penguin from being saved' 27 # 28 # * very basic pluralization of verb 'to be' 29 # 30 # e.g. "There are %d translations to do".t(1) => 'There is 1 translation to do' 31 # "There are %d translations to do".t(3) => 'There are 3 translations to do' 32 # 33 # "Are there %d penguins?".t(1) => 'Is there 1 penguin?' 34 # "Are there %d penguins?".t(3) => 'Are there 3 penguins?' 35 def self.translate(string, args) 36 return string if args.nil? || (args.is_a?(Array) && args.compact.empty?) 37 38 #Add support for namespaces (ns if first argument is symbol) 39 return string if args.is_a? Symbol 40 args.shift if args.first.is_a? Symbol 41 return string if args.is_a?(Array) && args.empty? 42 43 translated_string,string_interpolations, pluralization, plural = nil 44 45 case args 46 when String 47 translated_string = string % args 48 when Fixnum 49 pluralization = args 50 when Array 51 string_interpolations = args.select {|arg| arg.is_a? String} 52 pluralization = args.detect {|arg| arg.is_a? Fixnum} 53 end 54 55 if pluralization 56 57 #Handle singularization of verb 'to be' 58 translated_string = string.gsub(/(are)\s+/,'is ') if pluralization == 1 && string.match(DOWN_CASE_VERBAL_PLURALIZATION_REGEXP) 59 translated_string = string.gsub(/^(Are)\s+/,'Is ') if pluralization == 1 && string.match(UP_CASE_VERBAL_PLURALIZATION_REGEXP) 60 61 #Handle plurals 62 matches = translated_string ? translated_string.match(PLURALIZATION_REGEXP) : string.match(PLURALIZATION_REGEXP) 63 if matches 64 pluralizable_string = matches[1] 65 plural = (pluralizable_string.gsub(/%d\s+/,'') % pluralization) if pluralizable_string 66 plural = (pluralization == 1) ? Inflector.singularize(plural) : plural if plural 67 68 if plural 69 translated_string = if translated_string 70 translated_string.gsub(PLURALIZATION_REGEXP, "#{pluralization} #{plural}") 71 else 72 string.gsub(PLURALIZATION_REGEXP, "#{pluralization} #{plural}") 73 end 74 end 75 else 76 #If there's still a %d in the string but it's not pluralizable then just do a normal interpolation 77 translated_string = translated_string ? (translated_string % pluralization) : (string % pluralization) if string.match(/%d/) 78 end 79 80 end 81 82 #Handle string interpolations 83 translated_string = ((translated_string ? translated_string : string) % string_interpolations) unless string_interpolations.empty? 84 translated_string ? translated_string : string 85 end 86 end 87 88 89 # translates the supplied string 90 # 91 # (see DefaultTranslatableString for default implementation) 92 def self.translate(string, args) 93 DefaultTranslatableString.translate(string, args) 94 end 95 end 96 end 97 end -
activesupport/lib/active_support/translation/translatable_date_time.rb
old new 1 module ActiveSupport #:nodoc: 2 module Translation #:nodoc: 3 class TranslatableDateTime < ::DateTime #:nodoc: 4 def strftime(*args) 5 super(*args) 6 end 7 end 8 end 9 end -
activesupport/lib/active_support/translation/translatable_date.rb
old new 1 module ActiveSupport #:nodoc: 2 module Translation #:nodoc: 3 class TranslatableDate < ::Date #:nodoc: 4 def strftime(*args) 5 super(*args) 6 end 7 end 8 end 9 end -
activesupport/lib/active_support/translation/translatable_time.rb
old new 1 module ActiveSupport #:nodoc: 2 module Translation #:nodoc: 3 class TranslatableTime < ::Time#:nodoc: 4 def strftime(*args) 5 super(*args) 6 end 7 end 8 end 9 end -
activesupport/lib/active_support/translation.rb
old new 1 require 'active_support/translation/translatable_string' 2 require 'active_support/translation/translatable_time' 3 require 'active_support/translation/translatable_date' 4 require 'active_support/translation/translatable_date_time' -
activesupport/lib/active_support/core_ext/date_time/translation.rb
old new 1 module ActiveSupport #:nodoc: 2 module CoreExtensions #:nodoc: 3 module DateTime #:nodoc: 4 # Define methods for handeling translation of strings. 5 module Translation 6 def t 7 ActiveSupport::Translation::TranslatableDateTime.civil(self.year,self.mon, self.mday, self.hour, self.min, self.sec) 8 end 9 end 10 end 11 end 12 end -
activesupport/lib/active_support/core_ext/date_time.rb
old new 2 2 require "#{File.dirname(__FILE__)}/time/behavior" 3 3 require "#{File.dirname(__FILE__)}/date_time/calculations" 4 4 require "#{File.dirname(__FILE__)}/date_time/conversions" 5 require "#{File.dirname(__FILE__)}/date_time/translation" 5 6 6 7 class DateTime 7 8 include ActiveSupport::CoreExtensions::Time::Behavior 8 9 include ActiveSupport::CoreExtensions::DateTime::Calculations 9 10 include ActiveSupport::CoreExtensions::DateTime::Conversions 11 include ActiveSupport::CoreExtensions::DateTime::Translation 10 12 end -
activesupport/lib/active_support/core_ext/time/translation.rb
old new 1 module ActiveSupport #:nodoc: 2 module CoreExtensions #:nodoc: 3 module Time #:nodoc: 4 # Define methods for handeling translation of times. 5 module Translation 6 def t 7 ActiveSupport::Translation::TranslatableTime.at(self) 8 end 9 end 10 end 11 end 12 end -
activesupport/lib/active_support/core_ext/time/conversions.rb
old new 24 24 if formatter.respond_to?(:call) 25 25 formatter.call(self).to_s 26 26 else 27 strftime(formatter) 27 strftime(formatter).strip 28 28 end 29 29 else 30 30 to_default_s -
activesupport/lib/active_support/core_ext/object.rb
old new 1 1 require File.dirname(__FILE__) + '/object/extending' 2 require File.dirname(__FILE__) + '/object/instance_variables' 3 require File.dirname(__FILE__) + '/object/misc' 2 require File.dirname(__FILE__) + '/object/misc' -
activesupport/lib/active_support/core_ext/string.rb
old new 4 4 require File.dirname(__FILE__) + '/string/starts_ends_with' 5 5 require File.dirname(__FILE__) + '/string/iterators' unless 'test'.respond_to?(:each_char) 6 6 require File.dirname(__FILE__) + '/string/unicode' 7 require File.dirname(__FILE__) + '/string/translation' 7 8 8 9 class String #:nodoc: 9 10 include ActiveSupport::CoreExtensions::String::Access … … 14 15 include ActiveSupport::CoreExtensions::String::Iterators 15 16 end 16 17 include ActiveSupport::CoreExtensions::String::Unicode 18 include ActiveSupport::CoreExtensions::String::Translation 17 19 end -
activesupport/lib/active_support/core_ext/date/translation.rb
old new 1 module ActiveSupport #:nodoc: 2 module CoreExtensions #:nodoc: 3 module Date #:nodoc: 4 # Define methods for handeling translation of strings. 5 module Translation 6 def t 7 ActiveSupport::Translation::TranslatableDate.new(self.year,self.mon, self.mday) 8 end 9 end 10 end 11 end 12 end -
activesupport/lib/active_support/core_ext/date/conversions.rb
old new 17 17 alias_method :to_s, :to_formatted_s 18 18 alias_method :default_inspect, :inspect 19 19 alias_method :inspect, :readable_inspect 20 21 # Ruby 1.9 has Date#to_time which converts to localtime only.22 remove_method :to_time if base.instance_methods.include?(:to_time)23 20 end 24 21 end 25 22 … … 28 25 if formatter.respond_to?(:call) 29 26 formatter.call(self).to_s 30 27 else 31 strftime(formatter) 28 strftime(formatter).strip 32 29 end 33 30 else 34 31 to_default_s 35 32 end 36 33 end 37 34 38 35 # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005" 39 36 def readable_inspect 40 37 strftime("%a, %d %b %Y") … … 43 40 # To be able to keep Times, Dates and DateTimes interchangeable on conversions 44 41 def to_date 45 42 self 46 end if RUBY_VERSION < '1.9'43 end 47 44 48 45 # Converts self to a Ruby Time object; time is set to beginning of day 49 46 # Timezone can either be :local or :utc (default :local) 50 47 def to_time(form = :local) 51 48 ::Time.send("#{form}_time", year, month, day) 52 49 end 53 50 54 51 # Converts self to a Ruby DateTime object; time is set to beginning of day 55 52 def to_datetime 56 53 ::DateTime.civil(year, month, day, 0, 0, 0, 0, 0) 57 end if RUBY_VERSION < '1.9'54 end 58 55 59 56 def xmlschema 60 57 to_time.xmlschema -
activesupport/lib/active_support/core_ext/date.rb
old new 2 2 require File.dirname(__FILE__) + '/date/behavior' 3 3 require File.dirname(__FILE__) + '/date/calculations' 4 4 require File.dirname(__FILE__) + '/date/conversions' 5 require File.dirname(__FILE__) + '/date/translation' 5 6 6 7 class Date#:nodoc: 7 8 include ActiveSupport::CoreExtensions::Date::Behavior 8 9 include ActiveSupport::CoreExtensions::Date::Calculations 9 10 include ActiveSupport::CoreExtensions::Date::Conversions 11 include ActiveSupport::CoreExtensions::Date::Translation 10 12 end -
activesupport/lib/active_support/core_ext/time.rb
old new 11 11 require File.dirname(__FILE__) + '/time/behavior' 12 12 require File.dirname(__FILE__) + '/time/calculations' 13 13 require File.dirname(__FILE__) + '/time/conversions' 14 require File.dirname(__FILE__) + '/time/translation' 14 15 15 16 class Time#:nodoc: 16 17 include ActiveSupport::CoreExtensions::Time::Behavior 17 18 include ActiveSupport::CoreExtensions::Time::Calculations 18 19 include ActiveSupport::CoreExtensions::Time::Conversions 20 include ActiveSupport::CoreExtensions::Time::Translation 19 21 end -
activesupport/lib/active_support/core_ext/string/translation.rb
old new 1 module ActiveSupport #:nodoc: 2 module CoreExtensions #:nodoc: 3 module String #:nodoc: 4 # Define methods for handeling translation of strings. 5 module Translation 6 def t(*args) 7 ActiveSupport::Translation::TranslatableString.translate(self, args) 8 end 9 end 10 end 11 end 12 end -
activesupport/lib/active_support.rb
old new 46 46 47 47 require 'active_support/multibyte' 48 48 49 require 'active_support/translation' 50 -
activerecord/test/validations_test.rb
old new 25 25 fixtures :topics, :developers 26 26 27 27 def setup 28 29 Object.class_eval <<-ENDDEF 30 class ::ActiveSupport::Translation::TranslatableString 31 def self.overidden_translate(string, args) 32 DefaultTranslatableString.translate(string, args) 33 end 34 35 class << self 36 alias_method :old_translate, :translate 37 alias_method :translate, :overidden_translate 38 end 39 end 40 ENDDEF 41 28 42 Topic.write_inheritable_attribute(:validate, nil) 29 43 Topic.write_inheritable_attribute(:validate_on_create, nil) 30 44 Topic.write_inheritable_attribute(:validate_on_update, nil) 31 45 end 46 47 def tear_down 48 Object.class_eval <<-ENDDEF 49 class ::ActiveSupport::Translation::TranslatableString 50 class << self 51 alias_method :translate, :old_translate 52 end 53 end 54 ENDDEF 55 56 end 32 57 33 58 def test_single_field_validation 34 59 r = Reply.new … … 130 155 r.save 131 156 132 157 errors = [] 133 r.errors.each { |attr, msg| errors << [attr, msg] } 134 158 r.errors.each do |attr, msg| 159 txt_msg = msg.kind_of?(Array) ? msg.first.t(msg.last) : msg.first.t 160 errors << [attr, txt_msg] 161 end 162 135 163 assert errors.include?(["title", "Empty"]) 136 164 assert errors.include?(["content", "Empty"]) 137 165 end … … 160 188 r.errors.each_full { |error| errors << error } 161 189 162 190 assert_equal "Reply is not dignifying", r.errors.on_base 163 164 191 assert errors.include?("Title Empty") 165 192 assert errors.include?("Reply is not dignifying") 166 193 assert_equal 2, r.errors.count -
activerecord/lib/active_record/validations.rb
old new 10 10 attr_reader :record 11 11 def initialize(record) 12 12 @record = record 13 super("Validation failed: #{@record.errors.full_messages.join(", ")}")13 super("Validation failed: %s".t(@record.errors.full_messages.join(", "))) 14 14 end 15 15 end 16 16 … … 52 52 # Adds an error to the base object instead of any particular attribute. This is used 53 53 # to report errors that don't tie to any specific attribute, but rather to the object 54 54 # as a whole. These error messages don't get prepended with any field name when iterating 55 # with each_full, so they should be complete sentences. 56 def add_to_base(msg) 57 add(:base, msg) 55 # with each_full, so they should be complete sentences. The optional +num+ argument 56 # specifies a pluralization value for translatable messages. 57 def add_to_base(msg, num = nil) 58 add(:base, msg, num) 58 59 end 59 60 60 61 # Adds an error message (+msg+) to the +attribute+, which will be returned on a call to <tt>on(attribute)</tt> 61 62 # for the same attribute and ensure that this error object returns false when asked if <tt>empty?</tt>. More than one 62 63 # error can be added to the same +attribute+ in which case an array will be returned on a call to <tt>on(attribute)</tt>. 63 # If no +msg+ is supplied, "invalid" is assumed. 64 def add(attribute, msg = @@default_error_messages[:invalid]) 64 # If no +msg+ is supplied, "invalid" is assumed. The optional +num+ argument specifies a pluralization value for translatable 65 # messages. 66 def add(attribute, msg = @@default_error_messages[:invalid], num = nil) 65 67 @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil? 66 @errors[attribute.to_s] << msg68 @errors[attribute.to_s] << [ msg, num ] 67 69 end 68 70 69 71 # Will add an error message to each of the attributes in +attributes+ that is empty. … … 111 113 # company.errors.on(:email) # => "can't be blank" 112 114 # company.errors.on(:address) # => nil 113 115 def on(attribute) 114 errors = @errors[attribute.to_s] 115 return nil if errors.nil? 116 errors.size == 1 ? errors.first : errors 116 if @errors[attribute.to_s].nil? 117 return nil 118 else 119 msgs = @errors[attribute.to_s] 120 txt_msgs = msgs.map {|msg| msg.kind_of?(Array) ? msg.first.t(msg.last) : msg.first.t } 121 return txt_msgs.length == 1 ? txt_msgs.first : txt_msgs 122 end 117 123 end 118 124 119 125 alias :[] :on … … 172 178 @errors.each_key do |attr| 173 179 @errors[attr].each do |msg| 174 180 next if msg.nil? 175 181 182 # Extract the message and its quantity value for translation 183 msg = [ msg ].flatten 184 msg_text, msg_num = msg 185 176 186 if attr == "base" 177 full_messages << msg 187 full_messages << msg_text.t(msg_num) 178 188 else 179 full_messages << @base.class.human_attribute_name(attr) + " " + msg189 full_messages << (("#{@base.class.human_attribute_name(attr)} #{msg_text}").t(msg_num)) 180 190 end 181 191 end 182 192 end … … 546 556 when :within, :in 547 557 raise ArgumentError, ":#{option} must be a Range" unless option_value.is_a?(Range) 548 558 549 too_short = options[:too_short] % option_value.begin550 too_long = options[:too_long] % option_value.end559 too_short = options[:too_short] 560 too_long = options[:too_long] 551 561 552 562 validates_each(attrs, options) do |record, attr, value| 563 553 564 if value.nil? or value.split(//).size < option_value.begin 554 record.errors.add(attr, too_short )565 record.errors.add(attr, too_short, option_value.begin) 555 566 elsif value.split(//).size > option_value.end 556 record.errors.add(attr, too_long )567 record.errors.add(attr, too_long, option_value.end) 557 568 end 558 569 end 559 570 when :is, :minimum, :maximum … … 563 574 validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" } 564 575 message_options = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long } 565 576 566 message = (options[:message] || options[message_options[option]]) % option_value577 message = (options[:message] || options[message_options[option]]) 567 578 568 579 validates_each(attrs, options) do |record, attr, value| 569 580 if value.kind_of?(String) 570 record.errors.add(attr, message ) unless !value.nil? and value.split(//).size.method(validity_checks[option])[option_value]581 record.errors.add(attr, message, option_value) unless !value.nil? and value.split(//).size.method(validity_checks[option])[option_value] 571 582 else 572 record.errors.add(attr, message ) unless !value.nil? and value.size.method(validity_checks[option])[option_value]583 record.errors.add(attr, message, option_value) unless !value.nil? and value.size.method(validity_checks[option])[option_value] 573 584 end 574 585 end 575 586 end -
actionpack/lib/action_view/helpers/active_record_helper.rb
old new 118 118 # you need is significantly different from the default presentation, it makes plenty of sense to access the object.errors 119 119 # instance yourself and set it up. View the source of this method to see how easy it is. 120 120 def error_messages_for(*params) 121 options = params. extract_options!.symbolize_keys121 options = params.last.is_a?(Hash) ? params.pop.symbolize_keys : {} 122 122 objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact 123 123 count = objects.inject(0) {|sum, object| sum + object.errors.count } 124 124 unless count.zero? … … 131 131 html[key] = 'errorExplanation' 132 132 end 133 133 end 134 header_message = " #{pluralize(count, 'error')} prohibited this #{(options[:object_name] || params.first).to_s.gsub('_', ' ')} from being saved"134 header_message = "%d errors prohibited this #{(options[:object_name] || params.first).to_s.gsub('_', ' ')} from being saved".t(count) 135 135 error_messages = objects.map {|object| object.errors.full_messages.map {|msg| content_tag(:li, msg) } } 136 136 content_tag(:div, 137 137 content_tag(options[:header_tag] || :h2, header_message) << 138 content_tag(:p, 'There were problems with the following fields:' ) <<138 content_tag(:p, 'There were problems with the following fields:'.t) << 139 139 content_tag(:ul, error_messages), 140 140 html 141 141 ) -
actionpack/lib/action_view/helpers/date_helper.rb
old new 65 65 66 66 case distance_in_minutes 67 67 when 0..1 68 return (distance_in_minutes == 0) ? 'less than a minute' : '1 minute'unless include_seconds68 return (distance_in_minutes==0) ? 'less than a minute'.t : ('%d minutes'.t 1) unless include_seconds 69 69 case distance_in_seconds 70 when 0..4 then 'less than 5 seconds'71 when 5..9 then 'less than 10 seconds'72 when 10..19 then 'less than 20 seconds'73 when 20..39 then 'half a minute' 74 when 40..59 then 'less than a minute' 75 else ' 1 minute'70 when 0..4 then 'less than %d seconds'.t 5 71 when 5..9 then 'less than %d seconds'.t 10 72 when 10..19 then 'less than %d seconds'.t 20 73 when 20..39 then 'half a minute'.t 74 when 40..59 then 'less than a minute'.t 75 else '%d minutes'.t 1 76 76 end 77 77 78 when 2..44 then "#{distance_in_minutes} minutes"79 when 45..89 then 'about 1 hour'80 when 90..1439 then "about #{(distance_in_minutes.to_f / 60.0).round} hours"81 when 1440..2879 then ' 1 day'82 when 2880..43199 then "#{(distance_in_minutes / 1440).round} days"83 when 43200..86399 then 'about 1 month'84 when 86400..525599 then "#{(distance_in_minutes / 43200).round} months"85 when 525600..1051199 then 'about 1 year'86 else "over #{(distance_in_minutes / 525600).round} years"78 when 2..44 then '%d minutes'.t distance_in_minutes 79 when 45..89 then 'about %d hours'.t 1 80 when 90..1439 then 'about %d hours'.t((distance_in_minutes.to_f / 60.0).round) 81 when 1440..2879 then '%d days'.t 1 82 when 2880..43199 then '%d days'.t((distance_in_minutes / 1440).round) 83 when 43200..86399 then 'about %d months'.t 1 84 when 86400..525599 then '%d months'.t((distance_in_minutes / 43200).round) 85 when 525600..1051199 then 'about %d years'.t 1 86 else 'over %d years'.t((distance_in_minutes / 525600).round) 87 87 end 88 88 end 89 89 … … 495 495 end 496 496 497 497 month_options << ((val == month_number) ? 498 %(<option value="#{month_number}" selected="selected">#{month_name }</option>\n) :499 %(<option value="#{month_number}">#{month_name }</option>\n)498 %(<option value="#{month_number}" selected="selected">#{month_name.to_s.t}</option>\n) : 499 %(<option value="#{month_number}">#{month_name.to_s.t}</option>\n) 500 500 ) 501 501 end 502 502 select_html(options[:field_name] || 'month', month_options, options)