diff --git a/lib/i18n/interpolate/ruby.rb b/lib/i18n/interpolate/ruby.rb index 5b50593f..fc0abda9 100644 --- a/lib/i18n/interpolate/ruby.rb +++ b/lib/i18n/interpolate/ruby.rb @@ -43,7 +43,13 @@ def interpolate_hash(string, values) config.missing_interpolation_argument_handler.call(key, values, string) end value = value.call(values) if value.respond_to?(:call) - $3 ? sprintf("%#{$3}", value) : value + + # Localize interpolated Date's, Time's, and DateTime's + if value.respond_to?(:strftime) + backend.localize(locale, value) + else + $3 ? sprintf("%#{$3}", value) : value + end end end diff --git a/lib/i18n/tests/interpolation.rb b/lib/i18n/tests/interpolation.rb index 6bfe2e03..5180b7d1 100644 --- a/lib/i18n/tests/interpolation.rb +++ b/lib/i18n/tests/interpolation.rb @@ -107,6 +107,46 @@ module Interpolation end end + test "interpolation: given a Date with no default format set it raises I18n::MissingTranslationData" do + assert_raises(I18n::MissingTranslationData) do + date = Date.new(2008, 3, 1) + interpolate(:default => '%{date}', :date => date) + end + end + + test "interpolation: given a Time with no default format set it raises I18n::MissingTranslationData" do + assert_raises(I18n::MissingTranslationData) do + time = Time.utc(2008, 3, 1, 6, 0) + interpolate(:default => '%{time}', :time => time) + end + end + + test "interpolation: given a DateTime with no default format set it raises I18n::MissingTranslationData" do + assert_raises(I18n::MissingTranslationData) do + datetime = Time.utc(2008, 3, 1, 6, 0).to_datetime + interpolate(:default => '%{datetime}', :datetime => datetime) + end + end + + test "interpolation: given a Date it localizes it to the default date format" do + setup_datetime_localizations + date = Date.new(2008, 3, 1) + assert_equal '01.03.2008', interpolate(:default => '%{date}', :date => date) + end + + test "interpolation: given a Time it localizes it to the default time format" do + setup_datetime_localizations + time = Time.utc(2008, 3, 1, 6, 0) + assert_equal 'Sa, 01. Mär 2008 06:00:00 +0000', interpolate(:default => '%{time}', :time => time) + end + + # DateTime uses the Time format + test "interpolation: given a DateTime it localizes it to the default time format" do + setup_datetime_localizations + datetime = Time.utc(2008, 3, 1, 6, 0).to_datetime + assert_equal 'Sa, 01. Mär 2008 06:00:00 +0000', interpolate(:default => '%{datetime}', :datetime => datetime) + end + test "interpolation: given a translations containing a reserved key it raises I18n::ReservedInterpolationKey" do assert_raises(I18n::ReservedInterpolationKey) { interpolate(:foo => :bar, :default => '%{exception_handler}') } assert_raises(I18n::ReservedInterpolationKey) { interpolate(:foo => :bar, :default => '%{default}') } @@ -158,6 +198,25 @@ def interpolate(*args) key = args.pop I18n.backend.translate('en', key, options) end + + def setup_datetime_localizations + I18n.backend.store_translations :en, { + :date => { + :formats => { + :default => "%d.%m.%Y" + }, + # :time format uses abbreviated day and month names + :abbr_day_names => %w(So Mo Di Mi Do Fr Sa), + :abbr_month_names => %w(Jan Feb Mär Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil) + }, + + :time => { + :formats => { + :default => "%a, %d. %b %Y %H:%M:%S %z" + } + } + } + end end end end diff --git a/lib/i18n/tests/localization/date.rb b/lib/i18n/tests/localization/date.rb index c21fbbf3..0a3a5e68 100644 --- a/lib/i18n/tests/localization/date.rb +++ b/lib/i18n/tests/localization/date.rb @@ -22,6 +22,10 @@ def setup assert_equal '01.03.2008', I18n.l(@date, :format => :default, :locale => :de) end + test "localize Date: given no format it uses the default" do + assert_equal '01.03.2008', I18n.l(@date, :locale => :de) + end + test "localize Date: given a day name format it returns the correct day name" do assert_equal 'Samstag', I18n.l(@date, :format => '%A', :locale => :de) end diff --git a/lib/i18n/tests/localization/date_time.rb b/lib/i18n/tests/localization/date_time.rb index b5d3527d..9c30a8ea 100644 --- a/lib/i18n/tests/localization/date_time.rb +++ b/lib/i18n/tests/localization/date_time.rb @@ -23,6 +23,10 @@ def setup assert_equal 'Sa, 01. Mär 2008 06:00:00 +0000', I18n.l(@datetime, :format => :default, :locale => :de) end + test "localize DateTime: given no format it uses the default" do + assert_equal 'Sa, 01. Mär 2008 06:00:00 +0000', I18n.l(@datetime, :locale => :de) + end + test "localize DateTime: given a day name format it returns the correct day name" do assert_equal 'Samstag', I18n.l(@datetime, :format => '%A', :locale => :de) end diff --git a/lib/i18n/tests/localization/time.rb b/lib/i18n/tests/localization/time.rb index 456a7602..fa429da7 100644 --- a/lib/i18n/tests/localization/time.rb +++ b/lib/i18n/tests/localization/time.rb @@ -19,10 +19,13 @@ def setup assert_equal '01. März 2008 06:00', I18n.l(@time, :format => :long, :locale => :de) end - # TODO Seems to break on Windows because ENV['TZ'] is ignored. What's a better way to do this? - # def test_localize_given_the_default_format_it_uses_it - # assert_equal 'Sa, 01. Mar 2008 06:00:00 +0000', I18n.l(@time, :format => :default, :locale => :de) - # end + test "localize Time: given the default format it uses it" do + assert_equal 'Sa, 01. Mär 2008 06:00:00 +0000', I18n.l(@time, :format => :default, :locale => :de) + end + + test "localize Time: given no format it uses the default" do + assert_equal 'Sa, 01. Mär 2008 06:00:00 +0000', I18n.l(@time, :locale => :de) + end test "localize Time: given a day name format it returns the correct day name" do assert_equal 'Samstag', I18n.l(@time, :format => '%A', :locale => :de)