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:
= f.time_zone_select :timezone, ActiveSupport::TimeZone.all
Configure your Rails app The most important one is the config. time_zone configuration in your config/application. rb file. ActiveRecord will help you convert from and to (which the documentation fails to explain) UTC and the time zone of your choice.
In Rails, to see all the available time zones, run: $ rake time:zones:all * UTC -11:00 * American Samoa International Date Line West Midway Island Samoa * UTC -10:00 * Hawaii * UTC -09:00 * Alaska ... The default time zone in Rails is UTC.
Ruby | Time zone() functionTime#zone() : zone() is a Time class method which returns the name of the time zone used for time like “UTC”, “GMT”.
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
And in the view:
<%= 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With