Created
August 22, 2012 17:27
-
-
Save remi/3427752 to your computer and use it in GitHub Desktop.
Change I18n.locale just for the time of a block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Why not use I18n.with_locale ?
Pourquoi tu changes kernel?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I18n.localizeandI18n.translateare probably not the best use cases (since you can pass them a:localeoption) but it can be very useful in background jobs that have to redefine the locale each time it tries to send an email (becauseActionMailerdo not support being passed a :localeoption).