Distance_of_time_in_words, then delete the word "about"

I have this code:

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

which also generates:

 about 15 hours less than 

Is there a way to remove the word "O" from the results? I’ve searched many times already, but couldn’t find any information, the function itself perfectly casts hours, minutes, seconds, and so cool, but the worst “o” must go! Does anyone know how? THX!

+6
source share
2 answers

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

look here for more information

try 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 the lines that you want to remove from this line. user like this:

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

+10
source

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

config / locale / 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" 
+15
source

Source: https://habr.com/ru/post/910205/


All Articles