-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
Conversation
Calling |
@tvuotila thanks for the feedback. Since (I assume) your fix works with |
Yes, my fix works with
I like your solution more. It does not introduce another place for
If you could, that would be great. |
Alright, I'll fix the patch. |
This fixes issue #117 by setting locale in `force_locale` instead of replacing locale getter. Babel extension instance is shared across threads which causes issues when `force_locale` is used in one request while another starts. `forced_babel_locale` attribute of context is used to override `babel_locale` in case `refresh` is called within `force_locale` context.
I fixed the patch. It is not very pretty, but it works now with |
After spending 3 days trying to figure why our site randomly provides Chinese translations to all our users, we found this PR. This has resolved the issue and should be merged into the next release, so others don't have to go through the same troubles. |
Perhaps @TkTech could take a look? It looks like he has been maintaining this project lately. |
@lautat I can take a look this weekend. Keep in mind of the 4 people with admin access to this repo (which doesn't include me), none of them have ever committed or commented. I can't protect branches and add others as reviewers, nothing. So it's unfortunately just me vs the backlog and I no longer use flask at work. @mitsuhiko has unfortunately never replied to any of my emails over the years :( |
This fixes issue #117 by setting
babel_locale
inforce_locale
context manager instead of replacing locale getter. Babel extension instance is shared across threads (throughFlask.extensions
) which may cause wrong locale to be selected in request that is processed at the same time in one thread while another thread is usingforce_locale
. Setting locale to request context is thread-safe as Flask doesn't share request contexts between threads. I've added a test case to verify that this fix works, and it failed without changes toforce_locale
.