I18n Relative Time - DateHelpers with Prefixes/Suffixes

Hi all,

I am working on internationalization topics, specifically time localization, and have ran into an issue that could be a possible feature inclusion in a future release that I wanted to get others’ opinions on.

One of the popular formats in English to say a space in time relative to the present time would be to use “10 minutes ago”. Right now, DateHelper allows you to correctly get the text “10 minutes” and then override any of that language by creating a yml file similar to the default en datetime translation. The part that is missing is “ago” - so let’s just add in “ago” into the override file right?

The problem becomes this: If the someone uses the distance_of_time_in_words without one of the “to” or “from” specified as Time.now, it doesn’t make sense anymore.

I want to include “ago” for English but allow that relative time to be translated properly. I didn’t want to include a separate translation key for “ago” because that could result in improper translation without context. In addition, other languages have prefixes instead of suffixes when specifying relative time.

My proposal is to add some additional options for the distance_of_time_in_words method in DateHelper

  1. :include_prefix (default: false)

  2. :include_suffix (default: false)

In the default yml file for datetime distance, there would be additional keys - :prefix and :suffix

For English, suffix would be “ago” and prefix would be “” - this would allow users to customize not only the language used for the distance of time in words, but also any prefix or suffix used for the language.

Now, if I want my “10 minutes ago”, instead of specifying

<%= time_ago_in_words(Time.now) %> ago

we can now specify

<%= time_ago_in_words(Time.now, :include_prefix => true) %>

Thoughts?

-Andrew

Wouldn’t that work with a translation of your own, that receives the “time ago” as an argument that you can interpolate, like this:

translate :time_ago, time: time_ago_in_words(Time.current)

# locale

en:

time_ago: “%{time} ago”

pt-BR:

time_ago: “%{time} atrás”

zomg:

time_ago: “prefix %{time} suffix”

That “just works”, without adding any new extension to the method/framework.

Hope that helps.

Yes that works and that is what I am doing now, but it feels like it could follow the same conventions as the time_ago jquery plugin that was based of the rails verbiage (while extending it with the suggestion I made above): https://github.com/rmm5t/jquery-timeago

It is good that we can work around it without too much extra code, but it seems to force extensions that naturally should flow in with the rest of the phrases being constructed.

For example, the Spanish translation for “1 minute ago” would be “hace 1 minuto” - it has a prefix rather than a suffix to indicate a time in the past.

for past times, each language pack would have the two keys

en:

datetime:

distance_in_words:

prefix_ago: “”

suffix_ago: “ago”

es:

datetime:

distance_in_words:

prefix_ago: “hace”

suffix_ago: “”

if we wanted future times, we could do

en:

datetime:

distance_in_words:

prefix_from_now: “”

suffix_from_now: “from now”

es:

datetime:

distance_in_words:

prefix_from_now: “”

suffix_from_now: “a partir de ahora”