sveetch / Optimus

A simple building environment to produce static HTML from Jinja2 templates and with assets compress managing with webassets.
https://optimus.readthedocs.io/
MIT License
3 stars 3 forks source link

babel.cfg deprecation ? #40

Open sveetch opened 2 years ago

sveetch commented 2 years ago

I don't find anymore documentation about this file which was the reference config a long time ago and so i'm unable to find the right location for it in project templates (at root ? in project ? ... ?).

And is it still really implemented ? Maybe it have moved to another file ?

We have to find to enhance things, for now we may assume it as broken, however not any error is raised with Jinja and PO/MO files, but it may be with using the po CLI (didn't tested it again yet).

So finally what we will do:

sveetch commented 4 weeks ago

The babel.cfg file is indeed not commonly used since we define this config directly on babel.messages.extract.extract_from_dir from settings I18N_EXTRACT_MAP and I18N_EXTRACT_OPTIONS.

However the babel.cfg file may be used when user try to manage translation himself directly from Babel cli, this is documented in Optimus doc: https://optimus.readthedocs.io/en/latest/i18n.html#managing-translation-catalog-with-the-raw-way (this part is not up to date with Babel last release).

Additionally during my researches i found that string in code defined with gettext() are not translated in template render.

So with the following view code:

from gettext import gettext as _
from optimus.pages.views import PageTemplateView

class GenericPageView(PageTemplateView):
    """
    Basic page view.
    """
    title = "Not used"
    template_name = "generic.html"
    destination = "{language_code}/index.html"

    def get_title(self):
        return _("Generic basic page")

And this template:

<div>
    <h2 class="variable">{{ page_title }}</h2>
    <h2 class="hardcoded">{% trans %}Generic basic page{% endtrans %}</h2>
    <p>{% trans %}Hello World!{% endtrans %}</p>
</div>

The texts in tags {% trans %} are correctly translated, it demonstrates language and proper translations are given to Jinja. However the {{ page_title }} that is a gettext variable is still in english. The variable does not seem to be possibly rendered during. So for now, translated string can only work a translatable string in template, not from code (views or settings).

sveetch commented 4 weeks ago

Ok so finally the variable work but you must not just define it as a variable in template like this in template:

{{ page_title }}

But you need to define it in template like this:

{{ gettext(page_title) }}

So it just need to be documentated.