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

Translation does not work if not fa #16

Closed nboyet closed 1 year ago

nboyet commented 2 years ago

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 :

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

Then, I run the main and it is ok:

# Input 

    babel.locale = "fa"
    fa_text = _("File not found. There is nothing here")
    print(fa_text)
    babel.locale = "en"
    en_text = _("File not found. There is nothing here")
    print(en_text)

# Output

Test_fa_ok
File not found. There is nothing here

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 :

# Input

    babel.locale = "es"
    es_text = _("File not found. There is nothing here")
    print(es_text)
    babel.locale = "fa"
    fa_text = _("File not found. There is nothing here")
    print(fa_text)
    babel.locale = "en"
    en_text = _("File not found. There is nothing here")
    print(en_text)
    babel.locale = "fr"
    fr_text = _("File not found. There is nothing here")
    print(fr_text)

# Output

File not found. There is nothing here
Test_fa_ok
File not found. There is nothing here
File not found. There is nothing here

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.

    @property
    def gettext(self) -> gettext:
        if self.locale == "fa":
            gt = translation(
                self.domain,
                self.config.BABEL_TRANSLATION_DIRECTORY,
                [self.locale],
            )
            gt.install()
            return gt.gettext
        return gettext
# Into
    @property
    def gettext(self) -> gettext:
        if self.locale != "en":
            gt = translation(
                self.domain,
                self.config.BABEL_TRANSLATION_DIRECTORY,
                [self.locale],
            )
            gt.install()
            return gt.gettext
        return gettext

Example :

# Input

    babel.locale = "es"
    es_text = _("File not found. There is nothing here")
    print(es_text)
    babel.locale = "fa"
    fa_text = _("File not found. There is nothing here")
    print(fa_text)
    babel.locale = "en"
    en_text = _("File not found. There is nothing here")
    print(en_text)
    babel.locale = "fr"
    fr_text = _("File not found. There is nothing here")
    print(fr_text)

# Output

Test_es_ok
Test_fa_ok
File not found. There is nothing here
Test_fr_ok

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 !

Legopapurida commented 2 years ago

@nboyet Hello and thanks for issuing the problem We will handle this problem as soon as possible.

Legopapurida commented 2 years ago

@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

Legopapurida commented 2 years ago

@nboyet I reviewed your code but unfortunately, you did not install fr into your translation directory lunch this command and try it again:

  1. pybabel init -i messages.pot -d lang -l fr
  2. Goto lang/fr/LC_MESSAGES/messages.po and add your translation to your messages.
  3. pybabel compile -d lang
  4. run main.py
nboyet commented 1 year ago

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
Legopapurida commented 1 year ago

@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.

Legopapurida commented 1 year ago

@nboyet

Thanks for issue We fixed the bug you only need to clone the project and use it again without using previous version