From f5581dc2d9acfdd3fb1c32c771cccff9b925f1c4 Mon Sep 17 00:00:00 2001 From: Martin Catty Date: Mon, 18 Jan 2010 12:03:58 +0100 Subject: [PATCH] Add localize to date and time objects, calling the I18n method. --- .../active_support/core_ext/date/conversions.rb | 9 +++++++++ .../active_support/core_ext/time/conversions.rb | 9 +++++++++ activesupport/test/core_ext/date_ext_test.rb | 6 ++++++ activesupport/test/core_ext/time_ext_test.rb | 6 ++++++ 4 files changed, 30 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index 90ab1eb..0260f92 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -95,4 +95,13 @@ class Date def xmlschema to_time.xmlschema end + + # Localize time calling the I18n method. + # + # ==== Examples + # object.date.try(:l, :format => :short) + def localize(options = {}) + I18n.localize(self, options) + end + alias_method :l, :localize end diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb index 6d9c080..dbef1d6 100644 --- a/activesupport/lib/active_support/core_ext/time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/time/conversions.rb @@ -81,4 +81,13 @@ class Time def to_datetime ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400)) end unless method_defined?(:to_datetime) + + # Localize date calling the I18n method. + # + # ==== Examples + # object.time.try(:l, :format => :short) + def localize(options = {}) + I18n.localize(self, options) + end + alias_method :l, :localize end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 23c9bc7..d634d24 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -256,6 +256,12 @@ class DateExtCalculationsTest < Test::Unit::TestCase Date.new(2005,2,28).advance(options) assert_equal({ :years => 3, :months => 11, :days => 2 }, options) end + + def test_localize + today = Date.today + assert_equal today.localize, I18n.localize(today) + assert_equal today.localize(:format => :short), I18n.localize(today, :format => :short) + end protected def with_env_tz(new_tz = 'US/Eastern') diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index f6003bc..d716dc6 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -737,6 +737,12 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert Time === ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone['UTC']) assert_equal false, Time === DateTime.civil(2000) end + + def test_localize + now = Time.now + assert_equal now.localize, I18n.localize(now) + assert_equal now.localize(:format => :short), I18n.localize(now, :format => :short) + end protected def with_env_tz(new_tz = 'US/Eastern') -- 1.6.4.2