Strange I18n date output with rails
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.