Strange I18n date output with rails

2020-02-13 14:04发布

问题:

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 !

回答1:

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.



回答2:

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'