2011-06-05 101 views
1

我的環境:rails => 3.0.6,ruby => 1.9.2 我將我的語言環境設置爲Italan。逸岸,控制檯Rails I18n語言環境在Rails中設置日期格式

I18n.locale#=>裏:它

我的區域文件工作得很好,但I'cant讓我的日期顯示正確。例如。在我的控制檯

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

,而不是

05朱尼奧2011

但是,如果我嘗試其他方法,它的回報右譯文輸出

helper.number_to_currency(30)#=>「30.00€」

語言環境問題僅在日期時發生。爲什麼?

回答

4
Date.current => Sun, 05 Jun 2011 

不會通過localizer運行你的代碼,你應該使用

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

也有在Rails的helper方法,這會尊重語言環境,但只有(典型值)可用視圖,文檔,這些生命在這裏: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