Rails not picking up custom date & time formats in en.yml

半腔热情 提交于 2020-01-02 07:35:56

问题


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?


回答1:


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.




回答2:


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

<%= l Time.now, :format => :very_short %>


来源:https://stackoverflow.com/questions/12082665/rails-not-picking-up-custom-date-time-formats-in-en-yml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!