#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 Changed 15 years ago by
Component: | Template system → Internationalization |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 Changed 15 years ago by
Has patch: | set |
---|---|
milestone: | → 1.0 |
Changed 15 years ago by
Attachment: | fix_tag_translation.diff added |
---|
comment:4 Changed 15 years ago by
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 Changed 15 years ago by
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 Changed 15 years ago by
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 Changed 15 years ago by
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.
Changed 15 years ago by
Attachment: | fix_tag_translation-refactored-re_7027.diff added |
---|
Refactored the regex and wrote an explanation
Changed 15 years ago by
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 Changed 15 years ago by
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 Changed 15 years ago by
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 Changed 15 years ago by
An afterthought: the regex should allow empty strings, so (?:[^"\\]|\\.)+
should be (?:[^"\\]|\\.)*
instead.
Changed 15 years ago by
Attachment: | fix_tag_translation-refactored-re-allow_empty_strings-7027.diff added |
---|
Allow ''
and ""
comment:13 Changed 15 years ago by
Owner: | changed from nobody to Jacob |
---|---|
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 Changed 15 years ago by
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
This clearly a bug that needs to fixed before 1.0. Attaching a patch with fixes and tests.