0
votes

I am trying to translate my Rails 3 application, read the primer at http://guides.rubyonrails.org/i18n.html#adding-date-time-formats and subsequently downloaded the corresponding yml file from https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale (de.yml in my case).

In one of my views I had this code (some_model#index) ...

<td><%= time_ago_in_words(some_model.created_at) %></td>

... which I changed in ...

<td><%=l time_ago_in_words(some_model.created_at) %></td>.

Unfortunately this gives me this error:

Object must be a Date, DateTime or Time object. "etwa ein Monat" given.

Any idea why this fails? The created_at column has been created in the database via standard Rails scaffolding (database is mysql using mysql2 gem).

If I strip the time_ago_in_words helper from the code ...

<td><%=l some_model.created_at %></td>.

... the translation works - but the datetime now is of course too long for my <td>.

I also tried to duplicated the distance_in_words section of the de.yml and rename it to time_ago_in_words but this did not work either.

Am I missing something obvious?

1
Errr ... I ... am ... a bit ... confused: After writing the initial post, I ended up reverting the relevant code block to <td><%= time_ago_in_words(some_model.created_at) %></td> again and hoping for an answer here. Without the <%=l the translation suddenly works. o_Otschlein

1 Answers

1
votes

OK, there are 2 different methods in play here :

  1. the l method takes a Date, a Time or a DateTime object and returns a formatted version based on your I18n rules.

  2. the time_ago_words takes the same arguments and uses I18n to spit out a formatted string.

In your example, you're trying to use both! Put simply, all you need is <%= time_ago_in_words(some_model.created_at) %>.