51拾忆
51拾忆

改写time_ago_in_words方法

问题

rails中的time_ago_in_words方法,可以显示距离当前时间多久之前的时间。比如发表一篇文章,代码可以这样写 :

  post at <%= time_ago_in_words(@article.created_at) %> ago

页面中显示效果如下:
`“post at 5 days ago”`
如果要改成中文文字,需要对该方法重写。

解决

查找rails文档,可以发现这个方法是调用了如下这个方法实现的:

  def time_ago_in_words(from_time, include_seconds = false)
     distance_of_time_in_words(from_time, Time.now, include_seconds)
end

所以我们直接重写 distance_of_time_in_words 方法即可。方法如下:

def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
    from_time = from_time.to_time if from_time.respond_to?(:to_time)
    to_time = to_time.to_time if to_time.respond_to?(:to_time)
    distance_in_minutes = (((to_time - from_time).abs)/60).round
    distance_in_seconds = ((to_time - from_time).abs).round

    case distance_in_minutes
        when 0..1
            return (distance_in_minutes == 0) ? '不到 1 分钟' : '1 分钟' unless include_seconds
        case distance_in_seconds
            when 0..4   then '不到 5 秒'
            when 5..9   then '不到 10 秒'
            when 10..19 then '不到 20 秒'
            when 20..39 then '半分钟'
            when 40..59 then '不到 1 分钟'
            else             '1 分钟'
        end

        when 2..44           then "#{distance_in_minutes} 分钟"
        when 45..89          then '大约 1 小时'
        when 90..1439        then "大约 #{(distance_in_minutes.to_f / 60.0).round} 小时"
        when 1440..2879      then '1 天'
        when 2880..43199     then "#{(distance_in_minutes / 1440).round} 天"
        when 43200..86399    then '大约 1 个月'
        when 86400..525599   then "#{(distance_in_minutes / 43200).round} 个月"
        when 525600..1051199 then '大约 1 年'
    else                      "#{(distance_in_minutes / 525600).round} 年"
   end
end

发表评论

textsms
account_circle
email

51拾忆

改写time_ago_in_words方法
问题 rails中的time_ago_in_words方法,可以显示距离当前时间多久之前的时间。比如发表一篇文章,代码可以这样写 : post at ago 页面中显示效果如下: `“post at 5 days ago”` 如果要…
扫描二维码继续阅读
2020-11-21