Rails: "ago" 有内置翻译吗?
Rails: Is there a built translation for "ago"?
Rails 之前用 time_ago_in_words
翻译,文档是 this.
time_ago_in_words(3.minutes.from_now) # => 3 minutes
但是,在我们的应用中,我们使用 "ago": 3 minutes ago
那么,当 "ago" 出现在 time_ago
之前时,最好的翻译方式是什么,例如 French:
我还有 3 分钟
这是 Rails 内置的吗?
使用 Rails 4.2.10 和 rails-i18n gem 提供时间距离,但不是 "ago".
您可以使用自定义范围:
time_ago_in_words(3.minutes.ago, scope: 'datetime.distance_in_words_ago') # => 3 minutes ago
并且您需要将此添加到您的语言环境 (en.yml)
en:
datetime:
distance_in_words_ago:
x_days:
one: 1 day ago
other: "%{count} days ago"
x_minutes:
one: 1 minute ago
other: "%{count} minutes ago"
x_months:
one: 1 month ago
other: "%{count} months ago"
x_years:
one: 1 year ago
other: "%{count} years ago"
x_seconds:
one: 1 second ago
other: "%{count} seconds ago"
我认为你只需要将时间作为变量传递给翻译即可。
然后在语言 yml
s 中,您可以将字符串的其余部分放在需要的位置。
假设 @product.purchased_time_ago
returns time_ago_in_words()
.
# app/views/products/show.html.erb
<%= t('time_ago', time: @product.purchased_time_ago) %>
# config/locales/en.yml
en:
time_ago: "%{time} ago"
# config/locales/fr.yml
fr:
time_ago: "Il y a %{time}"
这是直接取自docs。
Rails 之前用 time_ago_in_words
翻译,文档是 this.
time_ago_in_words(3.minutes.from_now) # => 3 minutes
但是,在我们的应用中,我们使用 "ago": 3 minutes ago
那么,当 "ago" 出现在 time_ago
之前时,最好的翻译方式是什么,例如 French:
我还有 3 分钟
这是 Rails 内置的吗?
使用 Rails 4.2.10 和 rails-i18n gem 提供时间距离,但不是 "ago".
您可以使用自定义范围:
time_ago_in_words(3.minutes.ago, scope: 'datetime.distance_in_words_ago') # => 3 minutes ago
并且您需要将此添加到您的语言环境 (en.yml)
en:
datetime:
distance_in_words_ago:
x_days:
one: 1 day ago
other: "%{count} days ago"
x_minutes:
one: 1 minute ago
other: "%{count} minutes ago"
x_months:
one: 1 month ago
other: "%{count} months ago"
x_years:
one: 1 year ago
other: "%{count} years ago"
x_seconds:
one: 1 second ago
other: "%{count} seconds ago"
我认为你只需要将时间作为变量传递给翻译即可。
然后在语言 yml
s 中,您可以将字符串的其余部分放在需要的位置。
假设 @product.purchased_time_ago
returns time_ago_in_words()
.
# app/views/products/show.html.erb
<%= t('time_ago', time: @product.purchased_time_ago) %>
# config/locales/en.yml
en:
time_ago: "%{time} ago"
# config/locales/fr.yml
fr:
time_ago: "Il y a %{time}"
这是直接取自docs。