Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use localized date and time formats in interpolate_hash #674

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/i18n/interpolate/ruby.rb
Expand Up @@ -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

Expand Down
59 changes: 59 additions & 0 deletions lib/i18n/tests/interpolation.rb
Expand Up @@ -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}') }
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions lib/i18n/tests/localization/date.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/i18n/tests/localization/date_time.rb
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions lib/i18n/tests/localization/time.rb
Expand Up @@ -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)
Expand Down