Rails not picking up custom date & time formats in

2019-05-10 07:34发布

I'm not that familiar with I18N in Rails so bear with me. Trying to set a custom date & time format:

#config/locales/en.yml
en:
  date:
    formats:
      long_dateweek: "%A, %B %d, %Y"
  time:
    formats:
      very_short: "%H:%M"

But when I try to use them I just get the default formats:

1.9.3p194 :001 > Time.now.to_date.to_s(:long_dateweek)
 => "2012-08-22" 
1.9.3p194 :002 > Time.now.to_time.to_s(:very_short)
 => "2012-08-22 16:12:47 -0700" 

Tried restarting console (and even the server) to no avail... What'd I miss?

2条回答
Fickle 薄情
2楼-- · 2019-05-10 08:07

@alexsanford1 has the right answer. I modified mine below to work in the view.

<%= l Time.now, :format => :very_short %>
查看更多
Rolldiameter
3楼-- · 2019-05-10 08:11

You need to use the I18n.l method as follows:

1.9.3p194 :001 > I18n.l Time.now.to_date, :format => :long_dateweek
 => "Wednesday, August 22, 2012" 
1.9.3p194 :002 > I18n.l Time.now, :format => :very_short
 => "23:03"

You can also use the l helper method in your views. Look at this rails guide for more information.

查看更多
登录 后发表回答