Opened 10 years ago

Closed 9 years 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: dev
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


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):

Change History (15)

comment:1 by Baptiste Mispelon, 10 years ago


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.


comment:2 by Andrew, 10 years ago

Expample, when use Jinja2

When run makemessages -l uk

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

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

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

comment:3 by Tim Graham, 10 years ago

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

comment:4 by Tim Graham, 10 years ago

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

comment:5 by Aymeric Augustin, 10 years ago

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 by Aymeric Augustin, 10 years ago

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 by Andrey Antukh, 10 years ago

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 by Aymeric Augustin, 10 years ago

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 by Aymeric Augustin, 9 years ago

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 by Aymeric Augustin, 9 years ago

Owner: changed from nobody to Aymeric Augustin
Status: newassigned

comment:11 by Aymeric Augustin, 9 years ago

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, %}
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 by Moritz Sichert, 9 years ago

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:

comment:13 by Moritz Sichert, 9 years ago

Cc: Moritz Sichert added

comment:14 by Aymeric Augustin, 9 years ago

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 by Aymeric Augustin, 9 years ago

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