Opened 17 years ago

Closed 16 years ago

Last modified 13 years ago

#7027 closed (fixed)

Translating string constans passed to template tags breaks if the string contains spaces

Reported by: Konstantinos Metaxas <kmetaxas@…> Owned by: Jacob
Component: Internationalization Version: dev
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi. The documentation for i18n states that the following can be used to translate string constants passed to template tags:

{% some_special_tag _("Page not found") value|yesno:_("yes,no") %}

However, when i use it with a string that contains spaces (like in the example in the docs) it raises a TemplateSyntaxError claiming that more parameters where passed to the some_special_tag than it accepts.

It seems that django.utils.text.smart_split breaks up quoted strings that are enclosed like thus: _("my quoted string") and contain spaces.
Example:

>>> from django.utils.text import smart_split
>>> list(smart_split(' _("my quoted string") '))
[u'_("my', u'quoted', u'string")']
#Without spaces
>>> list(smart_split(' _("myquotedstring") '))
[u'_("myquotedstring")']

This makes translating multi-word string constants in template tags impossible.
I am using svn revision 7425.

Attachments (4)

fix_tag_translation.diff (3.1 KB ) - added by mrts 17 years ago.
fix_tag_translation-refactored-re_7027.diff (4.5 KB ) - added by mrts 16 years ago.
Refactored the regex and wrote an explanation
fix_tag_translation-refactored-re-strict_gettext_matching_7027.diff (4.5 KB ) - added by mrts 16 years ago.
This one won't let " ") and \(" " through, IMHO not worth the extra complexity though
fix_tag_translation-refactored-re-allow_empty_strings-7027.diff (4.5 KB ) - added by mrts 16 years ago.
Allow '' and ""

Download all attachments as: .zip

Change History (19)

comment:1 by Malcolm Tredinnick, 17 years ago

Component: Template systemInternationalization
Triage Stage: UnreviewedAccepted

comment:2 by mrts, 17 years ago

Has patch: set
milestone: 1.0

This clearly a bug that needs to fixed before 1.0. Attaching a patch with fixes and tests.

by mrts, 17 years ago

Attachment: fix_tag_translation.diff added

comment:3 by mrts, 17 years ago

Just a note that the patch passes all tests.

comment:4 by Jacob, 16 years ago

Goddamn that regex is scary. mrts, think you can rewrite it to use re.X and add some comments so we know what's going on there?

comment:5 by mrts, 16 years ago

Will do it shortly.

comment:6 by mrts, 16 years ago

But note that basically I just circumfixed non-grouping matches for either _('') or _("") and used raw strings to lessen the wild escaping in the original.

comment:7 by mrts, 16 years ago

Now that I review it, it looks like the original pattern (not written by me and carried over in this patch) is too complex. I should have cut it more radically. The proliferation of \ had a bad smell but I kind of assumed the person knew what he was doing. Jacob, do you know who it was so that I can discuss this with him? I see a much more straightforward pattern, but perhaps I'm missing something.

comment:8 by Malcolm Tredinnick, 16 years ago

Just upload a new patch. We'll work out if it's good enough. But, really, at this point, the smallest possible change that gets the job done is the only correct approach. Larger changes contain more bugs.

by mrts, 16 years ago

Refactored the regex and wrote an explanation

comment:9 by mrts, 16 years ago

Just a note -- this passes all tests.

by mrts, 16 years ago

This one won't let " ") and \(" " through, IMHO not worth the extra complexity though

comment:10 by anonymous, 16 years ago

This one won't let " ") and \(" " through, IMHO not worth the extra complexity though -- entirely pointless. Group C (see comments) will match unclosed bits anyway, so the last patch isn't really useful, there's no difference between _("foo and _("foo bar" correctness-wise -- both are invalid. Thus, fix_tag_translation-refactored-re_7027.diff is the way to go.

comment:11 by mrts, 16 years ago

And here are some illustrations for the invalid cases:

# with fix_tag_translation-refactored-re-strict_gettext_matching_7027.diff
>>> list(smart_split("_('foo bar'"))
[u"_('foo", u"bar'"]
>>> list(smart_split("_('foo bar"))
[u"_('foo", u'bar']
>>> list(smart_split("'foo bar"))
[u"'foo", u'bar']

# with fix_tag_translation-refactored-re_7027.diff -- best behaviour
>>> list(smart_split("_('foo bar'"))
[u"_('foo bar'"]
>>> list(smart_split("_('foo bar"))
[u"_('foo", u'bar']
>>> list(smart_split("'foo bar"))
[u"'foo", u'bar']

# original 
>>> list(smart_split("_('foo bar'"))
[u"_('foo", u"bar'"]
>>> list(smart_split("_('foo bar"))
[u"_('foo", u'bar']
>>> list(smart_split("'foo bar"))
[u"'foo", u'bar']

comment:12 by mrts, 16 years ago

An afterthought: the regex should allow empty strings, so (?:[^"\\]|\\.)+ should be (?:[^"\\]|\\.)* instead.

comment:13 by Jacob, 16 years ago

Owner: changed from nobody to Jacob
Status: newassigned

After thinking this through a bit more, doing this is smart_split isn't a good idea: smart_split shouldn't have to have any idea about translation-marked strings. I think the right place to this is in template.Token.split_contents, so I'll have a whack at doing it there.

comment:14 by Jacob, 16 years ago

Resolution: fixed
Status: assignedclosed

(In [8769]) Fixed #7027: template tags now corectly break tokens around strings marked for translation.

comment:15 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

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