#7027 closed (fixed)
Translating string constans passed to template tags breaks if the string contains spaces
Reported by: | 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)
Change History (19)
comment:1 by , 17 years ago
Component: | Template system → Internationalization |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 17 years ago
Has patch: | set |
---|---|
milestone: | → 1.0 |
by , 17 years ago
Attachment: | fix_tag_translation.diff added |
---|
comment:4 by , 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:6 by , 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 , 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 , 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 , 16 years ago
Attachment: | fix_tag_translation-refactored-re_7027.diff added |
---|
Refactored the regex and wrote an explanation
by , 16 years ago
Attachment: | fix_tag_translation-refactored-re-strict_gettext_matching_7027.diff added |
---|
This one won't let " ")
and \(" "
through, IMHO not worth the extra complexity though
comment:10 by , 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 , 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 , 16 years ago
An afterthought: the regex should allow empty strings, so (?:[^"\\]|\\.)+
should be (?:[^"\\]|\\.)*
instead.
by , 16 years ago
Attachment: | fix_tag_translation-refactored-re-allow_empty_strings-7027.diff added |
---|
Allow ''
and ""
comment:13 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
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 , 16 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
This clearly a bug that needs to fixed before 1.0. Attaching a patch with fixes and tests.