Rails distance_of_time_in_words返回"en,about_x_hours"

Ste*_* Ou 7 ruby time ruby-on-rails actionview

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

使用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"
Run Code Online (Sandbox Code Playgroud)

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

Bvu*_*Ic7 9

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

# I18n.localize(string)
I18n.l time_ago_in_words(3.minutes.ago)
Run Code Online (Sandbox Code Playgroud)

然后添加(如果不存在)

# 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"
      ....
Run Code Online (Sandbox Code Playgroud)

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

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

请告诉我它是否有效..


Ste*_* Ou 4

所以我终于开始工作了!!希望这可以帮助任何可能遇到同样问题的人......

利希坦伯格最初说的基本上有一半是正确的。en.yml 必须如下所示:

en:
  x_minutes:
    one:   "1 minute"
    other: "%{count} minutes"
Run Code Online (Sandbox Code Playgroud)

请注意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)
Run Code Online (Sandbox Code Playgroud)

而且...瞧!