带导轨的奇怪 I18n 日期输出 [英] Strange I18n date output with rails

查看:10
本文介绍了带导轨的奇怪 I18n 日期输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Ruby On Rails 3 应用程序中的日期翻译有一个奇怪的问题,我真的不明白为什么......

I've got a strange problem with date translations in my Ruby On Rails 3 application, and I really don't understand why...

这是我的 en.ymlfr.yml :

fr:
  date:
    formats:
      default: "%d/%m/%Y"
      short: "%e %b"
      long: "%e %B %Y" 
  time:
    formats:
      default: "%d %B %Y %H:%M:%S"
      short: "%d %b %H:%M"
      long: "%A %d %B %Y %H:%M"
    am: 'am'
    pm: 'pm'



en:
  date:
    formats:
      default: "%Y-%m-%d"
      long: "%B %d, %Y"
      short: "%b %d"
  time:
    am: am
    formats:
      default: ! '%a, %d %b %Y %H:%M:%S %z'
      long: ! '%B %d, %Y %H:%M'
      short: ! '%d %b %H:%M'
    pm: pm

这不是特定于特定视图,而是例如在我的一个视图中:

This is not specific to a particuliar view, but for instance in one of my view :

<td><%=l job_application.created_at, :format => :default %></td>

我得到了那些奇怪的输出:

I get those strange outputs :

With locale = :en
=> t, 30 o 2012 18:09:33 +0000

With locale = :fr
=> 30 o 2012 18:09:33

这些错误的格式"从何而来?

Where do these wrong "formats" come from ?

我使用的是 Rails 3.2.8(使用 Postgresql/gem pg),与 I18n 相关的所有内容都可以正常工作,日期除外.

I'm using Rails 3.2.8 (with Postgresql / gem pg), and everything related to I18n works fine except for dates.

感谢您的帮助!

推荐答案

我想我终于想通了,抱歉拖了这么久.

I think I've finally figured this out, sorry for taking so long.

Rails l 助手只调用 I18n.localize.如果您跟踪 I18n.localize 代码,您最终会得到 这里:

The Rails l helper just calls I18n.localize. If you trace through the I18n.localize code, you'll end up here:

format = format.to_s.gsub(/%[aAbBp]/) do |match|
  case match
  when '%a' then I18n.t(:"date.abbr_day_names",                  :locale => locale, :format => format)[object.wday]
  when '%A' then I18n.t(:"date.day_names",                       :locale => locale, :format => format)[object.wday]
  when '%b' then I18n.t(:"date.abbr_month_names",                :locale => locale, :format => format)[object.mon]
  when '%B' then I18n.t(:"date.month_names",                     :locale => locale, :format => format)[object.mon]
  when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format) if object.respond_to? :hour
  end
end

所以 localize 帮助器不会将 strftime 用于日期/时间的字符串"部分,它会尝试自行完成.为上述月份和日期名称添加翻译(作为 YAML 中的数组),您的本地化日期和时间应该开始工作了.

So the localize helper doesn't use strftime for the "stringy" parts of the date/time, it tries to do it by itself. Add the translations (as arrays in your YAML) for the month and day names as above and your localized dates and times should start working.

如果您的 YAML 中没有这些翻译数组,则 I18n.t(:"date.abbr_month_names") 将为您提供如下字符串:

If you don't have those translation arrays in your YAML, then I18n.t(:"date.abbr_month_names") will give you strings like this:

"translation missing: en.date.abbr_month_names"

然后 I18n.localize 最终会做这样的傻事:

and then I18n.localize will end up doing silly things like this:

"translation missing: en.date.abbr_month_names"[10]

这将使用 String#[] 而不是预期的 Array#[] 最终会得到随机的单字符月份和日期名称.

That will use String#[] instead of the expected Array#[] and you end up with random looking single character month and day names.

这篇关于带导轨的奇怪 I18n 日期输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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