views:

39

answers:

2

I'm having a weird problem, hoping someone knows what the issue is...

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

The pattern is correct, as in:

time_ago_in_words(50.minutes.ago) => "en, about_x_hours"
time_ago_in_words(3.minutes.ago) => "en, x_minutes"

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?!

A: 

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

Lichtamberg
The above gives me the following error: Object must be a Date, DateTime or Time object. "about x minutes" given.
Steven Ou
Updated my comment.. Hope it works now..
Lichtamberg
hmmmmm i tried the updated code but i still get: Object must be a Date, DateTime or Time object. "<span class=\"translation_missing\">en, x_minutes</span>" given. :(
Steven Ou
so i finally figured it out. thanks for your help! i couldn't have done it without you! though i'm still confused as to why it didn't just work... no one else seemed to have a problem...
Steven Ou
A: 

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!

Steven Ou