简体   繁体   English

Ruby / Rails时间助手方法

[英]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. 我正在寻找一个可以帮助我格式化时间助手的辅助程序类/方法/ gem。 The output I'm looking after passing in an instance of Time.now is something like the following: 传递Time.now实例后,我正在寻找的输出如下所示:

"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: 这样的助手已经存在并且内置在Rails中:

http://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words 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 : 例如,如果您想自定义措辞,则可以创建一个自定义的I18n语言环境,我在config/locales/time_ago.yml创建了一个名为time_ago的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

# 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! 您当然可以将其添加到config/locales/en.yml并在应用程序范围内完全覆盖它们,这将允许您如上所述调用time_ago_in_words

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM