We internationalized our site months ago, but forgot one part: The drop down where a user picks their timezone.
How do you translate the following line:
I would imagine that this would need to be done manually in the same way that any other I18n translations are done in Rails. This would mean setting up locale files with the translations. Something like:
# 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
You could overwrite the time_zone_options_for_select
method (which is used by time_zone_select
) with the following:
def time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone)
zone_options = ""
zones = model.all
convert_zones = lambda do |list|
list.map do |z|
localized_name = I18n.t(z.name, :scope => :timezones, :default => z.name)
[ "(GMT#{z.formatted_offset}) #{localized_name}", z.name ]
end
end
if priority_zones
if priority_zones.is_a?(Regexp)
priority_zones = model.all.find_all {|z| z =~ priority_zones}
end
zone_options += options_for_select(convert_zones[priority_zones], selected)
zone_options += "\n"
zones = zones.reject { |z| priority_zones.include?( z ) }
end
zone_options += options_for_select(convert_zones[zones], selected)
zone_options
end
The changes are:
convert_zones = lambda do |list|
list.map do |z|
localized_name = I18n.t(z.name, :scope => :timezones, :default => z.name)
[ "(GMT#{z.formatted_offset}) #{localized_name}", z.name ]
end
end
What we're doing is, getting the localized name from the TimeZone name with I18n.t which is looking in config/locales/LANG.yml
formatted as shown above. If we can't find the translation, we just fallback on using the TimeZone name.
Now that we've done this setup, we should be able to use :
f.time_zone_select :timezone, ActiveSupport::TimeZone.all
or the shorter
f.time_zone_select :timezone # defaults to ActiveSupport::TimeZone.all