翻译Rails时区 [英] Translating Rails Timezones
问题描述
几个月前,我们对网站进行了国际化,但忘记了一部分:用户选择时区的下拉菜单.
We internationalized our site months ago, but forgot one part: The drop down where a user picks their timezone.
您如何翻译以下行:
= f.time_zone_select :timezone, ActiveSupport::TimeZone.all
推荐答案
我遇到了同样的问题.但是,当我尝试实现 Peter的解决方案时,我想到了一个更简单的解决方案. time_zone_select
帮助器带有:model
选项,默认为ActiveSupport::TimeZone
.根据API文档,此模型所需要做的就是在all
方法中返回一个时区对象数组.然后,我们可以覆盖to_s
方法以返回翻译(如果未找到翻译,则默认为原始翻译).这是课程:
I've come across the same problem. However, when I was trying to implement Peter's solution, a simpler solution occurred to me. The time_zone_select
helper takes a :model
option, which defaults to ActiveSupport::TimeZone
. According to the API documentation, all this model has to do is return an array of timezone objects in the all
method. We can then override the to_s
method to return the translation (defaulting to the original if the translation isn't found). Here is the class:
# lib/i18n_time_zone.rb
class I18nTimeZone < ActiveSupport::TimeZone
def self.all
super.map { |z| create(z.name, z.utc_offset) }
end
def to_s
translated_name = I18n.t(name, :scope => :timezones, :default => name)
"(GMT#{formatted_offset}) #{translated_name}"
end
end
在视图中:
<%= time_zone_select :user, :time_zone, nil, :model => I18nTimeZone %>
与以前一样在翻译文件中指定翻译:
With the translations specified in the translation file as before:
# es.yml
es:
timezones:
"International Date Line West": "Línea de fecha internacional del oeste"
"Pacific Time (US & Canada)": "Tiempo pacífico (& de los E.E.U.U.; Canadá)"
# and so on
这篇关于翻译Rails时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!