Code

Opened 5 years ago

Closed 5 years ago

Last modified 3 years ago

#10604 closed (fixed)

Document that blocktrans fails if a defined expression is not used

Reported by: bartTC Owned by: jacob
Component: Documentation Version: master
Severity: Keywords: i18n, blocktrans,
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Blocktrans doesn't handle non-used expressions:

{% blocktrans count cl.result_count as total_count and cl.opts.verbose_name as verbose_name and cl.opts.verbose_name_plural as verbose_name_plural %}
0 of {{ total_count }} {{ verbose_name }} selected
{% plural %}
0 of {{ total_count }} {{ verbose_name_plural }} selected
{% endblocktrans %}

The above template raises an Exception with KeyError: u'verbose_name'. If I use verbose_name *and* verbose_name_plural in one group, it works.

Attachments (3)

ticket-10604-1.diff (683 bytes) - added by bartTC 5 years ago.
10604-ungettext-additional-vars-name-warning.diff (4.0 KB) - added by ramiro 5 years ago.
Documentation patch for this ticket
10604-ungettext-additional-vars-name-warning.2.diff (4.0 KB) - added by ramiro 5 years ago.
Smae patch with a code example fix. Thanks Alex Gaynor for noting it.

Download all attachments as: .zip

Change History (12)

comment:1 Changed 5 years ago by bartTC

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Here is a less cluttered example:

{% blocktrans count 1 as counter and "singular" as sing and "plural" as plur %}
    {{ sing }}
{% plural %}
    {{ plur }}
{% endblocktrans %}

Problem is, that the result only gets the variables out of the pural block. If you - in the single block - have a variable that is not listet in the plural block, it fails with a KeyError.

The attached patch merges the variables from the singular and the plural block into one. This works for me, tests went fine.

Changed 5 years ago by bartTC

comment:2 Changed 5 years ago by bartTC

  • Patch needs improvement set

Seems to be a limitation of gettext itself. Current patch works fine in Django, creates correct msgid's in the .po file but if I want to compile a po snippet like

#: templates/base.html:5
#, python-format
msgid "%(sing)s"
msgid_plural "%(plur)s"
msgstr[0] "translated: %(sing)s"
msgstr[1] "translated: %(plur)s"

it fails with:

LC_MESSAGES/django.po:27: a format specification for argument 'sing', as in 'msgstr[0]', doesn't exist in 'msgid'

I think it's out of Django's scope to fix this. However I leave this ticket open for discussion or references.

comment:3 Changed 5 years ago by ramiro

I think this is a duplicate of #10533.

comment:4 Changed 5 years ago by mtredinnick

  • Component changed from Template system to Documentation
  • milestone set to 1.1
  • Summary changed from blocktrans fails if a defined expression is not used to Document that blocktrans fails if a defined expression is not used
  • Triage Stage changed from Unreviewed to Accepted
  • Version set to SVN

There's nothing we can do here except document this. Gettext is broken in this respect and we have to live with it. leaving open until we document it.

comment:5 Changed 5 years ago by jacob

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

Changed 5 years ago by ramiro

Documentation patch for this ticket

comment:6 Changed 5 years ago by ramiro

  • Patch needs improvement unset

Changed 5 years ago by ramiro

Smae patch with a code example fix. Thanks Alex Gaynor for noting it.

comment:7 Changed 5 years ago by russellm

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [11164]) Fixed #10604 -- Added note on the limitation of ungettext, especially as relating to the {% blocktrans %} tag. Thanks to bartTC for the report, and Ramiro Morales for the patch.

comment:8 Changed 5 years ago by russellm

(In [11167]) [1.0.X] Fixed #10604 -- Added note on the limitation of ungettext, especially as relating to the {% blocktrans %} tag. Thanks to bartTC for the report, and Ramiro Morales for the patch.

Merge of r11164 from trunk.

comment:9 Changed 3 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.