3

I wanted to display datetime in words, like "1 hour ago" and i tried the following method but it is returning returning html in the string format as shown below.

method

helper.time_ago_in_words(Time.now)

result

=> "<span class=\"translation_missing\" title=\"translation missing: en.less_than_x_minutes\">Less Than X Minutes</span>"

rails console output:

>> helper.time_ago_in_words(Time.now)
=> "<span class=\"translation_missing\" title=\"translation missing: en.less_than_x_minutes\">Less Than X Minutes</span>"
>> I18n.locale = :de
=> :de
>> helper.time_ago_in_words(Time.now)
=> "<span class=\"translation_missing\" title=\"translation missing: de.less_than_x_minutes\">Less Than X Minutes</span>"
>> I18n.locale = :en
=> :en
>> helper.time_ago_in_words(Time.now)
=> "<span class=\"translation_missing\" title=\"translation missing: en.less_than_x_minutes\">Less Than X Minutes</span>"
Community
  • 1
  • 1
Rahul Chandra
  • 483
  • 7
  • 15
  • 1
    Your right the docs act like it should return a string... http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_ago_in_words. I don't know a solution but thanks for asking this so I, and others, can keep an eye out for this later. – rovermicrover Apr 13 '13 at 22:23

3 Answers3

1

Do you use any locales in config? English is for sure the default one and it should work correctly.

2.0.0p0 :001 > helper.time_ago_in_words(Time.now)
 => "less than a minute" 
2.0.0p0 :002 > I18n.locale = :de
 => :de 
2.0.0p0 :003 > helper.time_ago_in_words(Time.now)
 => "translation missing: de.datetime.distance_in_words.less_than_x_minutes" 
2.0.0p0 :004 > I18n.locale = :en
 => :en 
2.0.0p0 :005 > helper.time_ago_in_words(Time.now)
 => "less than a minute" 

Could you try to invoke this method on helper object, like I did in console (rails c)? What is the output?

konole
  • 766
  • 4
  • 8
  • thanks for the reply, i have updated the output in the above question please check. – Rahul Chandra Apr 15 '13 at 20:36
  • What Rails version do you use? If nothing will work, then the only option for you is to define those translations explicitly, like [here - link](http://stackoverflow.com/questions/4007073/rails-distance-of-time-in-words-returns-en-about-x-hours). You can find file with default translations [here - link](https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml). – konole Apr 16 '13 at 20:40
0

In a rails console, if I just use time_ago_in_words from ActionView::Helpers, I'll get the translation_missing error message. But if I include from ActionView::Helpers::DateHelper, it'll work as expected.

works:

[2] pry(main)> include ActionView::Helpers::DateHelper
=> Object 
[3] pry(main)> time_ago_in_words(Time.now)
=> "less than a minute"

translation missing:

[1] pry(main)> include ActionView::Helpers
=> Object [2] 
pry(main)> time_ago_in_words(Time.now)
=> "<span class=\"translation_missing\" title=\"translation missing: en.less_than_x_minutes\">Less Than X Minutes</span>"
Bruce Chu
  • 216
  • 3
  • 5
0

Try this:

time_ago_in_words(Time.now)

Vitalii
  • 1,137
  • 14
  • 25