Rails distance_of_time_in_words 返回“en,about_x_hours"; [英] Rails distance_of_time_in_words returns "en, about_x_hours"
问题描述
我遇到了一个奇怪的问题,希望有人知道是什么问题...
I'm having a weird problem, hoping someone knows what the issue is...
使用 distance_of_time_in_words(以及因此 time_ago_in_words)不会返回实际时间距离.相反,它返回诸如en, about_x_hours"或en, x_minutes"之类的内容.
Using distance_of_time_in_words (and consequently time_ago_in_words) is not returning the actual time distance. Instead it is returning things like "en, about_x_hours" or "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"?!
But why on earth is it displaying "x" instead of the actual number, "_" instead of spaces, and "en, " at the beginning of all of that?!
推荐答案
所以我终于让它工作了!!希望这可以帮助任何可能遇到同样问题的人...
So I finally got it to work!! Hope this helps anyone who might have the same problem...
Lichtamberg 首先说的基本上有一半是正确的.en.yml 必须如下所示:
Basically half of what Lichtamberg first said was correct. The en.yml had to be as follows:
en:
x_minutes:
one: "1 minute"
other: "%{count} minutes"
请注意,x_minutes
不在 datetime
之下,并且它有 one
和 other
.此外,不需要 I18n.l
,因为 I18n 已经在 distance_of_time_in_words
方法中实现.
Note that x_minutes
is not under datetime
and that it has one
and other
. Further, there is no need for I18n.l
as I18n is already implemented in the distance_of_time_in_words
method.
因此,对于上述(以及您可以在包含的 Lichtamberg 文件中找到的所有其他 about_x_hours、x_days 等模式),只需执行以下操作:
So with the above (plus all the other about_x_hours, x_days, etc patterns that you can find on the file Lichtamberg included), just do:
time_ago_in_words(3.minutes.ago)
还有……瞧!
这篇关于Rails distance_of_time_in_words 返回“en,about_x_hours";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!