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

Implemented request scoped translations #29

Closed AnderUstarroz closed 4 months ago

AnderUstarroz commented 4 months ago

There is a serious issue on current implementation, the same Babel instance is shared globally, which means that the request of one user can change the language of another user request depending on race conditions. This PR takes care of scoping a single Babel instance per request.

Legopapurida commented 4 months ago

I have reviewed your changes. You have implemented good changes but there are a few considerations for request.state. I will change the codes in the next version in another way. we must implement a request local proxy to handle the gettext as a global variable throughout the files and packages (without passing the request object to do that.

Legopapurida commented 4 months ago

I have tested the code in pull request and it does work fine.

Thanks for resolving this problem.

AnderUstarroz commented 4 months ago

I have reviewed your changes. You have implemented good changes but there are a few considerations for request.state. I will change the codes in the next version in another way. we must implement a request local proxy to handle the gettext as a global variable throughout the files and packages (without passing the request object to do that.

Sorry for not answering earlier... I saw that you remove the Singleton from core.py, then Babel.instance won't be needed anymore right? Thanks for taking the time to review the PR and making a new release 👍

AnderUstarroz commented 4 months ago

Actually... are you sure we can just get rid of the Babel.instance? It seems to be used also in the make_gettext function.