Rails distance_of_time_in_words returns “en, about_x_hours”

拟墨画扇 提交于 2019-12-03 00:28:06

That gives back a string to translate via the I18n-gem.... Try following:

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

And add (if doesnt exist) following in

# 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"
      ....

And be sure to include following (maybe customized) data in your en.yml:

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

Please tell me if it works..

So I finally got it to work!! Hope this helps anyone who might have the same problem...

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"

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.

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)

And... voila!

Ben Walding

Shamelessly ripped off time_ago_in_words => "in {{count}} days."?

Rails was using some deprecated syntax in the helper which then got dropped in the latest Ruby version. If you are using something like Heroku, try telling your production instance to use Rails 2.3.9. Otherwise you can also try downgrading Ruby.

See the changelog: 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}.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!