我知道这看起来微不足道,但是让我说在Ruby on Rails中我有
document.expire_in = 7.days如何打印人类可读版本的过期消息?
"Document will expire in #{document.expire_in}"
=> Document will expire in 7 days也许是与I18n.t或I18n.l一起工作的东西
唯一有效的方法是
7.days.inspect => "7天“
这是唯一的办法吗??
我正在查看ActiveSupport::Duration,但没有看到答案
thx
发布于 2013-03-23 00:43:30
所以在Rails中没有内置的解决方案来解决这个问题。我决定和他一起
7.days.inspect => "7 days"稍后,当项目将被翻译时,我将用一些有意义的东西来扩展ActiveSupport::Duration,它将翻译这些
然而,我建议看看罗伯茨对这个问题的评论。我同意在数据库中保持价值的解决方案,例如:"7天“,然后做一些事情。例如,翻译单位值
document = Document.new
document.expire_in = "7 days"
document.translated_day在文档模型(或装饰器)中
class Document < ActiveRecord::Base
#....
def translated_day
timeline = expire_in.split(' ')
"#{timeline.first} #{I18n.t("timeline.${timeline.last}")}"
end
#..
end
#config/locales/svk.yml
svk:
timeline:
days: "dni"发布于 2013-03-09 01:09:46
这并没有回答您的具体问题,但在我看来,您最好将datetime设置为过期,然后再利用distance_of_time_in_words。
如果你总是简单地说7天,为什么不把它写成一个硬编码的字符串呢?
发布于 2020-05-05 19:48:29
下面的示例演示了使用ActiveSupport::Duration#parts的i18n解决方案
duration.parts.map { |unit, n| I18n.t unit, count: n, scope: 'duration' }.to_sentence它可以与本地化一起工作,比如:
en:
duration:
years:
one: "%{count} year"
other: "%{count} years"
months:
one: "%{count} month"
other: "%{count} months"
weeks:
one: "%{count} week"
other: "%{count} weeks"
days:
one: "%{count} day"
other: "%{count} days"
hours:
one: "%{count} hour"
other: "%{count} hours"
minutes:
one: "%{count} minute"
other: "%{count} minutes"
seconds:
one: "%{count} second"
other: "%{count} seconds"https://stackoverflow.com/questions/15299344
复制相似问题