Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Rails: Time ago in Words and its Customization

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.76k
    Comment on it

    Rails: Customizing Time Ago in Words
    Hi friends,
    Whenever something is published to a site, we see in their posted time, it is written like about 2 minutes ago or about an hour ago. These kind of sentences displays how much time before the content was published. This thing can be achieved in rails using it's time_ago_in_words helper. How to use date time in rails has already been explained Date Time Helpers in Rails. time_ago_in_words can be used as:

    	## pass the datetime stamp inside this helper function
    	time_ago_in_words(any_date_time_stamp)
    
    	## This will return the difference between the current time
    	## and will display them like
    
    	#=> about an hour
    

    Now it will give the full string. Suppose you want to customize the string it returns you can override all the defaults texts that it returns. To do this you need to place this entire code inside the config/locales/en.yml

    	en:
    	    datetime:
    		    distance_in_words:
    		      about_x_hours:
    		        one: about 1 hour
    		        other: about %{count} hours
    		      about_x_months:
    		        one: about 1 month
    		        other: about %{count} months
    		      about_x_years:
    		        one: about 1 year
    		        other: about %{count} years
    		      almost_x_years:
    		        one: almost 1 year
    		        other: almost %{count} years
    		      half_a_minute: half a minute
    		      less_than_x_minutes:
    		        one: less than a minute
    		        other: less than %{count} minutes
    		      less_than_x_seconds:
    		        one: less than 1 second
    		        other: less than %{count} seconds
    		      over_x_years:
    		        one: over 1 year
    		        other: over %{count} years
    		      x_days:
    		        one: 1 day
    		        other: "%{count} days"
    		      x_minutes:
    		        one: 1 minute
    		        other: "%{count} minutes"
    		      x_months:
    		        one: 1 month
    		        other: "%{count} months"
    		      x_seconds:
    		        one: 1 second
    		        other: "%{count} seconds"
    		    prompts:
    		      day: Day
    		      hour: Hour
    		      minute: Minute
    		      month: Month
    		      second: Seconds
    		      year: Year
    

    Thus by default it displays these texts whenever you use time_ago_in_words, so you can change whatever you want here, suppose you want to have these kind of modifications like: less than greater than should be replaced by its symbol, minutes should be replaced by "m", hours with "h", days with "d", months with "M" and year with "Y". Then the en.yml file can be changed to:

    	en:
    	    datetime:
    		    distance_in_words:
    		      about_x_hours:
    		        one: 1h
    		        other: %{count}h
    		      about_x_months:
    		        one: 1M
    		        other: %{count}M
    		      about_x_years:
    		        one: 1Y
    		        other: %{count}Y
    		      almost_x_years:
    		        one: 1Y
    		        other: %{count}Y
    		      half_a_minute: 0.5m
    		      less_than_x_minutes:
    		        one: < 1m
    		        other: < %{count}m
    		      less_than_x_seconds:
    		        one: < 1s
    		        other: < %{count}s
    		      over_x_years:
    		        one: > 1Y
    		        other: > %{count}Y
    		      x_days:
    		        one: 1d
    		        other: "%{count}d"
    		      x_minutes:
    		        one: 1m
    		        other: "%{count}m"
    		      x_months:
    		        one: 1M
    		        other: "%{count}M"
    		      x_seconds:
    		        one: 1s
    		        other: "%{count}s"
    		    prompts:
    		      day: D
    		      hour: h
    		      minute: m
    		      month: M
    		      second: s
    		      year: Y
    

     


    Hope you like reading this blog. For more like this, Click here.

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: