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

Problem with `makemessages` after updating to django-jinja 2.7.0 #272

Closed pacahon closed 3 years ago

pacahon commented 3 years ago

Hello.

Looks like it's impossible to use {% trans myvar %} syntax in DTL files after this change https://github.com/niwinz/django-jinja/commit/e3764f65560c9e9edab71b858e345cc641621441 was introduced.

auvipy commented 3 years ago

@wizpig64 can you check please?

hugokernel commented 3 years ago

Same problem here.

Could you check please.

wizpig64 commented 3 years ago

TL;DR workaround for django 3.1 users:

Django 3.1 renamed the trans tag family to translate, but kept the old tags around for compatibility with old templates.

If you are using django 3.1 or higher, convert your DLT templates to use {% translate myvar %} instead of {% trans myvar %}, and things ought to work again using the current django-jinja 2.7.0.


exhaustive explanation:

I was able to replicate this issue by converting the repo's i18n_test.jinja to DTL. At first it looked like nothing was wrong, but after re-reading the ticket I realized I was using {% translate %} and not {% trans %}. switching got it to raise the initially-misleading message SyntaxError: Translation blocks must not include other block tags: trans 'Year' as t_year (file testapp/templates/i18n_test.html, line 6).

Here's what's happening in this particular failure: Django is trying to prevent nested {% blocktrans(late) %}/{% endblocktrans(late) %} blocks, but the name of jinja's equivalent to these ({% trans %}/{% endtrans %}) collides with django's standalone {% trans myvar %} tag. Because our monkey patched makemessages command processes both types of templates with the same collection of regexes, and I was unaware of this at the time of 2.7.0, my faulty regex change treated a DTL {% trans myvar %} tag like a {% blocktrans(late) %} tag, which breaks things as it expects a closing {% endblocktrans(late) %}.

My new regex uses a different approach to solve #255 that seems to work with the right tags in the right engines.

wizpig64 commented 3 years ago

@pacahon @hugokernel I've submitted a change that fixed this in my test template, and should not necessitate upgrading to the translate tag as above. Please test it out with your templates, as they might break differently than mine did.

hugokernel commented 3 years ago

@pacahon @hugokernel I've submitted a change that fixed this in my test template, and should not necessitate upgrading to the translate tag as above. Please test it out with your templates, as they might break differently than mine did.

I had try the new regexp and it works well.

Thx

pacahon commented 3 years ago

It works for me too. Thanks.