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 !
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.
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'
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
Related
I want to make the translation into Russian of months.
config/locales/ru.yml
ru:
date:
formats:
default: "%d-%m-%Y"
short: "%b %d"
long: "%B %d, %Y"
day_names: [Воскресенье, Понедельник, Вторник, Среда, Четверг, Пятница, Суббота]
abbr_day_names: [Вос, Пон, Вт, Ср, Чет, Пят, Суб]
month_names: [~, Январь, Февраль, Март, Апрель, Маи, Июнь, Июль, Август, Сентябрь, Октябрь, Ноябрь, Декабрь]
abbr_month_names: [~, Янв, Фев, Мар, Апр, Маи, Июн, Июл, Авг, Сен, Окт, Ноя, Дек]
time:
formats:
default: "%d-%m-%Y %H:%M:%S"
short: "%d %B %H:%M"
long: "%B %d, %Y %H:%M"
In the view:
time now: <%= l Time.now, :format => :short %>
Return:
time now: 31 l 13:13
Current locale is "ru".
<%= I18n.locale %>
Return:
ru
Also checked the localization of days by '%a', does not work either. Why localization of months and days of not working?
Found duplicate field date in the file location. Removed, the problem persists.
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 !
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.
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'
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
I am using Ruby on Rails v3.2.2 and I would like to display a data as-like <day_number> <month_name> <year_number>. That is, I have a datetime database table column and I would like to change its contained values, for example, from 2012-04-27 00:00:00 to 27 april 2012.
How can I make that?
The standard way is use rails localization.
model
I18n.l(datetime, :format => :short)
controller
I18n.l(datetime, :format => :long)
view
<%= l(datetime), :format => :custom %>
config/locales/en.yml
en:
time:
formats:
long: "%A %B %d %Y | %I:%M %p GMT"
short: "%d %B %Y, %H:%M GMT"
custom: "your custom format"
date:
formats:
short: "%d %B %Y"
long: "%A %B %d %Y"
custom: "your custom format"
In your case the format should be "%d %A %Y".
The benefit is if you want to change format you can do it in one place for all datetimes which you are using.
You can use the method
str_name.strftime("%d %A %Y")
I'm having an issue which I can't seem to figure out. I'm trying to format a date using a custom format I've defined in my en.yml file:
en:
hello: "Hello world"
time:
formats:
history_table: "%m/%d/%Y %I:%M:%S %p %Z"
This is being called using the 'l' helper:
l version.created_at, :format => :history_table
For some reason this is displaying the AM/PM in lowercase, instead of in uppercase as should be the case with %p.
I've played around in the console a bit, and it seems like it's a difference in behavior between the localization function and strftime:
ruby-1.9.2-p180 :043 > I18n.l user.updated_at, :format => "%m/%d/%Y %I:%M:%S %p %Z"
=> "03/23/2011 01:52:16 am UTC"
ruby-1.9.2-p180 :044 > user.updated_at.strftime("%m/%d/%Y %I:%M:%S %p %Z")
=> "03/23/2011 01:52:16 AM UTC"
Am I doing something wrong? Is this a bug? Any guidance is greatly appreciated as my forehead is sore from banging it against the wall.
Edit:
This has been solved(ish).
Looking at the default activesupport localization, there isn't any differentiation between %p and %P.
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml
I over-rode the localization in my local en.yml file to use uppercase. I would really have liked to see Rails support both options however.
Looking in the source for active support, I found the following under english localization:
time:
formats:
default: "%a, %d %b %Y %H:%M:%S %z"
short: "%d %b %H:%M"
long: "%B %d, %Y %H:%M"
am: "am"
pm: "pm"
In other words, there is no distinction between %P and %p built-in to localization as there is in strftime. This unfortunately means that in individual custom formats it is not possible to choose between upper and lower case representations, but it is possible to define which you would like globally by over-riding the default formats in your own en.yml file. For example, here's my updated en.yml file that now causes the output of upper-case AM/PM.
en:
hello: "Hello world"
time:
formats:
history_table: "%m/%d/%Y %I:%M:%S %p %Z"
am: "AM"
pm: "PM"
I have my default locale set in the environment.rb as de (German).
I also see all the error messages in German, so the locale is picked up by the server. But when I try to print date with strftime like following:
some_date.strftime('%B, %y')
It prints in English (January, 11), and not the expected German (Januar, 11).
How can I print the date according to the default locale?
Use the l (alias for localize) method instead of raw strftime, like this:
l(date, format: '%B %d, in the year %Y')
See here for more information.
You can also define 'named' formats, a couple of them (short, long) are already predefined.
you can also make it shorter:
l(some_date, :format => '%d %B %Y')
In es.yml put:
es:
date:
formats:
default: "%d / %m / %Y"
In index.html.erb put:
<%= l somemodel.datefield %>