Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange I18n date output with rails

I've got a strange problem with date translations in my Ruby On Rails 3 application, and I really don't understand why...

Here are my en.yml and fr.yml :

fr:
  date:
    formats:
      default: "%d/%m/%Y"
      short: "%e %b"
      long: "%e %B %Y" 
  time:
    formats:
      default: "%d %B %Y %H:%M:%S"
      short: "%d %b %H:%M"
      long: "%A %d %B %Y %H:%M"
    am: 'am'
    pm: 'pm'



en:
  date:
    formats:
      default: "%Y-%m-%d"
      long: "%B %d, %Y"
      short: "%b %d"
  time:
    am: am
    formats:
      default: ! '%a, %d %b %Y %H:%M:%S %z'
      long: ! '%B %d, %Y %H:%M'
      short: ! '%d %b %H:%M'
    pm: pm

This is not specific to a particuliar view, but for instance in one of my view :

<td><%=l job_application.created_at, :format => :default %></td>

I get those strange outputs :

With locale = :en
=> t, 30 o 2012 18:09:33 +0000

With locale = :fr
=> 30 o 2012 18:09:33

Where do these wrong "formats" come from ?

I'm using Rails 3.2.8 (with Postgresql / gem pg), and everything related to I18n works fine except for dates.

Thanks for any help !

like image 518
Tricote Avatar asked Sep 30 '12 18:09

Tricote


2 Answers

I think I've finally figured this out, sorry for taking so long.

The Rails l helper just calls I18n.localize. If you trace through the I18n.localize code, you'll end up here:

format = format.to_s.gsub(/%[aAbBp]/) 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.day_names",                       :locale => locale, :format => format)[object.wday]
  when '%b' then I18n.t(:"date.abbr_month_names",                :locale => locale, :format => format)[object.mon]
  when '%B' then I18n.t(:"date.month_names",                     :locale => locale, :format => format)[object.mon]
  when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format) if object.respond_to? :hour
  end
end

So the localize helper doesn't use strftime for the "stringy" parts of the date/time, it tries to do it by itself. Add the translations (as arrays in your YAML) for the month and day names as above and your localized dates and times should start working.

If you don't have those translation arrays in your YAML, then I18n.t(:"date.abbr_month_names") will give you strings like this:

"translation missing: en.date.abbr_month_names"

and then I18n.localize will end up doing silly things like this:

"translation missing: en.date.abbr_month_names"[10]

That will use String#[] instead of the expected Array#[] and you end up with random looking single character month and day names.

like image 181
mu is too short Avatar answered Nov 09 '22 06:11

mu is too short


Where do these wrong "formats" come from ?

Because created_at is a DateTime, rails using time formats (not date).

https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml#L195

time:
  am: am
  formats:
    default: ! '%a, %d %b %Y %H:%M:%S %z'
like image 22
melekes Avatar answered Nov 09 '22 06:11

melekes