Rails distance_of_time_in_words返回“en,about_x_hours”
我有一个奇怪的问题,希望有人知道问题是什么……
使用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”,在所有这些的开头?!
这会通过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
请告诉我它是否有效..
所以我终于开始工作了!! 希望这有助于任何可能遇到同样问题的人……
基本上Lichtamberg首先说的一半是正确的。 en.yml必须如下:
en: x_minutes: one: "1 minute" other: "%{count} minutes"
请注意, x_minutes
不在datetime
之下,并且它有one
和other
。 此外,不需要I18n.l
因为I18n已经在distance_of_time_in_words
方法中实现。
因此,使用上面的内容(加上所有其他about_x_hours,x_days等模式,您可以在Lichtamberg文件中找到),只需执行以下操作:
time_ago_in_words(3.minutes.ago)
而且……瞧!
无耻地扯掉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}.