开发者

Rails I18n locale set date format in Rails

My environment: rails => 3.0.6, ruby => 1.9.2 I set my locale to Italan. Infact, inside the console

I18n.locale # => :it

My locale file work just fine, but I'cant make my dates display right. For ex. in my console

Date.current => Sun, 05 Jun 2011

instead of

05 Giugno 2011

But if I try other methods it return the right translated output

helper.number_to_开发者_如何学JAVAcurrency(30) # => "30.00 €"

Locale issue occurs only with dates. Why?


Date.current => Sun, 05 Jun 2011

Won't run your code through the localizer, you should use

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

There are also the helper methods in Rails, which will respect the locale, but are only (typically) available in the view, documentation for these lives here: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html

Here's a short excerpt from an IRB session in a Rails 3.0.7 application (I don't have the other locales available)

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


Try

I18n.localize(Date.today)

or in the view just

l(Date.today)

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜