Update: month names list has nil at the beginning
Based on Localization for Ruby’s Time#strftime, I’ve come across a simple solution when you are developing a non-multilanguage app in a non-English language.
If it’s for rails, we put this on lib/environment.rb. In any other cases, put this code at the initialisation stage
Date::MONTHNAMES.replace [nil] + %w(Enero Febrero Marzo Abril Mayo Junio Julio Agosto Septiembre Octubre Noviembre Diciembre)
Date::DAYNAMES.replace = %w(Domingo Lunes Martes Miércoles Jueves Viernes Sábado)
Date::ABBR_MONTHNAMES.replace [nil] + %w(Ene Feb Mar Abr May Jun Jul Ago Sep Oct Nov Dic)
Date::ABBR_DAYNAMES.replace %w(Dom Lun Mar Mié Jue Vie Sab)
Of course, you should translate it to your language if it’s not Spanish.
The result:
>> Date.today.strftime("%A, %d de %B de %Y")
=> "Miércoles, 17 de Febrero de 2007"
I had a problem with this code “can’t modify frozen array (TypeError)” cant get it to work
This won’t work with Date::MONTHNAMES Array beeing frozen since Ruby 1.8.6
for freezing constant you can redefine them (with a warning) with monkeypatching:
or with const_set:
In moderns rails (2.2+ I think) you can use the I18n API :
I18n::localize(Date.today, :format => “%B %Y”)
Yep, now it’s much better, but back then when I wrote this post, rails internationalization was a complete mess
it’s still a mess !!
can’t find a way to get the translated day of the week !
and if i get it from DATE::DAYNAMES , i get the english version.. that’s plain stupid
I have the same problem! My calendar uses Date::DAYNAMES to get the days of the week. And now I can’t translate them! How to do?
I found this, it may help somebody else as well.
I18n.t ‘date.abbr_month_names’