Skip to content

Instantly share code, notes, and snippets.

@jimworm
Created July 15, 2011 12:26
Show Gist options
  • Save jimworm/1084598 to your computer and use it in GitHub Desktop.
Save jimworm/1084598 to your computer and use it in GitHub Desktop.
Quickly do something in a locale then switch back
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
@jimworm
Copy link
Author

jimworm commented Jul 15, 2011

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.

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