Fork me on GitHub

Use I18n.localize for date/time formating

06 Aug 2010

Ng Tze Yang (ngty77@gmail.com)

On different machines, Time#strftime can yield different results, and there is no reliable way to ensure u get the string with the desired language, even after setting the environment variable $LANG, and even recompiling ruby under the desired locale setting. Use I18n.localize instead:

I18n.localize(Time.now) # using :default format
I18n.localize(Time.now, :format => :short) # using short format

I18n.localize(Date.today) # using :default format
I18n.localize(Date.today, :format => :short) # using short format

In view layer, u can replace the verbose I18n.localize(...) with just shorthand l(...).

All u need is to have the appropriate yml data file under config/locales, the resources at http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale/ provides a good starting point.

Tags