Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix locale selection in multi-threaded Flask app when force_locale is used #125

Merged
merged 1 commit into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions flask_babel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ def refresh():
if hasattr(ctx, key):
delattr(ctx, key)

if hasattr(ctx, 'forced_babel_locale'):
ctx.babel_locale = ctx.forced_babel_locale


@contextmanager
def force_locale(locale):
Expand All @@ -325,20 +328,19 @@ def force_locale(locale):
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)
ctx.babel_locale = Locale.parse(locale)
ctx.forced_babel_locale = ctx.babel_locale
ctx.babel_translations = None
yield
finally:
babel.locale_selector_func = orig_locale_selector_func
if hasattr(ctx, 'forced_babel_locale'):
del ctx.forced_babel_locale

for key, value in orig_attrs.items():
setattr(ctx, key, value)

Expand Down
41 changes: 41 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

import pickle
from threading import Thread, Semaphore

import unittest
from decimal import Decimal
Expand Down Expand Up @@ -204,6 +205,46 @@ def select_locale():
assert str(babel.get_locale()) == 'en_US'
assert str(babel.get_locale()) == 'de_DE'

def test_force_locale_with_threading(self):
app = flask.Flask(__name__)
b = babel.Babel(app)

@b.localeselector
def select_locale():
return 'de_DE'

semaphore = Semaphore(value=0)

def first_request():
with app.test_request_context():
with babel.force_locale('en_US'):
assert str(babel.get_locale()) == 'en_US'
semaphore.acquire()

thread = Thread(target=first_request)
thread.start()

try:
with app.test_request_context():
assert str(babel.get_locale()) == 'de_DE'
finally:
semaphore.release()
thread.join()

def test_refresh_during_force_locale(self):
app = flask.Flask(__name__)
b = babel.Babel(app)

@b.localeselector
def select_locale():
return 'de_DE'

with app.test_request_context():
with babel.force_locale('en_US'):
assert str(babel.get_locale()) == 'en_US'
babel.refresh()
assert str(babel.get_locale()) == 'en_US'


class NumberFormattingTestCase(unittest.TestCase):

Expand Down