Anbarryprojects / fastapi-babel

FastAPI babel support pybable tools like translation of text, formatting of dates, times and numbers, and time zones.
MIT License
46 stars 14 forks source link

Library is using global state instead of per request state #25

Closed j123b567 closed 4 months ago

j123b567 commented 5 months ago

Currently, Babel.instance is used to store the current request language. If the request is running longer and different request with different locale comes, it can produce strange result.

It would be better to use request.state in InternationalizationMiddleware to store current translator and remove global state completely.

AnderUstarroz commented 4 months ago

This is another critical issue @Legopapurida, as @j123b567 mentioned, the Babel instance is shared by all users, this means that if two or more requests are happening at the same time (race conditions) one user will be able to change the language of other users.

I have created another PR https://github.com/Anbarryprojects/fastapi-babel/pull/29 which creates independent Babel instances for each user (request scoped), I think this could be a proper way to implement this.

I have still left the Babel.instance functionality so it can be used by the Babel CLI, although to be honest I don't understand why the Babel CLI is needed at all, when we just need:

pybabel extract -F babel.cfg -o messages.pot .
pybabel init -i messages.pot -d lang -l fa
pybabel compile -d lang

@j123b567 @Legopapurida could you have a look at the PR https://github.com/Anbarryprojects/fastapi-babel/pull/29 and tell me your thoughts?

peterschwarzdev commented 4 months ago

Is this project still alive? Why this PR is not merged or there are no new releases? @AnderUstarroz I think your pull request will make this project production ready.