Rails I18n语言环境在Rails中设置日期格式

时间:2011-06-05 15:48:33

标签: date internationalization locale

我的环境:rails => 3.0.6,ruby => 1.9.2 我将我的地方设置为Italan。事实上,在控制台内部

  

I18n.locale#=> :它

我的语言环境文件工作正常,但我不能让我的日期显示正确。对于前者在我的控制台中

  

Date.current => 2011年6月5日,星期日

而不是

  

05 Giugno 2011

但是如果我尝试其他方法,它会返回正确的翻译输出

  

helper.number_to_currency(30)#=> “30.00€”

区域设置问题仅发生在日期中。为什么呢?

2 个答案:

答案 0 :(得分:4)

Date.current => Sun, 05 Jun 2011

不会通过localizer运行您的代码,您应该使用

I18n.localize(Date.current)
I18n.l(Date.current)

Rails中还有一些辅助方法,它们会尊重语言环境,但只能(通常)在视图中可用,这些生活的文档在这里:http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html

以下是Rails 3.0.7应用程序中IRB会话的简短摘录(我没有其他可用的语言环境)

ruby-1.9.2-p180 :001 > Date.current
 => Sun, 05 Jun 2011 
ruby-1.9.2-p180 :002 > I18n.locale
 => :en 
ruby-1.9.2-p180 :003 > I18n.l(Date.current)
 => "2011-06-05" 
ruby-1.9.2-p180 :004 > I18n.locale = :ru
 => :ru 
ruby-1.9.2-p180 :005 > I18n.l(Date.current)
 => I18n::MissingTranslationData: translation missing: ru.date.formats.default

答案 1 :(得分:2)

尝试

I18n.localize(Date.today)

或在视图中

l(Date.today)

来源: http://guides.rubyonrails.org/i18n.html#adding-date-time-formats

相关问题