Translating dates in ruby (on rails)
Posted by Jorge Bernal January 17, 2007
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