Opened 7 months ago

Last modified 5 weeks ago

#23299 assigned New feature

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

Reported by: niwibe Owned by: aaugustin
Component: Internationalization Version: master
Severity: Normal Keywords: template-refactor
Cc: 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 (11)

comment:1 Changed 7 months ago by bmispelon

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset


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 Changed 6 months ago by gandriyko

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 6 months ago by gandriyko (previous) (diff)

comment:3 Changed 5 months ago by timgraham

  • Cc template-refactor added
  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 5 months ago by timgraham

  • Cc template-refactor removed
  • Keywords template-refactor added

comment:5 Changed 4 months ago by aaugustin

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 4 months ago by aaugustin

  • Resolution set to wontfix
  • Status changed from new to closed

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 4 months ago by niwibe

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 4 months ago by aaugustin

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 6 weeks ago by aaugustin

  • Resolution wontfix deleted
  • Status changed from closed to new

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

comment:10 Changed 6 weeks ago by aaugustin

  • Owner changed from nobody to aaugustin
  • Status changed from new to assigned

comment:11 Changed 5 weeks ago by aaugustin

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.

Note: See TracTickets for help on using tickets.
Back to Top