Rails 更短的“time_ago_in_words" [英] Rails shorter "time_ago_in_words"

查看:28
本文介绍了Rails 更短的“time_ago_in_words"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rails 中除了time_ago_in_words"之外还有其他时间计算吗??我希望能够只使用 'h' 几个小时 'd' 天 'm' 几个月......例如.3d,或4h,或5m

Is there a different time calculation in rails besides "time_ago_in_words"?? I want to be able to use just 'h' for hours 'd' days 'm' for months... ex. 3d, or 4h, or 5m

我现在的代码...

<%= time_ago_in_words(feed_item.created_at) %> ago.

推荐答案

组成这个字符串的组件可以本地化,并且在 datetime.distance_in_words 命名空间中

The components that make up this string can be localised, and are in the datetime.distance_in_words namespace

例如棍子

en:
  datetime:
    distance_in_words:
      x_minutes:
        one: "1m"
        other: "%{count}m"

rails 会说 10m 而不是 10 分钟.根据需要重复几小时,几秒天等.你可以检查 locales/en.yml 在所有键的 action_view 中.

And rails will say 10m instead of 10 minutes. Repeat as needed for hours, seconds days etc. you can check locales/en.yml in action_view for all the keys.

如果你只想要短格式,你可以创建一个只使用这些键的伪语言环境并像这样使用

If you only want the short format you could create a pseudo locale that only used those keys and use it like so

time_ago_in_words created_at, false, :locale => :en_abbrev

这篇关于Rails 更短的“time_ago_in_words"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆