Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

distance_of_time_in_words and then remove the word "about"

I have this code:

    = distance_of_time_in_words(Time.now, real_time + 0.seconds, true)

Which generates also like:

about 15 hours
less than

Is there a way to remove the word "About" from the results? Already searched a lot but cannot find any info, the function in itself is great it throws back hours, minutes, seconds, etc. so great but the worst "about" has to go! Anyone knows how?

like image 366
Rubytastic Avatar asked Mar 07 '12 20:03

Rubytastic


2 Answers

Since distance_of_time_in_words uses localization keys, you can simply override them to achieve what you want in an I18n-safe way:

config/locales/en.yml:

en:
  # 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:   "1 second" # default was: "less than 1 second"
        other: "%{count} seconds" # default was: "less than %{count} seconds"
      x_seconds:
        one:   "1 second"
        other: "%{count} seconds"
      less_than_x_minutes:
        one:   "a minute" # default was: "less than a minute"
        other: "%{count} minutes" # default was: "less than %{count} minutes"
      x_minutes:
        one:   "1 minute"
        other: "%{count} minutes"
      about_x_hours:
        one:   "1 hour" # default was: "about 1 hour"
        other: "%{count} hours" # default was: "about %{count} hours"
      x_days:
        one:   "1 day"
        other: "%{count} days"
      about_x_months:
        one:   "1 month" # default was: "about 1 month"
        other: "%{count} months" # default was: "about %{count} months"
      x_months:
        one:   "1 month"
        other: "%{count} months"
      about_x_years:
        one:   "1 year" # default was: "about 1 year"
        other: "%{count} years" # default was: "about %{count} years"
      over_x_years:
        one:   "1 year" # default was: "over 1 year"
        other: "%{count} years" # default was: "over %{count} years"
      almost_x_years:
        one:   "1 year" # default was: "almost 1 year"
        other: "%{count} years" # default was: "almost %{count} years"
like image 107
steveluscher Avatar answered Nov 14 '22 18:11

steveluscher


distance_of_time_in_words(Time.now, real_time + 0.seconds, true).gsub('about ','')

look here for more information

try a helper (put this code in app/helpers/application_helper.rb)

def remove_unwanted_words string
  bad_words = ["less than", "about"]

  bad_words.each do |bad|
    string.gsub!(bad + " ", '')
  end

  return string
end

in bad words you can define strings which you want to have removed from that string. user it like this:

<%= remove_unwanted_words distance_of_time_in_words(Time.now, real_time + 0.seconds, true) %>

like image 33
klump Avatar answered Nov 14 '22 18:11

klump