Rails没有选择自定义的日期和日期时间格式在en.yml中 [英] Rails not picking up custom date & time formats in en.yml

查看:418
本文介绍了Rails没有选择自定义的日期和日期时间格式在en.yml中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Rails中的I18N不是很熟悉,所以请耐心等待。试图设置一个自定义日期&时间格式:
$ b $ pre $ code#config / locales / en.yml
en
日期
格式:
long_dateweek:%A,%B%d,%Y
时间:
格式:
very_short:%H:%M

但是,当我尝试使用它们时,我只是得到了默认格式:

  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

尝试重新启动控制台(甚至服务器)无济于事... 我错过了什么? 您需要使用 I18n.l 方法如下:

  1.9.3p194:001 > I18n.l Time.now.to_date,:format => :long_dateweek 
=> 2012年8月22日(星期三)
1.9.3p194:002> I18n.l Time.now,:format => :very_short
=> 23:03

您也可以使用 l 辅助方法在你的意见。请参阅导轨指南以获取更多信息。


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?

解决方案

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.

这篇关于Rails没有选择自定义的日期和日期时间格式在en.yml中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆