【问题标题】:Rails I18n locale set date format in RailsRails I18n 语言环境在 Rails 中设置日期格式
【发布时间】:2011-06-05 15:48:33
【问题描述】:

我的环境:rails => 3.0.6,ruby => 1.9.2 我将语言环境设置为意大利语。事实上,在控制台内部

I18n.locale # => :it

我的语言环境文件工作正常,但我无法正确显示我的日期。例如。在我的控制台中

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

而不是

05 朱尼奥 2011

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

helper.number_to_currency(30) # => "30.00 €"

语言环境问题仅发生在日期上。为什么?

【问题讨论】:

    标签: date internationalization locale


    【解决方案1】:
    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
    

    【讨论】:

      【解决方案2】:

      试试

      I18n.localize(Date.today)
      

      或者只是在视图中

      l(Date.today)
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-05
        • 1970-01-01
        • 2012-01-18
        • 2012-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多