7

我遇到了一个奇怪的问题,希望有人知道问题是什么...

使用 distance_of_time_in_words(以及因此 time_ago_in_words)不会返回实际的时间距离。相反,它返回诸如“en,about_x_hours”或“en,x_minutes”之类的内容。

模式是正确的,如下所示:

time_ago_in_words(50.minutes.ago) => "en, about_x_hours"
time_ago_in_words(3.minutes.ago) => "en, x_minutes"

但是为什么它在所有这些的开头显示“x”而不是实际数字,“_”而不是空格,以及“en,”?!

4

3 回答 3

10

这会返回一个字符串以通过 I18n-gem 进行翻译......尝试以下操作:

# I18n.localize(string)
I18n.l time_ago_in_words(3.minutes.ago)

并添加(如果不存在)以下

# config/locales/en.yml
en:
  datetime:
    distance_in_words:
      ...
      less_than_x_minutes:
        one: "less than a minute"
        other: "less than %{count} minutes"
      x_minutes:
        one: "1 minute"
        other: "%{count} minutes"
      about_x_hours:
        one: "about 1 hour"
        other: "about %{count} hours"
      ....

并确保在您的 en.yml 中包含以下(可能是自定义的)数据:

http://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml

请告诉我它是否有效..

于 2010-10-24T11:36:46.473 回答
3

所以我终于让它工作了!!希望这可以帮助任何可能有同样问题的人......

Lichtamberg 最初所说的基本上有一半是正确的。en.yml 必须如下:

en:
  x_minutes:
    one:   "1 minute"
    other: "%{count} minutes"

请注意x_minutes不是 underdatetime并且它有oneother。此外,不需要,I18n.l因为 I18n 已经在该distance_of_time_in_words方法中实现。

因此,使用上述内容(以及您可以在 Lichtamberg 包含的文件中找到的所有其他 about_x_hours、x_days 等模式),只需执行以下操作:

time_ago_in_words(3.minutes.ago)

还有……瞧!

于 2010-10-28T06:55:56.927 回答
0

无耻地扯掉time_ago_in_words => “在 {{count}} 天内。”?

Rails 在帮助程序中使用了一些不推荐使用的语法,然后在最新的 Ruby 版本中被删除。如果您使用的是 Heroku 之类的东西,请尝试告诉您的生产实例使用 Rails 2.3.9。否则,您也可以尝试降级 Ruby。

查看更新日志:http ://weblog.rubyonrails.org/2010/9/4/ruby-on-rails-2-3-9-released

Changes i18n named-interpolation syntax from the deprecated Hello {{name}} to the 1.9-native Hello %{name}.
于 2011-01-15T11:35:14.547 回答