2010-10-24 46 views
6

我有一个奇怪的问题,希望有人知道是什么问题...Rails的distance_of_time_in_words返回“恩,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" 

但为什么在地球上是显示“×”,而不是实际的数字,“_”代替空格,而“恩,”开头所有这些?!

+0

怪......我试图在两条轨道3.0.1和2.2.2和她们俩给“约1小时”。 – 2010-10-24 04:25:14

+0

绝对奇怪...我跑2.3.8 btw ...如果它有所作为。我没有受过教育的猜测会是它与国际化有关吗?我猜“en”表示它应该是英文吗?不知道......疯狂的猜测。 – 2010-10-24 04:40:42

回答

2

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

Lichtamberg第一次说的基本上一半是正确的。该en.yml不得不如下:

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

注意x_minutesdatetime下,它具有oneother。此外,因为已经在distance_of_time_in_words方法中实施了I18n,所以不需要I18n.l

因此,与上述(加上所有其他about_x_hours,x_days,等等,你可以找到关于Lichtamberg包含的文件模式),只是做:

time_ago_in_words(3.minutes.ago) 

而且......瞧!

+1

,使用'helper.time_ago_in_words(3.minutes.ago)' – Magne 2015-02-05 11:56:07

8

这还给一个字符串通过的I18n-宝石翻译....尝试以下操作:

# 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

请告诉我,如果它的工作..

+0

以上给出了以下错误:对象必须是Date,DateTime或Time对象。 “约x分钟”给出。 – 2010-10-24 23:09:46

+0

更新我的评论..希望它的作品现在.. – Lichtamberg 2010-10-25 13:48:00

+0

hmmmmm我试过更新的代码,但我仍然得到:对象必须是Date,DateTime或Time对象。 “ en,x_minutes”given。 :( – 2010-10-28 02:27:48