0

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?

asked Sep 13, 2024 at 21:06
6
  • Put the client's language in a session variable. Then use that when calling the translation method before returning a response to the client. Commented Sep 13, 2024 at 21:09
  • I'm not asking how to store client's language. This problem is trivial. I'm asking whether changing of language using translation install() method on the server-side (as frequent as multiple times per second) is a good practice at all? Reading the doc it seems like the install() method was meant to be used on the client side i.e. to change the current language in the client code e.g. when user changes language. My question is whether it's good practice to use this message on the server side to change languages between outputting subsequent JSON messages? Commented Sep 13, 2024 at 22:33
  • Is there an alternative? If you're doing the translation on the server, and Translation.install() is the only way to change languages, then you do it. Commented Sep 13, 2024 at 22:39
  • It's just super confusing, reading this function description, that it's really meant to be used like that. Per description, install() is meant to install the _() function in Python's built-ins namespace. Why would I re-install the _() function every time the server needs to translate every single message (in practice, the re-installation would occur many times a second)? I'd expect the _() function to be installed just once, when the server starts, and to change current language there should be another function with a more appropriate name (in gettext module scope, not on translation object) Commented Sep 13, 2024 at 23:00
  • 1
    I don't use this library, but I think _() is meant as a convenience function. I'll bet there a lower-level call that you can use to translate to a specific language without changing the global language setting. Commented Sep 13, 2024 at 23:02

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.