I am working on a websocket server in python (FastAPI) which will work with multiple connected clients over websockets. The server is exchanging JSON messages with the clients. Some of the JSONs outputted by the server are UI-related i.e. they may include text messages which are then shown to the user in the client UI.
How do I make the server-side multilingual?
Let’s suppose I always know each user’s language and the language never changes as long as the websocket is open. All I want is my server to be able to translate messages on the fly, for each user.
Python’s gettext
includes an example where install()
method is invoked on translation object to do exactly that: change the language on the fly. But from what I understood the install()
method seems to be meant for the client-side usage, where the user changes the language in the client app. I want to be able to do the same thing on the server side, where I’ll have hundreds or even thousands connected websockets with potentially different languages. Which means the language switching can potentially happen for each serialized JSON message. With large number of concurrent users (with different languages) it can theoretically be many times per seconds.
Is calling the Translation.install()
method just before serialization of each JSON message on the server side really the correct way of doing it? Can very frequent changes affect overall server performance or maybe have another side effects? If it’s a no-go, what’s the best practice?
6