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

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

I also see all the error messages in German, so the locale is perceived by the server. But when I try to print the date with strftime as follows:

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

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

How to print the date according to the default standard?

+54
ruby ruby-on-rails localization strftime rails-i18n
Jan 04 2018-11-11T00: 00Z
source share
3 answers

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

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

See here for more info, hope this helps.

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

+102
Jan 04 2018-11-11T00:
source share

you can also make it shorter:

 l(some_date, :format => '%d %B %Y') 
+28
Mar 07 '11 at 11:45
source share

In es.yml put:

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

In index.html.erb put:

 <%= l somemodel.datefield %> 
+8
Sep 20 '12 at 23:10
source share



All Articles