Skip to content

Instantly share code, notes, and snippets.

@mitio
Created June 30, 2010 17:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitio/458992 to your computer and use it in GitHub Desktop.
Save mitio/458992 to your computer and use it in GitHub Desktop.
I18n.with_locale(:en) { do_stuff } # and others...
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
@skanev
Copy link

skanev commented Jun 30, 2010

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

@szajbus
Copy link

szajbus commented Sep 20, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment