在 Rails 中使用 I18n.l 和 %p 时,AM/PM 不是大写

发布于 2024-10-25 17:40:53 字数 1033 浏览 0 评论 0原文

我有一个我似乎无法弄清楚的问题。我正在尝试使用我在 en.yml 文件中定义的自定义格式来格式化日期:

en:
  hello: "Hello world"
  time:
      formats:
          history_table: "%m/%d/%Y %I:%M:%S %p %Z"

这是使用“l”助手调用的:

l version.created_at, :format => :history_table

出于某种原因,这以小写形式显示 AM/PM,而不是用大写,就像 %p 的情况一样。

我在控制台中玩了一下,似乎本地化函数和 strftime 之间的行为有所不同:

ruby-1.9.2-p180 :043 > I18n.l user.updated_at, :format => "%m/%d/%Y %I:%M:%S %p %Z"
 => "03/23/2011 01:52:16 am UTC" 
ruby-1.9.2-p180 :044 > user.updated_at.strftime("%m/%d/%Y %I:%M:%S %p %Z")
 => "03/23/2011 01:52:16 AM UTC"

我做错了什么吗?这是一个错误吗?非常感谢任何指导,因为我的额头因撞墙而疼痛。

编辑: 这已经解决了(ish)。 查看默认的 activesupport 本地化,%p 和 %P 之间没有任何区别。 https://github.com/rails/rails /blob/master/activesupport/lib/active_support/locale/en.yml

我重写了本地 en.yml 文件中的本地化以使用大写。不过,我真的很希望 Rails 支持这两种选项。

I'm having an issue which I can't seem to figure out. I'm trying to format a date using a custom format I've defined in my en.yml file:

en:
  hello: "Hello world"
  time:
      formats:
          history_table: "%m/%d/%Y %I:%M:%S %p %Z"

This is being called using the 'l' helper:

l version.created_at, :format => :history_table

For some reason this is displaying the AM/PM in lowercase, instead of in uppercase as should be the case with %p.

I've played around in the console a bit, and it seems like it's a difference in behavior between the localization function and strftime:

ruby-1.9.2-p180 :043 > I18n.l user.updated_at, :format => "%m/%d/%Y %I:%M:%S %p %Z"
 => "03/23/2011 01:52:16 am UTC" 
ruby-1.9.2-p180 :044 > user.updated_at.strftime("%m/%d/%Y %I:%M:%S %p %Z")
 => "03/23/2011 01:52:16 AM UTC"

Am I doing something wrong? Is this a bug? Any guidance is greatly appreciated as my forehead is sore from banging it against the wall.

Edit:
This has been solved(ish).
Looking at the default activesupport localization, there isn't any differentiation between %p and %P.
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml

I over-rode the localization in my local en.yml file to use uppercase. I would really have liked to see Rails support both options however.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2024-11-01 17:40:53

在源代码中查找积极支持,我在英语本地化下发现了以下内容:

  time:
    formats:
      default: "%a, %d %b %Y %H:%M:%S %z"
      short: "%d %b %H:%M"
      long: "%B %d, %Y %H:%M"
    am: "am"
    pm: "pm"

换句话说,本地化内置的 %P 和 %p 之间没有区别,就像 strftime 中一样。不幸的是,这意味着在单独的自定义格式中,无法在大写和小写表示之间进行选择,但可以通过覆盖您自己的 en.yml 文件中的默认格式来全局定义您想要的格式。例如,这是我更新的 en.yml 文件,现在它会输出大写的 AM/PM。

en:
  hello: "Hello world"
  time:
      formats:
          history_table: "%m/%d/%Y %I:%M:%S %p %Z"
      am: "AM"
      pm: "PM"

Looking in the source for active support, I found the following under english localization:

  time:
    formats:
      default: "%a, %d %b %Y %H:%M:%S %z"
      short: "%d %b %H:%M"
      long: "%B %d, %Y %H:%M"
    am: "am"
    pm: "pm"

In other words, there is no distinction between %P and %p built-in to localization as there is in strftime. This unfortunately means that in individual custom formats it is not possible to choose between upper and lower case representations, but it is possible to define which you would like globally by over-riding the default formats in your own en.yml file. For example, here's my updated en.yml file that now causes the output of upper-case AM/PM.

en:
  hello: "Hello world"
  time:
      formats:
          history_table: "%m/%d/%Y %I:%M:%S %p %Z"
      am: "AM"
      pm: "PM"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文