niwinz / django-jinja

Simple and nonobstructive jinja2 integration with Django.
http://niwinz.github.io/django-jinja/latest/
BSD 3-Clause "New" or "Revised" License
360 stars 102 forks source link

How to set the ext.i18n.trimmed policy ? #284

Closed paris-ci closed 3 years ago

paris-ci commented 3 years ago

Hello,

I want to set the ext.i18n.trimmed policy to trim the whitespace in {% trans %} blocks by default.

I couldn't find anything about it in the docs, what is the recommended way to proceed ?

Thanks.

wizpig64 commented 3 years ago

Hi @paris-ci,

This is the first I've read about this feature, but it looks like a simple enough addition for the project.

I pushed a PR that adds this as a feature via the TEMPLATES setting. Can you test out that branch and let me know if it works for your django project? No worries if you can't get around to it, I think I got it working in the test environment.

paris-ci commented 3 years ago

Hey @wizpig64,

Thanks for the quick PR, really appreciated. I tried installing your branch (pip3 install -e git+https://github.com/wizpig64/django-jinja.git@812c324c4a853ba502aaa5c5bc216006330c63ea#egg=django_jinja), set the policy like so

TEMPLATES = [
    {
        'BACKEND': 'django_jinja.backend.Jinja2',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'policies': {
                "ext.i18n.trimmed": True # <<<< Here
            },
            "match_extension": ".jinja2",
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            "constants": {
                ...
            },
            "extensions": DEFAULT_EXTENSIONS + [
                'pipeline.jinja2.PipelineExtension'
            ]
        },
    },

And ran the python3 manage.py makemessages --all -e "html,txt,py,jinja2" command.

Unfortunately, the following block (for example)

        <h1>{% trans players_count=paginator.count, channel_name=channel.name %}
            <span itemprop=\"numberOfPlayers\">{{ players_count }}</span> players on #{{ channel_name }}{% endtrans %}
        </h1>

Still gets added to the .po file like so, so without trimming.

#: botdata/templates/botdata/channel.jinja2:22
#, python-format
msgid ""
"\n"
"            <span itemprop=\\\"numberOfPlayers\\\">%(players_count)s</span> "
"players on #%(channel_name)s"
msgstr ""

Have I misunderstood something ?

Thanks for the help anyways :)

wizpig64 commented 3 years ago

Yeah, that's what it did for me too, with regards to not changing the .po file. What it did change was the rendered output in my web requests.

I don't think the makemessages command runs any jinja code. It builds on django's command, which does a regex search, but django-jinja adds regex rules for the jinja version of the translate tags, and thus doesn't know any policies.

From my test .po, It looks like if we use trans trimmed explicitly, the command will trim the block.

So perhaps we could figure out how to add automatic trimming support to the regex, by looking up the policy to always trim. I'll give it some thought and see if there's a simple way to do this.

paris-ci commented 3 years ago

Oh I see, I thought it was the other way around. Well to be honest, I don't really see a need to trim the templates output, but was rather looking for a way not to invalidate translations based on whitespace ¯_(ツ)_/¯

If there is a way to "fix" the regex, it'd be awesome !

Thanks for your help.

wizpig64 commented 3 years ago

@paris-ci

I've updated the makemessages command in my PR's branch. Now, if the ext.i18n.trimmed policy is set, it will insert the word trimmed into every trans block right before processing (unless it has notrimmed), resulting in properly trimmed .po files.

Could you test it out on your templates to see if I'm missing anything?

pip3 install -e git+https://github.com/wizpig64/django-jinja.git@ade847b3e5e4140809e7ea5a46c51ae175af3d31#egg=django_jinja

Thanks!

paris-ci commented 3 years ago

Just tried it... The previous example is now fixed to

#: botdata/templates/botdata/channel.jinja2:22
#, python-format
msgid ""
"<span itemprop=\\\"numberOfPlayers\\\">%(players_count)s</span> players on #"
"%(channel_name)s"
msgstr ""

Congratulations 🎉

wizpig64 commented 3 years ago

this feature is out now on pypi!