Created
July 15, 2011 12:26
-
-
Save jimworm/1084598 to your computer and use it in GitHub Desktop.
Quickly do something in a locale then switch back
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
class ActionController::Base | |
protected | |
def do_using_temp_locale(temp_locale, &block) | |
if I18n.available_locales.include?(temp_locale.to_sym) | |
original_locale = I18n.locale | |
I18n.locale = temp_locale | |
return_value = yield | |
I18n.locale = original_locale | |
else | |
return_value = yield | |
end | |
return_value | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
do_using_temp_locale('locale_code'){ # do these things }
Useful for things like sending an email in another language then returning to render a page in the original.