Opened 12 years ago

Closed 4 years ago

#18995 closed Cleanup/optimization (fixed)

String formatting error when passing floats as values in {% blocktrans %} tags

Reported by: Bruno Renié Owned by: Jacob Walls
Component: Internationalization Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With the following template code:

{% blocktrans count counter=person.distance_in_miles|floatformat:0 %}{{ counter }} mile away{% plural %}{{ counter }} miles away{% endblocktrans %}

And a russian translation:

#, python-format
msgid "%(counter)s mile away"
msgid_plural "%(counter)s miles away"
msgstr[0] "На расстоянии %(counter)s мили"
msgstr[1] "На расстоянии %(counter)s миль"
msgstr[2] "На расстоянии %(counter)s миль"

Rendering the template fails with a String formatting error: "TypeError: not all arguments converted during string formatting".

This happens because gettext string formatting fails when a float is passed. Removing the floatformat and casting the value as int works fine.

This could be improved by either:

  • Swallow TypeError and throw a friendlier message saying the type might not be compatible with gettext's string formatting (not a fan of swallowing TypeError though, we need to make sure nothing else in do_translate throws such an exception)
  • Actively checking the type's compatibility with the string format

Change History (7)

comment:1 by Claude Paroz, 12 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

comment:2 by Julien Phalip, 11 years ago

I've done some digging and found that the issue isn't to do with floats. In fact, ungettext will happily work with floats without raising any exceptions.

The root of the problem is that floatformat returns a string, which conflicts with the Russian plural rules. Indeed, those rules use the modulo sign %:

"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"

If you pass a string as a value for n, then the % sign will operate as string formatting instead of a modulo, therefore causing the TypeError. The same effect can be easily reproduced as follows:

>>> '123' % 10
*** TypeError: not all arguments converted during string formatting

I suggest making blocktrans raise an exception upstream if the value provided for the counter is not a number.

comment:3 by Jacob Walls, 4 years ago

Has patch: set
Owner: changed from nobody to Jacob Walls
Status: newassigned

comment:4 by Mariusz Felisiak, 4 years ago

Patch needs improvement: set
Version: 1.4master

comment:5 by Jacob Walls, 4 years ago

Patch needs improvement: unset

Adjusted patch per review to allow floats and decimal.Decimal in addition to integers.

comment:6 by Mariusz Felisiak, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 01a7af09:

Fixed #18995 -- Made blocktranslate tag raise TemplateSyntaxError when plural count is not a number.

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