Strange I18n date output with rails
Asked Answered
L

3

10

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 !

Lethia answered 30/9, 2012 at 18:57 Comment(5)
Yes I have too... (post updated)Lethia
The output matches the format strings for times except that the string parts are getting truncated to one character? Any idea where the o comes from? What should the formatted versions be?Harpp
Well not exactly, the single character is not always the first letter of the month or the day name. For instance : Mon, 01 Oct 2012 17:11:55 UTC +00:00 will be translated to : "01 n 2012 17:11:55"Lethia
What happens if you manually use those formats with strftime?Harpp
It works fine with strftime...Lethia
H
13

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.

Harpp answered 7/10, 2012 at 2:50 Comment(2)
I found the problem thanks to your answer ! Actualy, I had all the "date.abbr_day_names" / "date.abbr_month_names" / ... keys in my translation files, but I had overridden the "date" key to translate the "date" word (global key without namespace) ! Not very clever I know... Thanks for your time !Lethia
"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." What? Where? I'm sorry I don't get this, could someone provide an example please?Alo
P
1

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'
Pore answered 1/10, 2012 at 7:6 Comment(0)
C
0

Type in your console

I18n.t(:"date") 

to check if you are getting the translations you defined in your translation .yml file.

Compare the structure with the standard EN locale

I18n.t(:"date", locale:'en')

That made me notice I was declaring the date: attribute twice in my .yml, and the first part was being overwritten by the second declaration.

You should get the abbr_month_names that you declared when calling

I18n.t(:"date.abbr_month_names")

These are the ones that will be used when calling %b.

If not, check your locale .yml file to make sure they are properly declared, and not being declared twice.

You may also call I18n.locale to check if the .yml file you are editing is the one being used by rails

Contiguous answered 22/5, 2020 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.