July 7, 2008

Localizing dates with Gettext in Rails

I came into a problem where I needed to localize Time and Date in my Rails app. I found Samuel Lown's gettext patch which worked really well for my needs. However, my application's primary language is not English, for practical reasons, I translated the strings to the same locale as my app. After putting this into my lib/ directory, I can use these methods to localize Date and Time:
Time.now.strftime('%c')
=> Maa Heinä 7 21:21:52 2008

Time.now.to_localised_formatted_s
=> Maa Heinä 07 21:21:52 EEST 2008

GettextDate::Conversions.monthnames(Time.now.month)
=> Heinäkuu