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

Fixing discrepancy between date.to_s(:long) and localize(date, format: :long) #14245

Closed
2 changes: 1 addition & 1 deletion actionview/test/template/date_helper_test.rb
Expand Up @@ -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 = '<time datetime="2013-02-20T00:00:00+00:00">20 Feb 00:00</time>'
expected = '<time datetime="2013-02-20T00:00:00+00:00">Feb 20 00:00</time>'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about changing the order of day and month in the short format - I think that may be one standardisation too far

assert_equal expected, time_tag(time, :format => :short)
end

Expand Down
10 changes: 10 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,13 @@
* 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, 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`.

Expand Down
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/core_ext/date/conversions.rb
Expand Up @@ -5,8 +5,8 @@

class Date
DATE_FORMATS = {
:short => '%e %b',
:long => '%B %e, %Y',
:short => '%b %-e',
:long => '%B %-e, %Y',
:db => '%Y-%m-%d',
:number => '%Y%m%d',
:long_ordinal => lambda { |date|
Expand Down
10 changes: 5 additions & 5 deletions activesupport/lib/active_support/locale/en.yml
Expand Up @@ -5,8 +5,8 @@ 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"
long: "%B %d, %Y"
short: "%b %-e"
long: "%B %-e, %Y"

day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
abbr_day_names: [Sun, Mon, Tue, Wed, Thu, Fri, Sat]
Expand All @@ -22,9 +22,9 @@ en:

time:
formats:
default: "%a, %d %b %Y %H:%M:%S %z"
short: "%d %b %H:%M"
long: "%B %d, %Y %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"

Expand Down
8 changes: 7 additions & 1 deletion activesupport/test/core_ext/date_ext_test.rb
Expand Up @@ -20,14 +20,20 @@ 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)
assert_equal "21 Feb 2005", date.to_s(:rfc822)
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 "Feb 2", date.to_s(:short)
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
Expand Down
20 changes: 10 additions & 10 deletions activesupport/test/i18n_test.rb
Expand Up @@ -9,40 +9,40 @@ def setup
end

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)
now = Time.utc(2000).in_time_zone('Alaska')
assert_equal "1999-12-31 15:00:00 -0900", 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 2", I18n.localize(@date, :format => :short)
end

def test_date_localization_with_long_format
assert_equal @date.strftime("%B %d, %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 "2008-07-02 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 "2008-07-02 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 "Jul 2 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 2, 2008 16:47", I18n.localize(@time, :format => :long)
end

def test_day_names
Expand Down
13 changes: 13 additions & 0 deletions guides/source/i18n.md
Expand Up @@ -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

<h1><%=t :hello_world %></h1>
<p><%= l Date.new(2014, 2, 2), format: :short %></p>
<p><%= l Date.new(2014, 2, 2), format: :long %></p>
<p><%= l Date.new(2014, 2, 2), format: :default %></p>
<p><%= l Date.new(2014, 2, 2) %></p>
```
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
Expand Down