Skip to content

Commit

Permalink
Merge pull request #468 from jeffjyang/upcased-date-format-directives
Browse files Browse the repository at this point in the history
Add support for uppercased date format directives
  • Loading branch information
radar authored Feb 26, 2019
2 parents 8cfff80 + 405a7a8 commit 70daba7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 17 deletions.
6 changes: 5 additions & 1 deletion lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,16 @@ def load_json(filename)
end

def translate_localization_format(locale, object, format, options)
format.to_s.gsub(/%[aAbBpP]/) do |match|
format.to_s.gsub(/%(|\^)[aAbBpP]/) do |match|
case match
when '%a' then I18n.t!(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday]
when '%^a' then I18n.t!(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday].upcase
when '%A' then I18n.t!(:"date.day_names", :locale => locale, :format => format)[object.wday]
when '%^A' then I18n.t!(:"date.day_names", :locale => locale, :format => format)[object.wday].upcase
when '%b' then I18n.t!(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
when '%^b' then I18n.t!(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon].upcase
when '%B' then I18n.t!(:"date.month_names", :locale => locale, :format => format)[object.mon]
when '%^B' then I18n.t!(:"date.month_names", :locale => locale, :format => format)[object.mon].upcase
when '%p' then I18n.t!(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).upcase if object.respond_to? :hour
when '%P' then I18n.t!(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).downcase if object.respond_to? :hour
end
Expand Down
30 changes: 24 additions & 6 deletions lib/i18n/tests/localization/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def setup
end

test "localize Date: given the short format it uses it" do
# TODO should be Mrz, shouldn't it?
assert_equal '01. Mar', I18n.l(@date, :format => :short, :locale => :de)
assert_equal '01. Mär', I18n.l(@date, :format => :short, :locale => :de)
end

test "localize Date: given the long format it uses it" do
Expand All @@ -27,17 +26,36 @@ def setup
assert_equal 'Samstag', I18n.l(@date, :format => '%A', :locale => :de)
end

test "localize Date: given a uppercased day name format it returns the correct day name in upcase" do
assert_equal 'samstag'.upcase, I18n.l(@date, :format => '%^A', :locale => :de)
end

test "localize Date: given an abbreviated day name format it returns the correct abbreviated day name" do
assert_equal 'Sa', I18n.l(@date, :format => '%a', :locale => :de)
end

test "localize Date: given an abbreviated and uppercased day name format it returns the correct abbreviated day name in upcase" do
assert_equal 'sa'.upcase, I18n.l(@date, :format => '%^a', :locale => :de)
end

test "localize Date: given a month name format it returns the correct month name" do
assert_equal 'März', I18n.l(@date, :format => '%B', :locale => :de)
end

test "localize Date: given a uppercased month name format it returns the correct month name in upcase" do
assert_equal 'märz'.upcase, I18n.l(@date, :format => '%^B', :locale => :de)
end

test "localize Date: given an abbreviated month name format it returns the correct abbreviated month name" do
# TODO should be Mrz, shouldn't it?
assert_equal 'Mar', I18n.l(@date, :format => '%b', :locale => :de)
assert_equal 'Mär', I18n.l(@date, :format => '%b', :locale => :de)
end

test "localize Date: given an abbreviated and uppercased month name format it returns the correct abbreviated month name in upcase" do
assert_equal 'mär'.upcase, I18n.l(@date, :format => '%^b', :locale => :de)
end

test "localize Date: given a date format with the month name upcased it returns the correct value" do
assert_equal '1. FEBRUAR 2008', I18n.l(::Date.new(2008, 2, 1), :format => "%-d. %^B %Y", :locale => :de)
end

test "localize Date: given missing translations it returns the correct error message" do
Expand All @@ -50,7 +68,7 @@ def setup

test "localize Date: does not modify the options hash" do
options = { :format => '%b', :locale => :de }
assert_equal 'Mar', I18n.l(@date, options)
assert_equal 'Mär', I18n.l(@date, options)
assert_equal({ :format => '%b', :locale => :de }, options)
assert_nothing_raised { I18n.l(@date, options.freeze) }
end
Expand Down Expand Up @@ -89,7 +107,7 @@ def setup_date_translations
:day_names => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
:abbr_day_names => %w(So Mo Di Mi Do Fr Sa),
:month_names => %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember).unshift(nil),
:abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil)
:abbr_month_names => %w(Jan Feb Mär Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil)
}
}
end
Expand Down
29 changes: 23 additions & 6 deletions lib/i18n/tests/localization/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,51 @@ def setup
end

test "localize DateTime: given the short format it uses it" do
# TODO should be Mrz, shouldn't it?
assert_equal '01. Mar 06:00', I18n.l(@datetime, :format => :short, :locale => :de)
assert_equal '01. Mär 06:00', I18n.l(@datetime, :format => :short, :locale => :de)
end

test "localize DateTime: given the long format it uses it" do
assert_equal '01. März 2008 06:00', I18n.l(@datetime, :format => :long, :locale => :de)
end

test "localize DateTime: given the default format it uses it" do
# TODO should be Mrz, shouldn't it?
assert_equal 'Sa, 01. Mar 2008 06:00:00 +0000', I18n.l(@datetime, :format => :default, :locale => :de)
assert_equal 'Sa, 01. Mär 2008 06:00:00 +0000', I18n.l(@datetime, :format => :default, :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

test "localize DateTime: given a uppercased day name format it returns the correct day name in upcase" do
assert_equal 'samstag'.upcase, I18n.l(@datetime, :format => '%^A', :locale => :de)
end

test "localize DateTime: given an abbreviated day name format it returns the correct abbreviated day name" do
assert_equal 'Sa', I18n.l(@datetime, :format => '%a', :locale => :de)
end

test "localize DateTime: given an abbreviated and uppercased day name format it returns the correct abbreviated day name in upcase" do
assert_equal 'sa'.upcase, I18n.l(@datetime, :format => '%^a', :locale => :de)
end

test "localize DateTime: given a month name format it returns the correct month name" do
assert_equal 'März', I18n.l(@datetime, :format => '%B', :locale => :de)
end

test "localize DateTime: given a uppercased month name format it returns the correct month name in upcase" do
assert_equal 'märz'.upcase, I18n.l(@datetime, :format => '%^B', :locale => :de)
end

test "localize DateTime: given an abbreviated month name format it returns the correct abbreviated month name" do
# TODO should be Mrz, shouldn't it?
assert_equal 'Mar', I18n.l(@datetime, :format => '%b', :locale => :de)
assert_equal 'Mär', I18n.l(@datetime, :format => '%b', :locale => :de)
end

test "localize DateTime: given an abbreviated and uppercased month name format it returns the correct abbreviated month name in upcase" do
assert_equal 'mär'.upcase, I18n.l(@datetime, :format => '%^b', :locale => :de)
end

test "localize DateTime: given a date format with the month name upcased it returns the correct value" do
assert_equal '1. FEBRUAR 2008', I18n.l(::DateTime.new(2008, 2, 1, 6), :format => "%-d. %^B %Y", :locale => :de)
end

test "localize DateTime: given missing translations it returns the correct error message" do
Expand Down
26 changes: 22 additions & 4 deletions lib/i18n/tests/localization/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def setup
end

test "localize Time: given the short format it uses it" do
# TODO should be Mrz, shouldn't it?
assert_equal '01. Mar 06:00', I18n.l(@time, :format => :short, :locale => :de)
assert_equal '01. Mär 06:00', I18n.l(@time, :format => :short, :locale => :de)
end

test "localize Time: given the long format it uses it" do
Expand All @@ -29,17 +28,36 @@ def setup
assert_equal 'Samstag', I18n.l(@time, :format => '%A', :locale => :de)
end

test "localize Time: given a uppercased day name format it returns the correct day name in upcase" do
assert_equal 'samstag'.upcase, I18n.l(@time, :format => '%^A', :locale => :de)
end

test "localize Time: given an abbreviated day name format it returns the correct abbreviated day name" do
assert_equal 'Sa', I18n.l(@time, :format => '%a', :locale => :de)
end

test "localize Time: given an abbreviated and uppercased day name format it returns the correct abbreviated day name in upcase" do
assert_equal 'sa'.upcase, I18n.l(@time, :format => '%^a', :locale => :de)
end

test "localize Time: given a month name format it returns the correct month name" do
assert_equal 'März', I18n.l(@time, :format => '%B', :locale => :de)
end

test "localize Time: given a uppercased month name format it returns the correct month name in upcase" do
assert_equal 'märz'.upcase, I18n.l(@time, :format => '%^B', :locale => :de)
end

test "localize Time: given an abbreviated month name format it returns the correct abbreviated month name" do
# TODO should be Mrz, shouldn't it?
assert_equal 'Mar', I18n.l(@time, :format => '%b', :locale => :de)
assert_equal 'Mär', I18n.l(@time, :format => '%b', :locale => :de)
end

test "localize Time: given an abbreviated and uppercased month name format it returns the correct abbreviated month name in upcase" do
assert_equal 'mär'.upcase, I18n.l(@time, :format => '%^b', :locale => :de)
end

test "localize Time: given a date format with the month name upcased it returns the correct value" do
assert_equal '1. FEBRUAR 2008', I18n.l(::Time.utc(2008, 2, 1, 6, 0), :format => "%-d. %^B %Y", :locale => :de)
end

test "localize Time: given missing translations it returns the correct error message" do
Expand Down

0 comments on commit 70daba7

Please sign in to comment.