Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How to make Date strftime aware of the default locale?

I have my default locale set in the environment.rb as de (German).

I also see all the error messages in German, so the locale is picked up by the server. But when I try to print date with strftime like following:

some_date.strftime('%B, %y')

It prints in English (January, 11), and not the expected German (Januar, 11).

How can I print the date according to the default locale?

like image 306
rangalo Avatar asked Jan 04 '11 09:01

rangalo


3 Answers

Use the l (alias for localize) method instead of raw strftime, like this:

l(date, format: '%B %d, in the year %Y')

See here for more information, hope that helps.

You can also define 'named' formats, a couple of them (short, long) are already predefined.

like image 90
Milan Novota Avatar answered Nov 03 '22 05:11

Milan Novota


you can also make it shorter:

l(some_date, :format => '%d %B %Y')
like image 30
kstarski Avatar answered Nov 03 '22 05:11

kstarski


In es.yml put:

es:
  date:
    formats:
      default: "%d / %m / %Y"

In index.html.erb put:

<%= l somemodel.datefield %>
like image 10
Daniel Avatar answered Nov 03 '22 05:11

Daniel