public
Last active

I18n.with_locale(:en) { do_stuff } # and others...

  • Download Gist
i18n_module_helpers.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
module I18n
def self.with_locale(locale)
old_locale, result = I18n.locale, nil
I18n.locale = locale
result = yield
I18n.locale = old_locale
result
end
 
def self.each_locale
I18n.load_path.locales.each do |locale|
yield locale
end
end
 
def self.for_all_locales
I18n.each_locale do |locale|
I18n.with_locale(locale) { yield }
end
end
end

Моля те не пиши Kernel.block_given? Изглежда извънземно. Дори не знаех, че е метод на Kernel и си мислех, че е ключова дума в езика.

Hey, you should use ensure in case exception is raised in the block.

  def self.with_locale(locale)
    old_locale = I18n.locale
    I18n.locale = locale
    yield
  ensure
    I18n.locale = old_locale
  end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.