python-babel / flask-babel

i18n and l10n support for Flask based on Babel and pytz
https://python-babel.github.io/flask-babel/
Other
432 stars 159 forks source link

Fix force locale in app context and multiple threads #184

Closed lautat closed 1 year ago

lautat commented 3 years ago

Locale set by a force_locale context manager leaks between multiple threads when force_locale is used in application context rather than request context. This can be fixed by using flask.globals._app_ctx_stack as recommended by Flask documentation on extension development. Unlike current_app, the object on top of the _app_ctx_stack object is different in every application context so any attributes set to it do not change the same attributes in a different application context. This pull request is similar to #144, but it adds a test for the bug that the change fixes and moves tests related to force_locale to a new test module.

lautat commented 1 year ago

Flask 2.2 deprecated _request_ctx_stack, and it will be removed in version 2.3, so I updated the last commit to use flask.g instead as recommended by the documentation.