0

On a project where I use Eel Python and you can tell Eel to use Jinja2.

Works fine (congrats to the creators). I installed Babel, created a /lang/ folder where I store .pot and .po and .mo files after extraction (using cli command, pybabel extract...), init of languages (pybabel ini) and finally compilation to get the .mo per language (pybabel compile).

In Eel init from babel.support I import Translations. I add i18n extension to the created Jinja Environement and I add translations Translations.load('lang', ['fr']) in the install_gettext_translations method of the Jinja Environnement.

Whatever I do my {% trans %} tags, my _("here a text") are not translated into the language I put. In babel.support class Translations, load method, if I track my parameters, the method gettext.find(dirname, locales...) always returns a None ??

I was wondering if I have to instanciate Babel - babel = Babel() - somewhere so that my path /lang/ be registered ?

Or if I have to set this path somewhere but I haven't found where.

I read several chunks of the documentation (Babel and Jinja) but I am stuck.

There is smthg I am not doing right...

If anyone has a clue, I would appreciate.

Thanks.

  1. I moved /lang/ into jinja templates folder

  2. I tried relative and absolute path for the parameter dirname

  3. Finally decided to dig into Babel. In babel.support class Translations, load method, if I track my parameters, the method gettext.find(dirname, locales...) always returns a None ??

In vain.

1 Answers1

0

I was so many times saved by stackoverflow and every contributor whi takes the time to answer that I am posting now the solution I found. I hope it will help.

1.As said install Babel in your virtual env so that you can enjoy pybabel command line to prepare your languages files (very usefull link here https://phrase.com/blog/posts/i18n-advantages-babel-python/)

My src/lang/babel-mapping.ini file looks like : [python: **.py]

[jinja2: /templates/.html]

extensions=jinja2.ext.i18n NOTE : other extensions are not needed anymore with Jinja2 3.X

  1. What I was missing (read my question above) Since Babel relies on native python gettext module, language folder MUST be initialized. Given gettext is imported in my Config object.__init__ method I launch a InitLangFolder method : gettext.bindtextdomain('messages', self.paths['lang'])
  • Messages param is the expected filename (default in Babel)

  • self.paths["lang"] is initialized in my InitPath method and it is = f"{ self.GetProjectFolderName() }/lang/" My architecture looks like : main.py build dist main_env (venv) src config data entities gui init lang ** base.pot babel-mapping.ini fr ... etc (you'll figure out by using pybabel extract)

  1. In Python Eel.init.py from Babel.support import Translations Update _start_args argument that init() can understand and add for instance 'jinja2_ext_i18n' (NO dot in the name !!) setting to None (default value needed) Update Eel.init.Start() Method so that your argument be read and add extension

    Added MSR 05/2023 - Deal with jinja2.ext.i18n extension, jinja_env must be initialized (line before)

     if _start_args['jinja_env'] != None and _start_args['jinja2_ext_i18n']:
         _start_args['jinja_env'].add_extension('jinja2.ext.i18n')
         # Creating translations file
         translations = Translations.load('lang', ['fr', 'en', 'es'])
         _start_args['jinja_env'].install_gettext_translations(translations)
    

NOTES : A) Translations.load may take a 3rd arg that is "domain" intended as the name of .mo files as compilated. Here empty because easier to keep default messages value. B) Jinja2 uses "templates" as default folder for storing your templates. Actually Eel handles this. Check github Eel page : https://github.com/python-eel/Eel C) I put fr, es, and en ; a larger amount can be put. I use EN as primary language and I checked fr translation was after having extracted, initialized, translated and compiled the fr/LC_MESSAGES/messages.po file

  1. Finally :
    • in your py filesevery where needed import gettext and use the shortcut _() for tagging message to be translated. NOTE you must say _ = gettext.gettext
    • in your html template use {% trans %}...{% endtrans %}

Good luck ! Thanks to Python and to Python community.

Alexandre