From 7a39fca449d434645556d56f41bc1f26653c749f Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Sun, 8 Jul 2018 22:30:02 +0200 Subject: [PATCH] Add the force_locale utility function It will be needed when sending emails and generating log messages. --- calsocial/utils.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 calsocial/utils.py diff --git a/calsocial/utils.py b/calsocial/utils.py new file mode 100644 index 0000000..2127d61 --- /dev/null +++ b/calsocial/utils.py @@ -0,0 +1,70 @@ +# Calendar.social +# Copyright (C) 2018 Gergely Polonkai +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +"""Utility functions for Calendar.social +""" + +from contextlib import contextmanager + +@contextmanager +def force_locale(locale): + """Temporarily overrides the currently selected locale. + + Sometimes it is useful to switch the current locale to different one, do some tasks and then + revert back to the original one. For example, if the user uses German on the web site, but + you want to send them an email in English, you can use this function as a context manager:: + + with force_locale('en_US'): + send_email(gettext('Hello!'), ...) + + Shamelessly extracted from Flask-Babel. + + :param locale: The locale to temporary switch to (ex: 'en_US'). + :type locale: str + """ + + from flask import current_app, has_request_context, request + + def _get_current_context(): + if has_request_context(): + return request + + if current_app: + return current_app + + return None + + ctx = _get_current_context() + if ctx is None: + yield + return + + babel = current_app.extensions['babel'] + + orig_locale_selector_func = babel.locale_selector_func + orig_attrs = {} + for key in ('babel_translations', 'babel_locale'): + orig_attrs[key] = getattr(ctx, key, None) + + try: + babel.locale_selector_func = lambda: locale + for key in orig_attrs: + setattr(ctx, key, None) + yield + finally: + babel.locale_selector_func = orig_locale_selector_func + for key, value in orig_attrs.items(): + setattr(ctx, key, value)