根据当前区域设置在 Rails 中显示小时格式(上午/下午或非上午/下午)

发布于 2024-12-21 10:28:24 字数 885 浏览 3 评论 0原文

我刚刚意识到 Rails(目前为 3.1.3)中的 l(或 localize)方法似乎无法根据当前设置的区域设置来管理时钟格式。我只是假设情况如此,并没有专门对此进行测试...我刚刚验证了日期已根据区域设置重新格式化。

我在 Rails 控制台中获得了以下所有内容的 24 小时时钟。两个区域设置之间唯一明显的区别是月份名称的大写。

I18n.l Time.now, :format => :short,  :locale => :"en"
I18n.l Time.now + 12.hours, :format => :short,  :locale => :"en"
I18n.l Time.now, :format => :short,  :locale => :"sv-SE"
I18n.l Time.now + 12.hours, :format => :short,  :locale => :"sv-SE"

现在,这是为什么呢? 这不是本地化时间的一部分吗?

我发现 AM/PM 唯一的手动干预是生成表格选择,我可以“手动”要求 12 小时制。这是一个奇怪的选择,因为在我看来,这正是我想要依赖语言环境的事情。

看看 Mac OS,我确实可以单独选择格式首选项的语言和区域设置,但这就是为什么我们有 en-US、en-GB、sv-SE 命名,对吗?表示语言-国家。对我来说,通过适合瑞典的格式将包含英语翻译的 en-SE 文件添加到我的应用程序中是完全可以的。

我假设并非所有美国开发人员都会进入并编辑英语的默认区域设置文件(en-US 看起来与 en 顺便说一句相同)以获得适合美国用户的 12 小时时钟。所以,请随意告诉我我有多愚蠢。我完全希望这个问题能够解决一些非常非常基本的问题。

简而言之。我做错了什么?我如何“修复”我的时间戳? :)

I just realized that the l (or localize) method in Rails (3.1.3 at the moment) appears to not manage the clock format base on the currently set locale. I have just assumed this was the case and not tested for this specifically... I have just verified that dates were re-formatted according to the locale.

I get a 24-hour clock for all of the following in my rails console. The only visible difference between the two locales is capitalization of the month name.

I18n.l Time.now, :format => :short,  :locale => :"en"
I18n.l Time.now + 12.hours, :format => :short,  :locale => :"en"
I18n.l Time.now, :format => :short,  :locale => :"sv-SE"
I18n.l Time.now + 12.hours, :format => :short,  :locale => :"sv-SE"

Now, why is that?
Is this not part of localizing a time?

The only manual intervention of AM/PM I find is in generating the selects for forms where I can demand 12-hour clock "manually". An odd choice since it feels to me like just the kind of thing I want to rely on the locale for.

Looking at Mac OS, I do get to choose the language and the locale for format preferences separately, but that is why we have the en-US, en-GB, sv-SE naming, right? Signifying language-COUNTRY. It would be perfectly OK for me to add a en-SE file to my app containing English translations by formatting suitable in Sweden.

I assume not all US-developers go in and edit the defaults locale file for English (en-US looks the same as en btw) to get a 12-hour clock suitable for US users. So, feel free to show me how dumb I am. I fully expect this problem to be sure to something really really basic.

In short. What I am getting wrong? And how do I "fix" my timestamps? :)

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

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

发布评论

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

评论(2

单调的奢华 2024-12-28 10:28:25

当然,您可以根据当前区域设置显示小时格式(上午/下午或不),例如,如果您在不同的语言/区域设置文件中定义不同的时间格式。请记住,日期和时间格式之间存在差异,如果您使用 Time.now,则使用时间格式(如果您使用日期值或类型转换 to_date,则使用使用日期格式)。瑞典语文件 sv.yml 看起来像

sv:
  time:
    formats:
      default: "%H:%M"

英语语言环境文件 en.yml 看起来像

en:
  time:
    formats:
      default: '%I.%m %p'

然后

> I18n.l Time.now, :format => :default, :locale => :en
=> "06.04 PM"
> I18n.l Time.now, :format => :default, :locale => :sv
=> "18:04"

Of course you can display the hour-format (am/pm or not) depending on the current locale, for example if you define it differently in different language/locale files. Remember there is a difference between date and time formats, if you use Time.now then the time format is used (if you use a Date value or typecast to_date, then the date format is used). The Swedish file sv.yml would look like

sv:
  time:
    formats:
      default: "%H:%M"

The english locale file en.yml would look like

en:
  time:
    formats:
      default: '%I.%m %p'

Then

> I18n.l Time.now, :format => :default, :locale => :en
=> "06.04 PM"
> I18n.l Time.now, :format => :default, :locale => :sv
=> "18:04"
家住魔仙堡 2024-12-28 10:28:25

如果直接使用 %I.%m %p 指定,您可以获得 12 小时制时钟:%p 表示指示器(“AM”或“PM”),%I 表示一天中的小时对于 12 小时制 (01..12)

>> I18n.l Time.now, :format => "%I.%m %p",  :locale => :"en"
=> "01.12 pm"
>> I18n.l Time.now + 12.hours, :format => "%I.%m %p",  :locale => :"en"
=> "01.12 am"

这适用于 Rails 3.1

You can get a 12-hour clock if you specify it directly with %I.%m %p: %p means indicator (“AM” or “PM”), %I means hour of the day for a 12-hour clock (01..12)

>> I18n.l Time.now, :format => "%I.%m %p",  :locale => :"en"
=> "01.12 pm"
>> I18n.l Time.now + 12.hours, :format => "%I.%m %p",  :locale => :"en"
=> "01.12 am"

This works in Rails 3.1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文