Locale change to time_ago_in_words not changing - ruby-on-rails

I am trying to change the default time_ago_in_words function to use 1min instead for 1minute for example.
I came across the following but it doesn't seem to change anything. Inside en.yml
en:
datetime:
distance_in_words:
x_minutes:
one: "1 min"
other: "%{count} min"
any clues?

Related

Rails: Is there a built translation for "ago"?

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".
You could use custom scopes:
https://github.com/rails/rails/blob/fc5dd0b85189811062c85520fd70de8389b55aeb/actionview/lib/action_view/helpers/date_helper.rb#L75
time_ago_in_words(3.minutes.ago, scope: 'datetime.distance_in_words_ago') # => 3 minutes ago
and you need to add this to your locale (en.yml)
en:
datetime:
distance_in_words_ago:
x_days:
one: 1 day ago
other: "%{count} days ago"
x_minutes:
one: 1 minute ago
other: "%{count} minutes ago"
x_months:
one: 1 month ago
other: "%{count} months ago"
x_years:
one: 1 year ago
other: "%{count} years ago"
x_seconds:
one: 1 second ago
other: "%{count} seconds ago"
https://github.com/rails/rails/blob/fc5dd0b85189811062c85520fd70de8389b55aeb/actionview/lib/action_view/helpers/date_helper.rb#L78
I think you just need to pass the time as a variable to the translation.
Then in the language ymls, 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.

Ruby/Rails time helper method

I am looking for a helper class/method/gem that's out there that will help me format a time helper. The output I'm looking after passing in an instance of Time.now is something like the following:
"1 minute ago"
"2 minutes ago"
"1 hour ago"
"2 hours ago"
"1 day ago"
"2 days ago"
"over a year ago"
I started writing something like this but it's going to be long and painful and I feel like something like this has to exist. The only catch is I need it to use my own wording, so something with a formatter is required..
def time_ago_to_str(timestamp)
minutes = (((Time.now.to_i - timestamp).abs)/60).round
return nil if minutes < 0
Rails.logger.debug("minutes #{minutes}")
return "#{minutes} minute ago" if minutes == 1
return "#{minutes} minutes ago" if minutes < 60
# crap load more return statements to follow?
end
Such a helper already exists and is built-in to Rails:
http://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words
time_ago_in_words(5.days.ago)
=> "5 days"
EDIT:
If you'd like to customize the wording you can create a custom I18n locale, for example, I created one called time_ago in config/locales/time_ago.yml:
time_ago:
datetime:
distance_in_words:
half_a_minute: "half a minute"
less_than_x_seconds:
one: "less than 1 second"
other: "less than %{count} seconds"
x_seconds:
one: "1 second"
other: "%{count} seconds"
less_than_x_minutes:
one: "less than a minute"
other: "less than %{count} minutes"
x_minutes:
one: "1 min"
other: "%{count} mins"
about_x_hours:
one: "about 1 hour"
other: "about %{count} hours"
x_days:
one: "1 day"
other: "%{count} days"
about_x_months:
one: "about 1 month"
other: "about %{count} months"
x_months:
one: "1 month"
other: "%{count} months"
about_x_years:
one: "about 1 year"
other: "about %{count} years"
over_x_years:
one: "over 1 year"
other: "over %{count} years"
almost_x_years:
one: "almost 1 year"
other: "almost %{count} years"
Now, you can use the locale with distance_of_time_in_words:
# distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, options = {})
distance_of_time_in_words(5.minutes.ago, Time.now, true, {:locale => "time_ago"})
=> "5 mins"
You could of course add this to config/locales/en.yml and completely override them application wide, which would you allow you to call time_ago_in_words as mentioned above!

I18n::InvalidPluralizationData error

Based on help from this site, I was able to set up a display for the person's age. But when I do something like <%= distance_of_time_in_words(DateTime.now, p.dob) %>, I get an invalidPliralizationData error, "translation data {:one=>"1 an", :many=>"{{count}} ans"} can not be used with :count => 30"
My yml file has all the translations for datetime:distance_in_words_... where ... defines all the various possible occurrences.
Again help will be highly appreciated. All previous Google searches haven't been fruitful
I think the hash keys are :one and :other (not :many). I suspect they chose this wording because :other also includes the zero case (?).
Anyway, hope that helps!
It seems you didn't define :many in your localization file.
English has only :one and :many
If you are using :en then you should replace :many by :other.
Else if you are using another language you need to check the mapping key for 30 in config/locales/plurals.rb and define it in your localization file.
For example, 30 should be :many in :ar Arabic
Helped me with such localization file
en:
datetime:
distance_in_words:
less_than_x_seconds:
one: "1 second" # default was: "less than 1 second"
many: "%{count} seconds" # default was: "less than %{count} seconds"
x_seconds:
one: "1 second"
many: "%{count} seconds"
less_than_x_minutes:
one: "a minute" # default was: "less than a minute"
many: "less than %{count} minutes" # default was: "less than % {count} minutes"
x_minutes:
one: "1 minute"
many: "about %{count} minutes"
about_x_hours:
one: "1 hour" # default was: "about 1 hour"
many: "about %{count} hours" # default was: "about %{count} hours"
x_days:
one: "1 day"
many: "%{count} days"
about_x_months:
one: "1 month" # default was: "about 1 month"
many: "%{count} months" # default was: "about %{count} months"
x_months:
one: "1 month"
many: "%{count} months"
about_x_years:
one: "1 year" # default was: "about 1 year"
many: "%{count} years" # default was: "about %{count} years"
over_x_years:
one: "1 year" # default was: "over 1 year"
many: "%{count} years" # default was: "over %{count} years"
almost_x_years:
one: "1 year" # default was: "almost 1 year"
many: "%{count} years" # default was: "almost %{count} years"
As you can see, :other key changed to :many

Rails time_ago_in_words producing bad output

I thought this might be due to moving to activesupport 2.3.5 but now I believe something else must have happened.
Model has a valid rfc822 style date:
>> s.lastVisitDate
=> "Thu, 06 Jan 2011 22:24:10 -0800"
But in my view:
<%=h time_ago_in_words(#site.lastVisitDate) -%>
renders: *about {{count}} hours ago*
instead of: *about 2 hours ago* which was working just fine earlier.
Wondering if anyone else has seen this behavior. I've reviewed my version history for the model and view and nothing has changed recently so I believe I must have messed up something on the config side of things.
I found that I was missing the appropriate values in a locale file.
So in my case I added the following to /config/locales/en.yml
I am not sure why this was previously working or what specific gem or config change triggered this issue but having the proper definition here make actionpack happy.
# Used in distance_of_time_in_words(), distance_of_time_in_words_to_now(), time_ago_in_words()
datetime:
distance_in_words:
half_a_minute: "half a minute"
less_than_x_seconds:
one: "less than 1 second"
other: "less than %{count} seconds"
x_seconds:
one: "1 second"
other: "%{count} seconds"
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"
x_days:
one: "1 day"
other: "%{count} days"
about_x_months:
one: "about 1 month"
other: "about %{count} months"
x_months:
one: "1 month"
other: "%{count} months"
about_x_years:
one: "about 1 year"
other: "about %{count} years"
over_x_years:
one: "over 1 year"
other: "over %{count} years"
almost_x_years:
one: "almost 1 year"
other: "almost %{count} years"
prompts:
year: "Year"
month: "Month"
day: "Day"
hour: "Hour"
minute: "Minute"
second: "Seconds"

Rails 3 - time_ago_in_words says "ABOUT 2 hours ago"

Code:
<%="#{time_ago_in_words(comment.created_at)} ago "%>
What i'd like is for it not to have "ABOUT" in front of the 2 hours ago, which shows up for hours but not for minutes...
Is there another function or a way to remove it without finding and replacing?
You can change this via your I18n locale file. In config/locales/en.yml...
"en":
datetime:
distance_in_words:
about_x_hours:
# The defaults are "about 1 hour" and "about %{count} hours"
one: "1 hour"
other: "%{count} hours"
See the default locale file in actionpack for a complete reference.
I had the same issue, I ended up doing this, mostly because I'm still up in the air about whether or not to remove the about globally -
<p class="entry_created_at"><%= time_ago_in_words(plate.created_at).gsub('about','') + ' ago' %></p>
You can use my dotiw gem/plugin for that. It adds a couple of additional options and has greater precision than the one Rails offers.
distance_of_time_in_words(time1, time2, :only => [:days, :hours, :minutes])

Resources