We have asynchronous python application (telegram bot), and we want to add localization: user selects language when he starts dialog with bot, then bot translates all messages for him.
Django allows to change language for every request, it is working normally, because Django create separate process for each request. But it will not work in async bot — there is only one process and we should handle multiple users with different languages inside of it.
We can do simple thing — store user's preferences in Database, load the preferred language from DB with each incoming message, and them pass this settings to all inside functions — but it is quite complicated, because our bot is complex and there can be more than dozen included function calls.
How we can implement language switching in the asynchronous application with elegant way?
Okay, we solved problem using
with that provide us with context to all inner function calls.