Skip to content

Instantly share code, notes, and snippets.

@remi
Created August 22, 2012 17:27
Show Gist options
  • Save remi/3427752 to your computer and use it in GitHub Desktop.
Save remi/3427752 to your computer and use it in GitHub Desktop.
Change I18n.locale just for the time of a block
module Kernel
# I18n.locale = :en
# with_locale(:fr) do
# I18n.localize(Time.now, :format => :long) # => "mercredi 22 août 2012 13:23"
# end
# I18n.localize(Time.now, :format => :long) # => "August 22, 2012 13:23"
def with_locale(locale)
old_locale = I18n.locale
I18n.locale = locale
capture = yield
I18n.locale = old_locale
capture
end
end
@remi
Copy link
Author

remi commented Aug 22, 2012

I18n.localize and I18n.translate are probably not the best use cases (since you can pass them a :locale option) but it can be very useful in background jobs that have to redefine the locale each time it tries to send an email (because ActionMailer do not support being passed a :locale option).

@jbourassa
Copy link

Why not use I18n.with_locale ?

@ph
Copy link

ph commented Aug 23, 2012

Pourquoi tu changes kernel?

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