From 0070b57e2f3d58eeb5e8e844e897e21dc9de164a Mon Sep 17 00:00:00 2001 From: masone (Christian Felder) Date: Sat, 17 Jul 2010 14:47:50 +0200 Subject: [PATCH] I18n for Date#to_s --- .../active_support/core_ext/date/conversions.rb | 10 ++--- .../core_ext/date_time/conversions.rb | 10 +++- .../active_support/core_ext/time/conversions.rb | 13 +++--- activesupport/lib/active_support/locale/en.yml | 5 +- activesupport/lib/active_support/time_with_zone.rb | 12 ++++- activesupport/test/core_ext/date_ext_test.rb | 40 +++++++++++++++++++- activesupport/test/core_ext/date_time_ext_test.rb | 40 +++++++++++++++++-- activesupport/test/core_ext/time_ext_test.rb | 33 +++++++++++++++- activesupport/test/core_ext/time_with_zone_test.rb | 23 +++++++++++ 9 files changed, 158 insertions(+), 28 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index 092f936..50bc1fe 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -4,8 +4,6 @@ require 'active_support/core_ext/date/zones' class Date DATE_FORMATS = { - :short => "%e %b", - :long => "%B %e, %Y", :db => "%Y-%m-%d", :number => "%Y%m%d", :long_ordinal => lambda { |date| date.strftime("%B #{ActiveSupport::Inflector.ordinalize(date.day)}, %Y") }, # => "April 25th, 2007" @@ -18,7 +16,7 @@ class Date # Ruby 1.9 has Date#xmlschema which converts to a string without the time component. remove_method :xmlschema if method_defined?(:xmlschema) - # Convert to a formatted string. See DATE_FORMATS for predefined formats. + # Convert to a localized formatted string. See DATE_FORMATS for predefined formats. # # This method is aliased to to_s. # @@ -44,12 +42,12 @@ class Date def to_formatted_s(format = :default) if formatter = DATE_FORMATS[format] if formatter.respond_to?(:call) - formatter.call(self).to_s + I18n.l self, :format => formatter.call(self).to_s else - strftime(formatter) + I18n.l self, :format => formatter end else - to_default_s + I18n.l self, :format => format end end alias_method :to_default_s, :to_s diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index 47b8aa5..9576c49 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -8,7 +8,7 @@ class DateTime # DateTimes outside the range of what can be created with Time. remove_method :to_time if instance_methods.include?(:to_time) - # Convert to a formatted string. See Time::DATE_FORMATS for predefined formats. + # Convert to a localized formatted string. See Time::DATE_FORMATS for predefined formats. # # This method is aliased to to_s. # @@ -34,9 +34,13 @@ class DateTime # Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") } def to_formatted_s(format = :default) if formatter = ::Time::DATE_FORMATS[format] - formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter) + if formatter.respond_to?(:call) + I18n.l self, :format => formatter.call(self).to_s + else + I18n.l self, :format => formatter + end else - to_default_s + I18n.l self, :format => format end end alias_method :to_default_s, :to_s unless (instance_methods(false) & [:to_s, 'to_s']).empty? diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb index 86103eb..7bcd70f 100644 --- a/activesupport/lib/active_support/core_ext/time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/time/conversions.rb @@ -6,14 +6,11 @@ class Time DATE_FORMATS = { :db => "%Y-%m-%d %H:%M:%S", :number => "%Y%m%d%H%M%S", - :time => "%H:%M", - :short => "%d %b %H:%M", - :long => "%B %d, %Y %H:%M", :long_ordinal => lambda { |time| time.strftime("%B #{ActiveSupport::Inflector.ordinalize(time.day)}, %Y %H:%M") }, :rfc822 => lambda { |time| time.strftime("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") } } - # Converts to a formatted string. See DATE_FORMATS for builtin formats. + # Converts to a localized formatted string. See DATE_FORMATS for builtin formats. # # This method is aliased to to_s. # @@ -39,9 +36,13 @@ class Time # Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") } def to_formatted_s(format = :default) if formatter = DATE_FORMATS[format] - formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter) + if formatter.respond_to?(:call) + I18n.l self, :format => formatter.call(self).to_s + else + I18n.l self, :format => formatter + end else - to_default_s + I18n.l self, :format => format end end alias_method :to_default_s, :to_s diff --git a/activesupport/lib/active_support/locale/en.yml b/activesupport/lib/active_support/locale/en.yml index 8f08f37..d839c97 100644 --- a/activesupport/lib/active_support/locale/en.yml +++ b/activesupport/lib/active_support/locale/en.yml @@ -7,7 +7,7 @@ en: 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] @@ -23,8 +23,9 @@ en: time: formats: default: "%a, %d %b %Y %H:%M:%S %z" + time: "%H:%M" short: "%d %b %H:%M" - long: "%B %d, %Y %H:%M" + long: "%B %d, %Y %H:%M" am: "am" pm: "pm" diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 62d02bd..7f45062 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -154,12 +154,18 @@ module ActiveSupport # :db format outputs time in UTC; all others output time in local. # Uses TimeWithZone's +strftime+, so %Z and %z work correctly. def to_s(format = :default) - if format == :db + if format == :default + "#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby 1.9 Time#to_s format + elsif format == :db utc.to_s(format) elsif formatter = ::Time::DATE_FORMATS[format] - formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter) + if formatter.respond_to?(:call) + I18n.l self, :format => formatter.call(self).to_s + else + I18n.l self, :format => formatter + end else - "#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby 1.9 Time#to_s format + I18n.l self, :format => format end end alias_method :to_formatted_s, :to_s diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 59c168d..4327a20 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -2,16 +2,54 @@ require 'abstract_unit' require 'active_support/time' class DateExtCalculationsTest < ActiveSupport::TestCase + def test_to_s date = Date.new(2005, 2, 21) assert_equal "2005-02-21", date.to_s - assert_equal "21 Feb", date.to_s(:short) + assert_equal "Feb 21", date.to_s(:short) assert_equal "February 21, 2005", date.to_s(:long) assert_equal "February 21st, 2005", date.to_s(:long_ordinal) assert_equal "2005-02-21", date.to_s(:db) assert_equal "21 Feb 2005", date.to_s(:rfc822) end + def test_date_format_localization + I18n.expects(:t).with(:'date.formats.default', anything()).returns('localized date') + assert_equal "localized date", Date.new(2005, 2, 21).to_s + end + + def test_custom_date_format + Date::DATE_FORMATS[:custom_year_and_month] = '%Y %m' + assert_equal '2005 02', Date.new(2005, 2, 21).to_s(:custom_year_and_month) + Date::DATE_FORMATS.delete(:custom_year_and_month) + end + + def test_custom_date_format_localization + I18n.expects(:t).with(:'date.formats.custom_localized_date', anything()).returns('localized date') + + assert_equal "localized date", Date.new(2005, 2, 21).to_s(:custom_localized_date) + end + + def test_custom_date_format_with_lambda + Date::DATE_FORMATS[:custom_year_and_month] = lambda { |d| d.strftime("%Y %m") } + assert_equal '2005 02', Date.new(2005, 2, 21).to_s(:custom_year_and_month) + Date::DATE_FORMATS.delete(:custom_year_and_month) + end + + def test_custom_date_format_with_lambda_localization + date = Date.new(2005, 2, 21) + Date::DATE_FORMATS[:custom_year_and_month] = lambda { |d| d.strftime("%Y %m") } + I18n.expects(:l).with(date, anything()) + date.to_s(:custom_year_and_month) + Date::DATE_FORMATS.delete(:custom_year_and_month) + end + + def test_missing_date_format + assert_raise I18n::MissingTranslationData do + Date.new(2005, 2, 21).to_s(:undefined_format) + end + end + def test_readable_inspect assert_equal "Mon, 21 Feb 2005", Date.new(2005, 2, 21).readable_inspect assert_equal Date.new(2005, 2, 21).readable_inspect, Date.new(2005, 2, 21).inspect diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 19d7935..101b908 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -2,6 +2,7 @@ require 'abstract_unit' require 'active_support/time' class DateTimeExtCalculationsTest < Test::Unit::TestCase + def test_to_s datetime = DateTime.new(2005, 2, 21, 14, 30, 0, 0) assert_equal "2005-02-21 14:30:00", datetime.to_s(:db) @@ -13,18 +14,47 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_match(/^2005-02-21T14:30:00(Z|\+00:00)$/, datetime.to_s) end - def test_readable_inspect - datetime = DateTime.new(2005, 2, 21, 14, 30, 0) - assert_equal "Mon, 21 Feb 2005 14:30:00 +0000", datetime.readable_inspect - assert_equal datetime.readable_inspect, datetime.inspect + def test_datetime_format_localization + I18n.expects(:t).with(:'time.formats.default', anything()).returns('localized time') + assert_equal "localized time", DateTime.new(2005, 2, 21, 14, 30, 0, 0).to_s end - def test_custom_date_format + def test_custom_datetime_format Time::DATE_FORMATS[:custom] = '%Y%m%d%H%M%S' assert_equal '20050221143000', DateTime.new(2005, 2, 21, 14, 30, 0).to_s(:custom) Time::DATE_FORMATS.delete(:custom) end + def test_custom_datetime_format_localization + I18n.expects(:t).with(:'time.formats.custom_localized_time', anything()).returns('localized time') + assert_equal "localized time", DateTime.new(2005, 2, 21, 14, 30, 0).to_s(:custom_localized_time) + end + + def test_custom_datetime_format_with_lambda + Time::DATE_FORMATS[:custom_day_and_hour] = lambda { |time| time.strftime("%a %H:%M") } + assert_equal 'Mon 14:30', DateTime.new(2005, 2, 21, 14, 30, 0).to_s(:custom_day_and_hour) + Time::DATE_FORMATS.delete(:custom_day_and_hour) + end + + def test_custom_datetime_format_with_lambda_localization + datetime = DateTime.new(2005, 2, 21, 14, 30, 0) + Date::DATE_FORMATS[:custom_day_and_hour] = lambda { |t| t.strftime("%a %H:%M") } + I18n.expects(:l).with(datetime, anything()) + datetime.to_s(:custom_day_and_hour) + end + + def test_missing_datetime_format + assert_raise I18n::MissingTranslationData do + DateTime.new(2005, 2, 21, 14, 30, 0).to_s(:undefined_format) + end + end + + def test_readable_inspect + datetime = DateTime.new(2005, 2, 21, 14, 30, 0) + assert_equal "Mon, 21 Feb 2005 14:30:00 +0000", datetime.readable_inspect + assert_equal datetime.readable_inspect, datetime.inspect + end + def test_to_date assert_equal Date.new(2005, 2, 21), DateTime.new(2005, 2, 21, 14, 30, 0).to_date end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 1cf84df..59725a8 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -494,7 +494,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase def test_to_s time = Time.utc(2005, 2, 21, 17, 44, 30) assert_equal time.to_default_s, time.to_s - assert_equal time.to_default_s, time.to_s(:doesnt_exist) assert_equal "2005-02-21 17:44:30", time.to_s(:db) assert_equal "21 Feb 17:44", time.to_s(:short) assert_equal "17:44", time.to_s(:time) @@ -509,12 +508,42 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_custom_date_format + def test_time_format_localization + I18n.expects(:t).with(:'time.formats.default', anything()).returns('localized time') + assert_equal "localized time", Time.local(2009, 2, 5, 14, 30, 5).to_s + end + + def test_custom_time_format Time::DATE_FORMATS[:custom] = '%Y%m%d%H%M%S' assert_equal '20050221143000', Time.local(2005, 2, 21, 14, 30, 0).to_s(:custom) Time::DATE_FORMATS.delete(:custom) end + def test_custom_time_format_localization + I18n.expects(:t).with(:'time.formats.custom_localized_time', anything()).returns('localized time') + assert_equal "localized time", Time.local(2005, 2, 21, 14, 30, 0).to_s(:custom_localized_time) + end + + def test_custom_time_format_with_lambda + Time::DATE_FORMATS[:custom_day_and_hour] = lambda { |t| t.strftime("%a %H:%M") } + assert_equal 'Mon 14:30', Time.local(2005, 2, 21, 14, 30, 0).to_s(:custom_day_and_hour) + Time::DATE_FORMATS.delete(:custom_day_and_hour) + end + + def test_custom_date_format_with_lambda_localization + time = Time.local(2005, 2, 21, 14, 30, 0) + Date::DATE_FORMATS[:custom_day_and_hour] = lambda { |t| t.strftime("%a %H:%M") } + I18n.expects(:l).with(time, anything()) + time.to_s(:custom_day_and_hour) + Date::DATE_FORMATS.delete(:custom_day_and_hour) + end + + def test_missing_time_format + assert_raise I18n::MissingTranslationData do + Time.local(2005, 2, 21, 14, 30, 0).to_s(:undefined_format) + end + end + def test_to_date assert_equal Date.new(2005, 2, 21), Time.local(2005, 2, 21, 17, 44, 30).to_date end diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 5ce4277..c6b8f2f 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -86,6 +86,29 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal '1999-12-31 19:00:00 -0500', @twz.to_formatted_s end + def test_to_formatted_s_with_lambda + Time::DATE_FORMATS[:custom_month_and_year] = lambda { |time| time.strftime("%B %Y") } + assert_equal 'December 1999', @twz.to_formatted_s(:custom_month_and_year) + Time::DATE_FORMATS.delete(:custom_month_and_year) + end + + def test_custom_date_format_with_lambda_localization + Date::DATE_FORMATS[:custom_day_and_hour] = lambda { |t| t.strftime("%a %H:%M") } + I18n.expects(:l).with(@twz, anything()) + @twz.to_s(:custom_day_and_hour) + Date::DATE_FORMATS.delete(:custom_day_and_hour) + end + + def test_missing_time_format + assert_raise I18n::MissingTranslationData do + @twz.to_s(:undefined_format) + end + end + + def test_formatted_s_short + assert_equal '31 Dec 19:00', @twz.to_s(:short) + end + def test_to_s_db assert_equal '2000-01-01 00:00:00', @twz.to_s(:db) end -- 1.6.5.1