我们几个月前将我们的网站国际化,但忘记了一个部分:用户选择时区的下拉菜单。
如何翻译以下内容:
= f.time_zone_select :timezone, ActiveSupport::TimeZone.all
答案 0 :(得分:14)
我遇到了同样的问题。但是,当我尝试实现Peter's solution时,我发现了一个更简单的解决方案。 time_zone_select
帮助程序采用:model
选项,默认为ActiveSupport::TimeZone
。根据API文档,所有这个模型都要做的是在all
方法中返回一个时区对象数组。然后,我们可以覆盖to_s
方法以返回翻译(如果未找到翻译,则默认为原始翻译)。这是班级:
# 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 %>
使用翻译文件中指定的翻译:
# 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
答案 1 :(得分:5)
我在这篇文章中提出了一些想法并构建了一个宝石来处理时区翻译。我现在只有en和es翻译,但很容易将它添加到你的应用程序并添加更多的语言环境。只需将其添加到您的Gemfile:
gem i18n-timezones
你可以在这里获取来源:
答案 2 :(得分:4)
我遇到了同样的问题,fishwebby的答案很容易解决。我需要将时区翻译成西班牙语,所以我在这里留下列表:
"International Date Line West": "Línea de fecha internacional del oeste"
"Midway Island": "Isla de Midway"
"Samoa": "Samoa"
"Hawaii": "Hawai"
"Alaska": "Alaska"
"Pacific Time (US & Canada)": "Hora del Pacífico (EE.UU. y Canadá)"
"Tijuana": "Tijuana"
"Mountain Time (US & Canada)": "Hora de las Montañas (EE.UU. y Canadá)"
"Arizona": "Arizona"
"Chihuahua": "Chihuahua"
"Mazatlan": "Mazatlán"
"Central Time (US & Canada)": "Hora central (EE.UU. y Canadá)"
"Saskatchewan": "Saskatchewan"
"Guadalajara": "Guadalajara"
"Mexico City": "Ciudad de México"
"Monterrey": "Monterrey"
"Central America": "América Central"
"Eastern Time (US & Canada)": "Hora del Este (EE.UU. y Canadá)"
"Indiana (East)": "Indiana (Este)"
"Bogota": "Bogotá"
"Lima": "Lima"
"Quito": "Quito"
"Atlantic Time (Canada)": "Hora del Atlántico (Canadá)"
"Caracas": "Caracas"
"La Paz": "La Paz"
"Santiago": "Santiago"
"Newfoundland": "Terranova"
"Brasilia": "Brasilia"
"Buenos Aires": "Buenos Aires"
"Georgetown": "Georgetown"
"Greenland": "Groenlandia"
"Mid-Atlantic": "Atlántico medio"
"Azores": "Azores"
"Cape Verde Is.": "Isla Cabo Verde"
"Dublin": "Dublín"
"Edinburgh": "Edimburgo"
"Lisbon": "Lisboa"
"London": "Londres"
"Casablanca": "Casablanca"
"Monrovia": "Monrovia"
"UTC": "UTC"
"Belgrade": "Belgrado"
"Bratislava": "Bratislava"
"Budapest": "Budapest"
"Ljubljana": "Ljubljana"
"Prague": "Praga"
"Sarajevo": "Sarajevo"
"Skopje": "Skopje"
"Warsaw": "Varsovia"
"Zagreb": "Zagreb"
"Brussels": "Bruselas"
"Copenhagen": "Copenhague"
"Madrid": "Madrid"
"Paris": "París"
"Amsterdam": "Amsterdam"
"Berlin": "Berlín"
"Bern": "Berna"
"Rome": "Roma"
"Stockholm": "Estocolmo"
"Vienna": "Viena"
"West Central Africa": "Centro-Oeste de África"
"Bucharest": "Bucarest"
"Cairo": "El Cairo"
"Helsinki": "Helsinki"
"Kyev": "Kyev"
"Riga": "Riga"
"Sofia": "Sofía"
"Tallinn": "Tallin"
"Vilnius": "Vilnius"
"Athens": "Atenas"
"Istanbul": "Estambul"
"Minsk": "Minsk"
"Jerusalem": "Jerusalén"
"Harare": "Harare"
"Pretoria": "Pretoria"
"Moscow": "Moscú"
"St. Petersburg": "San Petersburgo"
"Volgograd": "Volgogrado"
"Kuwait": "Kuwait"
"Riyadh": "Riad"
"Nairobi": "Nairobi"
"Baghdad": "Bagdad"
"Tehran": "Teherán"
"Abu Dhabi": "Abu Dhabi"
"Muscat": "Moscatel"
"Baku": "Bakú"
"Tbilisi": "Tbilisi"
"Yerevan": "Ereván"
"Kabul": "Kabul"
"Ekaterinburg": "Ekaterinburg"
"Islamabad": "Islamabad"
"Karachi": "Karachi"
"Tashkent": "Tashkent"
"Chennai": "Chennai"
"Kolkata": "Kolkata"
"Mumbai": "Mumbai"
"New Delhi": "Nueva Delhi"
"Kathmandu": "Katmandú"
"Astana": "Astana"
"Dhaka": "Dhaka"
"Sri Jayawardenepura": "Sri Jayawardenepura"
"Almaty": "Almaty"
"Novosibirsk": "Novosibirsk"
"Rangoon": "Rangún"
"Bangkok": "Bangkok"
"Hanoi": "Hanoi"
"Jakarta": "Yakarta"
"Krasnoyarsk": "Krasnoyarsk"
"Beijing": "Beijing"
"Chongqing": "Chongqing"
"Hong Kong": "Hong Kong"
"Urumqi": "Urumqi"
"Kuala Lumpur": "Kuala Lumpur"
"Singapore": "Singapur"
"Taipei": "Taipei"
"Perth": "Perth"
"Irkutsk": "Irkutsk"
"Ulaan Bataar": "Ulán Bator"
"Seoul": "Seúl"
"Osaka": "Osaka"
"Sapporo": "Sapporo"
"Tokyo": "Tokio"
"Yakutsk": "Yakutsk"
"Darwin": "Darwin"
"Adelaide": "Adelaida"
"Canberra": "Canberra"
"Melbourne": "Melbourne"
"Sydney": "Sydney"
"Brisbane": "Brisbane"
"Hobart": "Hobart"
"Vladivostok": "Vladivostok"
"Guam": "Guam"
"Port Moresby": "Port Moresby"
"Magadan": "Magadan"
"Solomon Is.": "Islas Salomón"
"New Caledonia": "Nueva Caledonia"
"Fiji": "Fiji"
"Kamchatka": "Kamchatka"
"Marshall Is.": "Islas Marshall"
"Auckland": "Auckland"
"Wellington": "Wellington"
"Nuku'alofa": "Nuku'alofa"
答案 3 :(得分:4)
fishwebby的解决方案对我不起作用。最简单的方法是将其粘贴在/config/initializers/time_zone_patch.rb中:
# See https://stackoverflow.com/questions/1396623/translating-rails-timezones
class ActiveSupport::TimeZone
def to_s
offset = "(GMT#{formatted_offset})"
translated_name = I18n.t(name, :scope => :time_zones, :default => name)
%(#{offset} #{translated_name})
end
end
无需修改您的视图代码。
您可以将其与this fork of the rails-i18n gem结合使用。它包括在对此问题的另一个回复中提供的西班牙语时区翻译。我将根据需要或提供更多翻译,直到原始宝石作者将时区合并到他们的项目中。
答案 4 :(得分:1)
我认为这需要手动完成,就像在Rails中完成任何其他I18n翻译一样。这意味着使用翻译设置语言环境文件。类似的东西:
# 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
您可以使用以下内容覆盖time_zone_options_for_select
方法(由time_zone_select
使用):
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 += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
zones = zones.reject { |z| priority_zones.include?( z ) }
end
zone_options += options_for_select(convert_zones[zones], selected)
zone_options
end
变化是:
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
我们正在做的是,使用I18n.t从TimeZone名称获取本地化名称,其中config/locales/LANG.yml
格式如上所示。如果我们找不到翻译,我们只会使用TimeZone名称。
现在我们已经完成了这个设置,我们应该可以使用:
f.time_zone_select :timezone, ActiveSupport::TimeZone.all
或更短的
f.time_zone_select :timezone # defaults to ActiveSupport::TimeZone.all
答案 5 :(得分:1)
我在翻译时区时遇到了问题,比如“圣彼得堡”
"translation missing: en, time_zones, St, Petersburg"
我从密钥中删除了。并更改了这个:
translated_name = I18n.t(name, :scope => :time_zones, :default => name)
为:
translated_name = I18n.t(name.gsub(".", ""), :scope => :time_zones, :default => name)
有四个键。:
答案 6 :(得分:0)
法语翻译:
"American Samoa": "Samoa américaines" # -11:00
#"International Date Line West": "" # -11:00
"Midway Island": "Îles Midway" # -11:00
"Hawaii": "Hawaï" # -10:00
"Alaska": "Alaska" # -09:00
"Pacific Time (US & Canada)": "Pacific Time (É.-U. et Canada)" # -08:00
"Tijuana": "Tijuana" # -08:00
"Arizona": "Arizona" # -07:00
"Chihuahua": "Chihuahua" # -07:00
"Mazatlan": "Mazatlán" # -07:00
"Mountain Time (US & Canada)": "Mountain Time (É.-U. et Canada)" # -07:00
"Central America": "Amérique centrale" # -06:00
"Central Time (US & Canada)": "Central Time (É.-U. et Canada)" # -06:00
"Guadalajara": "Guadalajara" # -06:00
"Mexico City": "Mexico" # -06:00
"Monterrey": "Monterrey" # -06:00
"Saskatchewan": "Saskatchewan" # -06:00
"Bogota": "Bogotá" # -05:00
"Eastern Time (US & Canada)": "Heure de l'Est (É.-U. et Canada)" # -05:00
"Indiana (East)": "Indiana (est)" # -05:00
"Lima": "Lima" # -05:00
"Quito": "Quito" # -05:00
"Caracas": "Caracas" # -04:30
"Atlantic Time (Canada)": "Atlantic Time (Canada)" # -04:00
"Georgetown": "Georgetown" # -04:00
"La Paz": "La Paz" # -04:00
"Santiago": "Santiago" # -04:00
"Newfoundland": "Terre-Neuve" # -03:30
"Brasilia": "Brasilia" # -03:00
"Buenos Aires": "Buenos Aires" # -03:00
"Greenland": "Groenland" # -03:00
#"Mid-Atlantic": "" # -02:00
"Azores": "Açores" # -01:00
"Cape Verde Is.": "Cap-Vert" # -01:00
"Casablanca": "Casablanca" # +00:00
"Dublin": "Dublin" # +00:00
"Edinburgh": "Édimbourg" # +00:00
"Lisbon": "Lisbonne" # +00:00
"London": "Londres" # +00:00
"Monrovia": "Monrovia" # +00:00
"UTC": "UTC" # +00:00
"Amsterdam": "Amsterdam" # +01:00
"Belgrade": "Belgrade" # +01:00
"Berlin": "Berlin" # +01:00
"Bern": "Berne" # +01:00
"Bratislava": "Bratislava" # +01:00
"Brussels": "Bruxelles" # +01:00
"Budapest": "Budapest" # +01:00
"Copenhagen": "Copenhague" # +01:00
"Ljubljana": "Ljubljana" # +01:00
"Madrid": "Madrid" # +01:00
"Paris": "Paris" # +01:00
"Prague": "Prague" # +01:00
"Rome": "Rome" # +01:00
"Sarajevo": "Sarajevo" # +01:00
"Skopje": "Skopje" # +01:00
"Stockholm": "Stockholm" # +01:00
"Vienna": "Vienne" # +01:00
"Warsaw": "Varsovie" # +01:00
"West Central Africa": "Heure d'Afrique occidentale" # +01:00
"Zagreb": "Zagreb" # +01:00
"Athens": "Athènes" # +02:00
"Bucharest": "Bucarest" # +02:00
"Cairo": "Caire" # +02:00
"Harare": "Harare" # +02:00
"Helsinki": "Helsinki" # +02:00
"Istanbul": "Istanbul" # +02:00
"Jerusalem": "Jérusalem" # +02:00
"Kyiv": "Kiev" # +02:00
"Pretoria": "Pretoria" # +02:00
"Riga": "Riga" # +02:00
"Sofia": "Sofia" # +02:00
"Tallinn": "Tallinn" # +02:00
"Vilnius": "Vilnius" # +02:00
"Baghdad": "Bagdad" # +03:00
"Kuwait": "Koweït" # +03:00
"Minsk": "Minsk" # +03:00
"Nairobi": "Nairobi" # +03:00
"Riyadh": "Riyad" # +03:00
"Tehran": "Téhéran" # +03:30
"Abu Dhabi": "Abou Dabi" # +04:00
"Baku": "Bakou" # +04:00
"Moscow": "Moscou" # +04:00
"Muscat": "Mascate" # +04:00
"St. Petersburg": "Saint-Pétersbourg" # +04:00
"Tbilisi": "Tbilissi" # +04:00
"Volgograd": "Volgograd" # +04:00
"Yerevan": "Erevan" # +04:00
"Kabul": "Kaboul" # +04:30
"Islamabad": "Islamabad" # +05:00
"Karachi": "Karachi" # +05:00
"Tashkent": "Tachkent" # +05:00
"Chennai": "Chennai" # +05:30
"Kolkata": "Calcutta" # +05:30
"Mumbai": "Bombay" # +05:30
"New Delhi": "New Delhi" # +05:30
"Sri Jayawardenepura": "Sri Jayawardenapura" # +05:30
"Kathmandu": "Katmandou" # +05:45
"Almaty": "Almaty" # +06:00
"Astana": "Astana" # +06:00
"Dhaka": "Dacca" # +06:00
"Ekaterinburg": "Iekaterinbourg" # +06:00
"Rangoon": "Rangoun" # +06:30
"Bangkok": "Bangkok" # +07:00
"Hanoi": "Hanoï" # +07:00
"Jakarta": "Jakarta" # +07:00
"Novosibirsk": "Novossibirsk" # +07:00
"Beijing": "Pékin" # +08:00
"Chongqing": "Chongqing" # +08:00
"Hong Kong": "Hong Kong" # +08:00
"Krasnoyarsk": "Krasnoïarsk" # +08:00
"Kuala Lumpur": "Kuala Lumpur" # +08:00
"Perth": "Perth" # +08:00
"Singapore": "Singapour" # +08:00
"Taipei": "Taipei" # +08:00
"Ulaan Bataar": "Oulan-Bator" # +08:00
"Urumqi": "Ürümqi" # +08:00
"Irkutsk": "Irkoutsk" # +09:00
"Osaka": "Osaka" # +09:00
"Sapporo": "Sapporo" # +09:00
"Seoul": "Séoul" # +09:00
"Tokyo": "Tokyo" # +09:00
"Adelaide": "Adélaïde" # +09:30
"Darwin": "Darwin" # +09:30
"Brisbane": "Brisbane" # +10:00
"Canberra": "Canberra" # +10:00
"Guam": "Guam" # +10:00
"Hobart": "Hobart" # +10:00
"Melbourne": "Melbourne" # +10:00
"Port Moresby": "Port Moresby" # +10:00
"Sydney": "Sydney" # +10:00
"Yakutsk": "Iakoutsk" # +10:00
"New Caledonia": "Nouvelle-Calédonie" # +11:00
"Vladivostok": "Vladivostok" # +11:00
"Auckland": "Auckland" # +12:00
"Fiji": "Fidji" # +12:00
"Kamchatka": "Kamtchatka" # +12:00
"Magadan": "Magadan" # +12:00
"Marshall Is.": "Îles_Marshall" # +12:00
"Solomon Is.": "Îles Salomon" # +12:00
"Wellington": "Wellington" # +12:00
"Nuku'alofa": "Nuku'alofa" # +13:00
"Samoa": "Samoa" # +13:00
"Tokelau Is.": "Tokelau" # +14:00
答案 7 :(得分:0)
日语翻译
ja:
timezones:
"International Date Line West": "国際日付変更線"
"Midway Island": "ミッドウェイ諸島"
"American Samoa": "米領サモア"
"Samoa": "サモア"
"Hawaii": "ハワイ"
"Alaska": "アラスカ"
"Pacific Time (US & Canada)": "太平洋標準時 (アメリカ・カナダ)"
"Tijuana": "ティファナ"
"Mountain Time (US & Canada)": "山岳部標準時 (アメリカ・カナダ)"
"Arizona": "アリゾナ"
"Chihuahua": "チワワ"
"Mazatlan": "マサトラン"
"Central Time (US & Canada ": "中部標準時 (アメリカ・カナダ)"
"Saskatchewan": "サスカチュワン"
"Guadalajara": "グアダラハラ"
"Mexico City": "メキシコシティ"
"Monterrey": "モントレー"
"Central America": "中央アメリカ"
"Eastern Time (US & Canada)": "東部標準時 (アメリカ・カナダ)"
"Indiana (East)": "インディアナ (東)"
"Bogota": "ボゴタ"
"Lima": "リマ"
"Quito": "キト"
"Atlantic Time (Canada)": "大西洋標準時 (カナダ)"
"Caracas": "カラカス"
"La Paz": "ラパス"
"Santiago": "サンティアゴ"
"Newfoundland": "ニューファンドランド"
"Brasilia": "ブラジリア"
"Buenos Aires": "ブエノスアイレス"
"Georgetown": "ジョージタウン"
"Greenland": "グリーンランド"
"Mid - Atlantic": "中部大西洋"
"Azores": "アゾレス諸島"
"Cape Verde Is": "カーボベルデ"
"Dublin": "ダブリン"
"Edinburgh": "エディンバラ"
"Lisbon": "リスボン"
"London": "ロンドン"
"Casablanca": "カサブランカ"
"Monrovia": "モンロビア"
"UTC": "協定世界時"
"Belgrade": "ベオグラード"
"Bratislava": "ブラチスラヴァ"
"Budapest": "ブダペスト"
"Ljubljana": "リュブリャナ"
"Prague": "プラハ"
"Sarajevo": "サラエボ"
"Skopje": "スコピエ"
"Warsaw": "ワルシャワ"
"Zagreb": "ザグレブ"
"Brussels": "ブリュッセル"
"Copenhagen": "コペンハーゲン"
"Madrid": "マドリード"
"Paris": "パリ"
"Amsterdam": "アムステルダム"
"Berlin": "ベルリン"
"Bern": "ベルン"
"Rome": "ローマ"
"Stockholm": "ストックホルム"
"Vienna": "ウィーン"
"West Central Africa": "西中部アフリカ"
"Bucharest": "ブカレスト"
"Cairo": "カイロ"
"Helsinki": "ヘルシンキ"
"Kyiv": "キエフ"
"Riga": "リガ"
"Sofia": "ソフィア"
"Tallinn": "タリン"
"Vilnius": "ヴィリニュス"
"Athens": "アテネ"
"Istanbul": "イスタンブル"
"Minsk": "ミンスク"
"Jerusalem": "エルサレム"
"Harare": "ハラレ"
"Pretoria": "プレトリア"
"Moscow": "モスクワ"
"St. Petersburg": "サンクトペテルブルク"
"Volgograd": "ボルゴグラード"
"Kuwait": "クウェート"
"Riyadh": "リヤド"
"Nairobi": "ナイロビ"
"Baghdad": "バグダード"
"Tehran": "テヘラン"
"Abu Dhabi": "アブダビ"
"Muscat": "マスカット"
"Baku": "バクー"
"Tbilisi": "トビリシ"
"Yerevan": "エレバン"
"Kabul": "カーブル"
"Ekaterinburg": "エカテリンブルク"
"Islamabad": "イスラマバード"
"Karachi": "カラチ"
"Tashkent": "タシュケント"
"Chennai": "チェンナイ"
"Kolkata": "コルカタ"
"Mumbai": "ムンバイ"
"New Delhi": "ニューデリー"
"Kathmandu": "カトマンズ"
"Astana": "アスタナ"
"Dhaka": "ダッカ"
"Sri Jayawardenapura": "スリジャヤワルダナプラ"
"Almaty": "アルマトイ"
"Novosibirsk": "ノヴォシビルスク"
"Rangoon": "ヤンゴン"
"Bangkok": "バンコク"
"Hanoi": "ハノイ"
"Jakarta": "ジャカルタ"
"Krasnoyarsk": "クラスノヤルスク"
"Beijing": "北京市"
"Chongqing": "重慶市"
"Hong Kong": "香港"
"Urumqi": "ウルムチ市"
"Kuala Lumpur": "クアラルンプール"
"Singapore": "シンガポール"
"Taipei": "台北市"
"Perth": "パース"
"Irkutsk": "イルクーツク"
"Ulaan Bataar": "ウランバートル"
"Seoul": "ソウル"
"Osaka": "大阪"
"Sapporo": "札幌"
"Tokyo": "東京"
"Yakutsk": "ヤクーツク"
"Darwin": "ダーウィン"
"Adelaide": "アデレード"
"Canberra": "キャンベラ"
"Melbourne": "メルボルン"
"Sydney": "シドニー"
"Brisbane": "ブリスベン"
"Hobart": "ホバート"
"Vladivostok": "ウラジオストク"
"Guam": "グアム"
"Port Moresby": "ポートモレスビー"
"Magadan": "マガダン"
"Solomon Is": "ソロモン諸島"
"New Caledonia": "ニューカレドニア"
"Fiji": "フィジー"
"Kamchatka": "カムチャツカ半島"
"Marshall Is": "マーシャル諸島"
"Auckland": "オークランド"
"Wellington": "ウェリントン"
"Nuku'alofa": "ヌクアロファ"
"Tokelau Is": "トケラウ"
答案 8 :(得分:0)
@DaveHollingworth接受的答案在Rails 3.2下为我们工作,但是在升级到Rails 4.2时,我们遇到了@NazarHlynskyi报告的错误:
NoMethodError (undefined method [] for nil:NilClass)
要解决此问题,我已采用上面的ActiveSupport::TimeZone
方法通过猴子修补#to_s
。
所以现在在config/initializers/time_zone.rb
中,我们有以下内容
class ActiveSupport::TimeZone
def to_s
translated_name = I18n.t(name, :scope => :timezones, :default => name)
"(GMT#{formatted_offset}) #{translated_name}"
end
end
,我们不再拥有单独的I18nTimeZone
模型。
猴子补丁可能不符合您的喜好,在这种情况下,您可能需要对ActiveSupport::TimeZone
的内部更改更加熟悉才能重新创建行为,但是提供了猴子补丁选项快速解决方案。