Closed nboyet closed 1 year ago
@nboyet Hello and thanks for issuing the problem We will handle this problem as soon as possible.
@nboyet we've resolved the problem and You should uninstall v0.0.3 and install v0.0.4 pip install fastapi-babel
you can check new syntax and usage of fastapi babel in the main readme: README or pypi documentation
I offer you to use FastAPI Babel Cli => Link
@nboyet I reviewed your code but unfortunately, you did not install fr into your translation directory lunch this command and try it again:
pybabel init -i messages.pot -d lang -l fr
pybabel compile -d lang
Hi @Legopapurida,
Thanks for the update. So I've update it to v0.0.4 but unfortunately, it doesn't work.
Just to be totally sure, I removed the files babel.py and babel.cfg, lang folder and start from the beginning with and without cli. It works really well for fa, even for API response, I have the translation. But unfortunately, it does not work for other languages. Does it work for you with other languages ?
As well, I said it in my first post but I must admit I was not very clear : I did the same step to add translations for something other than fa by doing the 4 steps you describe for "fr" and "es".
My architecture is very simple and it is like this : ├── babel.cfg ├── babel.py ├── main.py ├── messages.pot ├── init.py ├── lang │ ├── es │ │ └── LC_MESSAGES │ │ ├── messages.mo │ │ └── messages.po │ ├── fa │ │ └── LC_MESSAGES │ │ ├── messages.mo │ │ └── messages.po │ └── fr │ └── LC_MESSAGES │ ├── messages.mo │ └── messages.po
My babel.py :
from fastapi_babel import Babel
from fastapi_babel import BabelConfigs
from config import settings
configs: BabelConfigs = BabelConfigs(
ROOT_DIR=__file__,
BABEL_DEFAULT_LOCALE="en",
BABEL_TRANSLATION_DIRECTORY="lang",
)
babel: Babel = Babel(configs=configs)
if __name__ == "__main__":
babel.run_cli()
For the 3 different messages.po (fr, fa, es), I just put the translation "test_countryname" such as "test_fa" for fa and "test_fr" for fr. For the main, I just call the babel text and it does not work for fr and es.
main.py :
if __name__ == "__main__":
babel.locale = "en"
en_text = _("File not found. There is nothing here")
print(en_text)
babel.locale = "fa"
fa_text = _("File not found. There is nothing here")
print(fa_text)
babel.locale = "fr"
fr_text = _("File not found. There is nothing here")
print(fr_text)
babel.locale = "es"
es_text = _("File not found. There is nothing here")
print(es_text)
main.py output:
~/Projects/my_project$ python main.py
File not found. There is nothing here
test_fa
File not found. There is nothing here
File not found. There is nothing here
@nboyet Because you set the default local Lang is en so if your message dir or po file not found Will act as simple str function without any lookup to dir
I will try to another language, we have tested for all languages.
Local es does not exist in to Lang dir And also I think your fr message.po is not in fr LC_MESSAGES dir. Reinstall the Lang with that.
He @Shahin-rmz tried in Dutch Lang and it worked well.
@nboyet
Thanks for issue We fixed the bug you only need to clone the project and use it again without using previous version
Hello,
TL;DR
The translations only work for "fa".
Explanation
I followed the steps available on the Readme. Everything's fine if the local is defined as fa, as shown on the example. I do :
Then, I run the main and it is ok:
However, if I do the same step to add translations for something other than fa, such as es or fr, the translation does not work.
Example :
By navigating around the core.py file, if I modify the property gettext by putting self.locale != 'en' instead of if self.locale == "fa", this time it works as expected.
Example :
So is there any way to modify this ? I tried to change BABEL_DEFAULT_LOCALE variable, BABEL_TRANSLATION_DIRECTORY, make en folder but nothing works except the code change. Have you any clue on how to correct this ?
Thanks !