From a18f855965f0b3b0dddb8a9758b06b5674be8d51 Mon Sep 17 00:00:00 2001 From: Thales Oliveira Date: Sun, 2 Mar 2014 08:54:20 -0300 Subject: [PATCH 01/11] Using same format for date.to_s and default en.yml --- .../lib/active_support/core_ext/date/conversions.rb | 2 +- activesupport/lib/active_support/locale/en.yml | 2 +- activesupport/test/core_ext/date_ext_test.rb | 6 ++++++ activesupport/test/i18n_test.rb | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index df419a6e63271..f5a2a21853f17 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -6,7 +6,7 @@ class Date DATE_FORMATS = { :short => '%e %b', - :long => '%B %e, %Y', + :long => '%B %-e, %Y', :db => '%Y-%m-%d', :number => '%Y%m%d', :long_ordinal => lambda { |date| diff --git a/activesupport/lib/active_support/locale/en.yml b/activesupport/lib/active_support/locale/en.yml index a4563ace8ff1d..a96c223d35d63 100644 --- a/activesupport/lib/active_support/locale/en.yml +++ b/activesupport/lib/active_support/locale/en.yml @@ -6,7 +6,7 @@ en: # You can provide other formats here if you like! default: "%Y-%m-%d" short: "%b %d" - long: "%B %d, %Y" + long: "%B %-e, %Y" day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday] abbr_day_names: [Sun, Mon, Tue, Wed, Thu, Fri, Sat] diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 5d0af035cc134..5ed398f9e184c 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -28,6 +28,12 @@ def test_to_s assert_equal "2005-02-21", date.to_s(:iso8601) end + def test_to_s_one_digit_day + date = Date.new(2005, 2, 2) + + assert_equal "February 2, 2005", date.to_s(:long) + 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/i18n_test.rb b/activesupport/test/i18n_test.rb index 5ef59b6e6bd27..0b36006f326dd 100644 --- a/activesupport/test/i18n_test.rb +++ b/activesupport/test/i18n_test.rb @@ -26,7 +26,7 @@ def test_date_localization_with_short_format end def test_date_localization_with_long_format - assert_equal @date.strftime("%B %d, %Y"), I18n.localize(@date, :format => :long) + assert_equal @date.strftime("%B %-e, %Y"), I18n.localize(@date, :format => :long) end def test_time_localization_should_use_default_format From a95f0f8d4dda9856614cfaa151180827c0b275f8 Mon Sep 17 00:00:00 2001 From: Thales Oliveira Date: Sun, 2 Mar 2014 10:00:57 -0300 Subject: [PATCH 02/11] Removing unnecessary space --- activesupport/test/core_ext/date_ext_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 5ed398f9e184c..31fd97a2e0927 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -30,7 +30,6 @@ def test_to_s def test_to_s_one_digit_day date = Date.new(2005, 2, 2) - assert_equal "February 2, 2005", date.to_s(:long) end From 2312917aea31f1d06056735ee5ff38308b8e3207 Mon Sep 17 00:00:00 2001 From: Thales Oliveira Date: Sun, 2 Mar 2014 10:01:34 -0300 Subject: [PATCH 03/11] Replacing dynamic date and time values for literal ones on asserts --- activesupport/test/i18n_test.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/activesupport/test/i18n_test.rb b/activesupport/test/i18n_test.rb index 0b36006f326dd..00c1d556976dd 100644 --- a/activesupport/test/i18n_test.rb +++ b/activesupport/test/i18n_test.rb @@ -10,39 +10,39 @@ def setup def test_time_zone_localization_with_default_format now = Time.local(2000) - assert_equal now.strftime("%a, %d %b %Y %H:%M:%S %z"), I18n.localize(now) + assert_equal "Sat, 01 Jan 2000 00:00:00 -0200", I18n.localize(now) end def test_date_localization_should_use_default_format - assert_equal @date.strftime("%Y-%m-%d"), I18n.localize(@date) + assert_equal "2008-07-02", I18n.localize(@date) end def test_date_localization_with_default_format - assert_equal @date.strftime("%Y-%m-%d"), I18n.localize(@date, :format => :default) + assert_equal "2008-07-02", I18n.localize(@date, :format => :default) end def test_date_localization_with_short_format - assert_equal @date.strftime("%b %d"), I18n.localize(@date, :format => :short) + assert_equal "Jul 02", I18n.localize(@date, :format => :short) end def test_date_localization_with_long_format - assert_equal @date.strftime("%B %-e, %Y"), I18n.localize(@date, :format => :long) + assert_equal "July 2, 2008", I18n.localize(@date, :format => :long) end def test_time_localization_should_use_default_format - assert_equal @time.strftime("%a, %d %b %Y %H:%M:%S %z"), I18n.localize(@time) + assert_equal "Wed, 02 Jul 2008 16:47:01 +0000", I18n.localize(@time) end def test_time_localization_with_default_format - assert_equal @time.strftime("%a, %d %b %Y %H:%M:%S %z"), I18n.localize(@time, :format => :default) + assert_equal "Wed, 02 Jul 2008 16:47:01 +0000", I18n.localize(@time, :format => :default) end def test_time_localization_with_short_format - assert_equal @time.strftime("%d %b %H:%M"), I18n.localize(@time, :format => :short) + assert_equal "02 Jul 16:47", I18n.localize(@time, :format => :short) end def test_time_localization_with_long_format - assert_equal @time.strftime("%B %d, %Y %H:%M"), I18n.localize(@time, :format => :long) + assert_equal "July 02, 2008 16:47", I18n.localize(@time, :format => :long) end def test_day_names From 3894c2ba2e611748cd0a30ce3d5194443121e628 Mon Sep 17 00:00:00 2001 From: Thales Oliveira Date: Mon, 3 Mar 2014 01:06:53 -0300 Subject: [PATCH 04/11] Using a known timezone rather than the local one --- activesupport/test/i18n_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activesupport/test/i18n_test.rb b/activesupport/test/i18n_test.rb index 00c1d556976dd..b13f4e8417322 100644 --- a/activesupport/test/i18n_test.rb +++ b/activesupport/test/i18n_test.rb @@ -9,8 +9,8 @@ def setup end def test_time_zone_localization_with_default_format - now = Time.local(2000) - assert_equal "Sat, 01 Jan 2000 00:00:00 -0200", I18n.localize(now) + now = Time.utc(2000).in_time_zone('Alaska') + assert_equal "Fri, 31 Dec 1999 15:00:00 -0900", I18n.localize(now) end def test_date_localization_should_use_default_format From 68f07c2e9443dae09c2540da6cd2af2b6a0cef53 Mon Sep 17 00:00:00 2001 From: Thales Oliveira Date: Mon, 3 Mar 2014 01:14:27 -0300 Subject: [PATCH 05/11] Replicating discrepancy fix for Time --- activesupport/lib/active_support/locale/en.yml | 2 +- activesupport/test/i18n_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/locale/en.yml b/activesupport/lib/active_support/locale/en.yml index a96c223d35d63..43cb53174ca0e 100644 --- a/activesupport/lib/active_support/locale/en.yml +++ b/activesupport/lib/active_support/locale/en.yml @@ -24,7 +24,7 @@ en: formats: default: "%a, %d %b %Y %H:%M:%S %z" short: "%d %b %H:%M" - long: "%B %d, %Y %H:%M" + long: "%B %-e, %Y %H:%M" am: "am" pm: "pm" diff --git a/activesupport/test/i18n_test.rb b/activesupport/test/i18n_test.rb index b13f4e8417322..a8080272a2e0a 100644 --- a/activesupport/test/i18n_test.rb +++ b/activesupport/test/i18n_test.rb @@ -42,7 +42,7 @@ def test_time_localization_with_short_format end def test_time_localization_with_long_format - assert_equal "July 02, 2008 16:47", I18n.localize(@time, :format => :long) + assert_equal "July 2, 2008 16:47", I18n.localize(@time, :format => :long) end def test_day_names From 3a2e3623d128480852dfb6a2e6bbc40fe1c5ec7d Mon Sep 17 00:00:00 2001 From: Thales Oliveira Date: Mon, 3 Mar 2014 01:15:16 -0300 Subject: [PATCH 06/11] Adding changelog entry --- activesupport/CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 38a761384eacd..8fad60373c5fc 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,12 @@ +* Fixing discrepancy between date and time to_s(:long) and I18n.localize(date, format: long). + Previously, a call to date.to_s(:long) when there was only one digit in the day would cause + a zero to be added, while calling I18n.localize(date, format: long) would add an unnecessary + space. Now, both calls return the date/time without the zero and the space. + + Fixes #14245. + + *Thales Oliveira* + * Change the signature of `fetch_multi` to return a hash rather than an array. This makes it consistent with the output of `read_multi`. From e363c0aa565571ce680c75db622d13df075be739 Mon Sep 17 00:00:00 2001 From: Thales Oliveira Date: Mon, 3 Mar 2014 20:50:42 -0300 Subject: [PATCH 07/11] Fixing short and default formats for Time and Date --- .../lib/active_support/core_ext/date/conversions.rb | 2 +- activesupport/lib/active_support/locale/en.yml | 6 +++--- activesupport/test/core_ext/date_ext_test.rb | 3 ++- activesupport/test/i18n_test.rb | 10 +++++----- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index f5a2a21853f17..63b039453bffc 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -5,7 +5,7 @@ class Date DATE_FORMATS = { - :short => '%e %b', + :short => '%b %-e', :long => '%B %-e, %Y', :db => '%Y-%m-%d', :number => '%Y%m%d', diff --git a/activesupport/lib/active_support/locale/en.yml b/activesupport/lib/active_support/locale/en.yml index 43cb53174ca0e..66f609ecfffc0 100644 --- a/activesupport/lib/active_support/locale/en.yml +++ b/activesupport/lib/active_support/locale/en.yml @@ -5,7 +5,7 @@ en: # When no format has been given, it uses default. # You can provide other formats here if you like! default: "%Y-%m-%d" - short: "%b %d" + short: "%b %-e" long: "%B %-e, %Y" day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday] @@ -22,8 +22,8 @@ en: time: formats: - default: "%a, %d %b %Y %H:%M:%S %z" - short: "%d %b %H:%M" + default: "%Y-%m-%d %H:%M:%S %z" + short: "%b %-e %H:%M" long: "%B %-e, %Y %H:%M" am: "am" pm: "pm" diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 31fd97a2e0927..c28543c381a5f 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -20,7 +20,7 @@ def test_tomorrow_in_calendar_reform 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) @@ -30,6 +30,7 @@ def test_to_s def test_to_s_one_digit_day date = Date.new(2005, 2, 2) + assert_equal "Feb 2", date.to_s(:short) assert_equal "February 2, 2005", date.to_s(:long) end diff --git a/activesupport/test/i18n_test.rb b/activesupport/test/i18n_test.rb index a8080272a2e0a..4b4a8e072c542 100644 --- a/activesupport/test/i18n_test.rb +++ b/activesupport/test/i18n_test.rb @@ -10,7 +10,7 @@ def setup def test_time_zone_localization_with_default_format now = Time.utc(2000).in_time_zone('Alaska') - assert_equal "Fri, 31 Dec 1999 15:00:00 -0900", I18n.localize(now) + assert_equal "1999-12-31 15:00:00 -0900", I18n.localize(now) end def test_date_localization_should_use_default_format @@ -22,7 +22,7 @@ def test_date_localization_with_default_format end def test_date_localization_with_short_format - assert_equal "Jul 02", I18n.localize(@date, :format => :short) + assert_equal "Jul 2", I18n.localize(@date, :format => :short) end def test_date_localization_with_long_format @@ -30,15 +30,15 @@ def test_date_localization_with_long_format end def test_time_localization_should_use_default_format - assert_equal "Wed, 02 Jul 2008 16:47:01 +0000", I18n.localize(@time) + assert_equal "2008-07-02 16:47:01 +0000", I18n.localize(@time) end def test_time_localization_with_default_format - assert_equal "Wed, 02 Jul 2008 16:47:01 +0000", I18n.localize(@time, :format => :default) + assert_equal "2008-07-02 16:47:01 +0000", I18n.localize(@time, :format => :default) end def test_time_localization_with_short_format - assert_equal "02 Jul 16:47", I18n.localize(@time, :format => :short) + assert_equal "Jul 2 16:47", I18n.localize(@time, :format => :short) end def test_time_localization_with_long_format From 741e2ab4f73828c9eb34b4c4938b4af5d3801e16 Mon Sep 17 00:00:00 2001 From: Thales Oliveira Date: Mon, 3 Mar 2014 21:13:33 -0300 Subject: [PATCH 08/11] Updating date helper to reflect changes on the date/time formats --- actionview/test/template/date_helper_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actionview/test/template/date_helper_test.rb b/actionview/test/template/date_helper_test.rb index 6f77c3c99d180..792448b8a2e60 100644 --- a/actionview/test/template/date_helper_test.rb +++ b/actionview/test/template/date_helper_test.rb @@ -3197,7 +3197,7 @@ def test_time_tag_with_given_block def test_time_tag_with_different_format time = Time.new(2013, 2, 20, 0, 0, 0, '+00:00') - expected = '' + expected = '' assert_equal expected, time_tag(time, :format => :short) end From 039eacf764102d6802d34e48052a63d06bb9a8ee Mon Sep 17 00:00:00 2001 From: Thales Oliveira Date: Mon, 3 Mar 2014 21:18:03 -0300 Subject: [PATCH 09/11] Updating the guides to reflect the changes made on I18n Date/Time formats --- guides/source/i18n.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/guides/source/i18n.md b/guides/source/i18n.md index d72717fa3b2a8..5400a0f92d955 100644 --- a/guides/source/i18n.md +++ b/guides/source/i18n.md @@ -434,6 +434,19 @@ So that would give you: ![rails i18n demo localized time to pirate](images/i18n/demo_localized_pirate.png) +It is important to note that the standard long, short and default Date/Time formats used by Rails do not pad one digit days with zeroes or spaces, e.g.: + +```erb +# app/views/home/index.html.erb + +

<%=t :hello_world %>

+

<%= l Date.new(2014, 2, 2), format: :short %>

+

<%= l Date.new(2014, 2, 2), format: :long %>

+

<%= l Date.new(2014, 2, 2), format: :default %>

+

<%= l Date.new(2014, 2, 2) %>

+``` +The same applies for the Time object. + TIP: Right now you might need to add some more date/time formats in order to make the I18n backend work as expected (at least for the 'pirate' locale). Of course, there's a great chance that somebody already did all the work by **translating Rails' defaults for your locale**. See the [rails-i18n repository at GitHub](https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale) for an archive of various locale files. When you put such file(s) in `config/locales/` directory, they will automatically be ready for use. ### Inflection Rules For Other Locales From 424bfc362648d1688f2697549630a42b5292d75e Mon Sep 17 00:00:00 2001 From: Thales Oliveira Date: Mon, 3 Mar 2014 21:23:21 -0300 Subject: [PATCH 10/11] Updating the changelog --- activesupport/CHANGELOG.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 8fad60373c5fc..2a16f850c9468 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,11 +1,11 @@ -* Fixing discrepancy between date and time to_s(:long) and I18n.localize(date, format: long). - Previously, a call to date.to_s(:long) when there was only one digit in the day would cause - a zero to be added, while calling I18n.localize(date, format: long) would add an unnecessary - space. Now, both calls return the date/time without the zero and the space. +* Fixing discrepancy between Date and Time to_s and I18n.localize for default, short and long formats. + Previously, a call to Date#to_s when there was only one digit in the day would cause + a zero to be added before the day digit, while calling I18n#localize would add an unnecessary + space. Now, both calls return the Date/Time without the zero and the space. Fixes #14245. - *Thales Oliveira* + *Thales Oliveira, Gabriele Cirulli* * Change the signature of `fetch_multi` to return a hash rather than an array. This makes it consistent with the output of `read_multi`. From a4c7df99524924fa5ebb8904b5e994c04f889ffb Mon Sep 17 00:00:00 2001 From: Thales Oliveira Date: Sun, 9 Mar 2014 13:50:39 -0300 Subject: [PATCH 11/11] Wrapping Changelog --- activesupport/CHANGELOG.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 2a16f850c9468..946350168a738 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,7 +1,8 @@ -* Fixing discrepancy between Date and Time to_s and I18n.localize for default, short and long formats. - Previously, a call to Date#to_s when there was only one digit in the day would cause - a zero to be added before the day digit, while calling I18n#localize would add an unnecessary - space. Now, both calls return the Date/Time without the zero and the space. +* Fixing discrepancy between Date and Time to_s and I18n.localize for default, + short and long formats. Previously, a call to Date#to_s when there was only + one digit in the day would cause a zero to be added before the day digit, + while calling I18n#localize would add an unnecessary space. Now, both calls + return the Date/Time without the zero and the space. Fixes #14245.