Opened 2 years ago

Closed 17 months ago

#23299 closed New feature (wontfix)

Add some interoperability to _() function calls parsing in templatize function.

Reported by: Andrey Antukh Owned by: Aymeric Augustin
Component: Internationalization Version: master
Severity: Normal Keywords: template-refactor
Cc: Moritz Sichert Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The current implementation only works for django and works very well. But for make easy use another template engine like jinja2 and combine it with django i18n makemessages process, would be awesome include some support for more generic parsing of _() function calls.

The change is very small, it uses findall instead of match, that allows finding inner _() function calls that are very common in jinja2 templates (and not in django templates):

https://github.com/niwibe/django/compare/interoprability-jinja-django-i18n-parse

Change History (15)

comment:1 Changed 2 years ago by Baptiste Mispelon

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

Hi,

Can you show some examples of use-cases that are made possible by your change (these would be useful as testcases if we were to include the feature anyway).

For reference, I just checked your patch against Django's test suite and it doesn't seem to break anything which is a good sign.

Thanks.

comment:2 Changed 2 years ago by Andrew

Expample, when use Jinja2

When run django-admin.py makemessages -l uk

its collect fine items like {{ _('Index') }}

but item <th>{{ table_sort('date_start', _('Year')) }}</th> not working.

Last edited 2 years ago by Andrew (previous) (diff)

comment:3 Changed 2 years ago by Tim Graham

Cc: template-refactor added
Needs tests: set
Triage Stage: UnreviewedAccepted

comment:4 Changed 2 years ago by Tim Graham

Cc: template-refactor removed
Keywords: template-refactor added

comment:5 Changed 2 years ago by Aymeric Augustin

I believe this is a duplicate of #12377 which was closed as wontfix long ago.

But we're going to need this as part of my plan to support multiple template engines.

comment:6 Changed 2 years ago by Aymeric Augustin

Resolution: wontfix
Status: newclosed

The approach proposed here is too simple. It won't work with arbitrary template engines (eg. XML-based). Django needs proper extensibility.

comment:7 Changed 2 years ago by Andrey Antukh

I understand that django needs proper extensibility and obviously, my purpose fix only a little interoperability problem.

But the purposed change is very small, not affects to existing code and helps a lot to people that uses jinja with django. Please reconsider apply the change.

I don't think that it can disturb while django extensibility is solved...

comment:8 Changed 2 years ago by Aymeric Augustin

It will be solved in Django 1.8, unless my Multiple Template Engines for Django project fails. If that happens, we can reopen this ticket.

comment:9 Changed 21 months ago by Aymeric Augustin

Resolution: wontfix
Status: closednew

Reopening because I will include this as a stopgap solution in Django 1.8 until #24167 can be tackled.

comment:10 Changed 21 months ago by Aymeric Augustin

Owner: changed from nobody to Aymeric Augustin
Status: newassigned

comment:11 Changed 21 months ago by Aymeric Augustin

I extracted all translation constructs from examples given in Jinja2's documentation:

<p>{% trans %}1 Hello {{ user }}!{% endtrans %}</p>

<p>{% trans user=user.username %}2 Hello {{ user }}!{% endtrans %}</p>

{% trans book_title=book.title, author=author.name %}
3 This is {{ book_title }} by {{ author }}
{% endtrans %}

{% trans count=list|length %}
4 There is {{ count }} {{ name }} object.
{% pluralize %}
4 There are {{ count }} {{ name }} objects.
{% endtrans %}

{{ _('5 Hello World!') }}

{{ _('6 Hello %(user)s!')|format(user=user.username) }}

{{ gettext('7 Hello World!') }}

{{ gettext('8 Hello %(name)s!', name='World') }}

{{ ngettext('9 %(num)d apple', '9 %(num)d apples', apples|count) }}

(Numbers added to make strings unique.)

The change proposed in the patch doesn't cover them.

It doesn't seem reasonable to me to attempt to support both Django and Jinja2 syntax with a single regex-based parser, all the more since the {% trans %} tag has different syntax in the two languages.

Perhaps the right solution for Django 1.8 is to tell people to use Babel. I haven't benchmarked its features against makemessages yet.

comment:12 Changed 19 months ago by Moritz Sichert

There is also the possibility to write extensions for jinja2 that add the i18n tags with django's syntax so makemessages detects them.
I did a proof of concept of this and also added some other django-only tags:
https://github.com/MoritzS/jinja2-django-tags

comment:13 Changed 19 months ago by Moritz Sichert

Cc: Moritz Sichert added

comment:14 Changed 17 months ago by Aymeric Augustin

So, we've documented Babel as the recommended solution in 1.8, and not gotten negative feedback until now.

What about closing this ticket as wontfix?

comment:15 Changed 17 months ago by Aymeric Augustin

Resolution: wontfix
Status: assignedclosed
Note: See TracTickets for help on using tickets.
Back to Top