Rails month localize helper does not work - ruby-on-rails

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.

Related

How to use locale structure for I18n.localize?

The provided locale structure in the I18n gem can look like the following:
de:
date:
abbr_day_names:
- So
- Mo
- Di
- Mi
- Do
- Fr
- Sa
But trying to output the day as described in the guides doesn't work, it seems like it looks for a format: in the locale aswell?
I18n.locale = :de
l(Date.current, format: :abbr_day_names)
"I18n::MissingTranslationData: translation missing: de.date.formats.abbr_day_names"
This is how you should do (french used):
date:
abbr_day_names: [Dim, Lun, Mar, Mer, Jeu, Ven, Sam]
abbr_month_names: [~, Jan, Fév, Mar, Avr, Mai, Jun, Jul, Août, Sep, Oct, Nov, Déc]
day_names: [Dimanche, Lundi, Mardi, Mercredi, Jeudi, Vendredi, Samedi]
formats:
day_month: "%b %d"
default: "%Y-%m-%d"
hour: "%H:%M"
long: "%A %d %B %Y"
long_month: "%d %B %Y"
month_abbr: "%d %b %Y"
So in date.abbr_day_names you define the abbreviated day names, same for date.abbr_months_names. Then you can set a custom format located in date.formats.name_of_your_format
In your view, you would use it this way:
l(Date.current, format: :long)
# OR
l(Date.current, format: :month_abbr)
# etc.
It works the same with datetime.formats and time.formats.
Here is an example of a common en-US.yml file for date/time formats: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en-US.yml
I can't find the full documentation about every wildcards usable in the i18n localization system. If somebody knows where to get it, your input will be greatly appreciated!

rails localization of date format gives unexpected results [duplicate]

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

Localize month names - Calendar Railscasts #213

i'm try tutorial calendar from railscasts episode #213.
i have add es.yml but not worked.
i try localize month names with replace word on en.yml such as
en:
date:
month_names: [~, Enero, Febrero, Marzo, Abril, Mayo, Junio, Julio, Agosto, Septiembre, Octubre, Noviembre, Diciembre]
abbr_month_names: [~, Ene, Feb, Mar, Abr, May, Jun, Jul, Ago, Sep, Oct, Nov, Dic]
not working too
on html.erb
<h2 id="month"><%= #date.strftime("%B %Y") %></h2>
i want change this
anyone help me?
thank's
You should use the localize method of I18n (shortened as l):
<h2 id="month"><%= l(#date) %></h2>
Then you can set different formats on your own:
http://guides.rubyonrails.org/i18n.html#adding-date-time-formats
# config/locales/es.yml
es:
date:
formats:
short: "%B %Y"
default: "%D %m, %Y"
And use it like this:
<h2 id="month"><%= l(#date, format: :short) %></h2>
just want to clarify that if you use with active record, just simply convert the string datetime value to date object as example below.
en:
date:
formats:
default: "%Y-%m-%d"
short: "%b %d"
long: "%B %d, %Y"
enter code here
<%= l(post.the_created_at.to_date, format: :long) %>

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 !
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

How to handle a `datetime` value so to change it from `2012-04-27 00:00:00` to `27 april 2012`?

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")

Resources