Translating Rails Timezones

前端 未结 9 684
滥情空心
滥情空心 2021-02-05 11:09

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:



        
9条回答
  •  囚心锁ツ
    2021-02-05 11:35

    The accepted answer from @DaveHollingworth worked for us under Rails 3.2, but we hit the error reported by @NazarHlynskyi when we upgraded to Rails 4.2:

    NoMethodError (undefined method [] for nil:NilClass)
    

    To work around this I have resorted to monkey patching the ActiveSupport::TimeZone, with the #to_s method above.

    So now in config/initializers/time_zone.rb we have the following

    class ActiveSupport::TimeZone
      def to_s
        translated_name = I18n.t(name, :scope => :timezones, :default => name)
        "(GMT#{formatted_offset}) #{translated_name}"
      end
    end
    

    and we no longer have the separate I18nTimeZone model.

    Monkey-patching may not be to your liking, in which case you will probably need to get a bit more intimate with the internal changes to ActiveSupport::TimeZone in order to recreate the behaviour, but the monkey-patching option provided a quick resolution for us.

提交回复
热议问题