Rails translate time ago with time_ago_in_words
and documentation is this.
time_ago_in_words(3.minutes.from_now) # => 3 minutes
However, in our app, we use "ago": 3 minutes ago
So, what is the best way to translate when "ago" appears before the time_ago
, such as in French:
Il y a 3 minutes
Is this built into Rails?
Using Rails 4.2.10 and rails-i18n gem which provides the distance in time, but not the "ago".
I think you just need to pass the time as a variable to the translation.
Then in the language yml
s, you can have the rest of the string where it needs to be.
Suppose that @product.purchased_time_ago
returns the time_ago_in_words()
.
# app/views/products/show.html.erb
<%= t('time_ago', time: @product.purchased_time_ago) %>
# config/locales/en.yml
en:
time_ago: "%{time} ago"
# config/locales/fr.yml
fr:
time_ago: "Il y a %{time}"
This is direct taken from the docs.