Fixed #3600 -- Made smart_unicode respect deferred evaluation in the case

of strings translated with gettext_lazy and friends.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@4904 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-04-02 10:53:05 +00:00
parent b6f085aa43
commit 4a3db287f1
2 changed files with 27 additions and 0 deletions

View File

@ -1,11 +1,20 @@
from django.conf import settings from django.conf import settings
from django.utils.html import escape from django.utils.html import escape
from django.utils.functional import Promise, lazy
# Converts a dictionary to a single string with key="value", XML-style with # Converts a dictionary to a single string with key="value", XML-style with
# a leading space. Assumes keys do not need to be XML-escaped. # a leading space. Assumes keys do not need to be XML-escaped.
flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()]) flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
def smart_unicode(s): def smart_unicode(s):
if isinstance(s, Promise):
# The input is something from gettext_lazy or similar. We don't want to
# translate it until render time, so defer the conversion.
return smart_unicode_lazy(s)
else:
return smart_unicode_immediate(s)
def smart_unicode_immediate(s):
if not isinstance(s, basestring): if not isinstance(s, basestring):
if hasattr(s, '__unicode__'): if hasattr(s, '__unicode__'):
s = unicode(s) s = unicode(s)
@ -15,6 +24,8 @@ def smart_unicode(s):
s = unicode(s, settings.DEFAULT_CHARSET) s = unicode(s, settings.DEFAULT_CHARSET)
return s return s
smart_unicode_lazy = lazy(smart_unicode_immediate, unicode)
class StrAndUnicode(object): class StrAndUnicode(object):
""" """
A class whose __str__ returns its __unicode__ as a bytestring A class whose __str__ returns its __unicode__ as a bytestring

View File

@ -10,4 +10,20 @@ It should be possible to re-use attribute dictionaries (#3810)
... f2 = CharField(widget=TextInput(attrs=extra_attrs)) ... f2 = CharField(widget=TextInput(attrs=extra_attrs))
>>> TestForm(auto_id=False).as_p() >>> TestForm(auto_id=False).as_p()
u'<p>F1: <input type="text" class="special" name="f1" maxlength="10" /></p>\n<p>F2: <input type="text" class="special" name="f2" /></p>' u'<p>F1: <input type="text" class="special" name="f1" maxlength="10" /></p>\n<p>F2: <input type="text" class="special" name="f2" /></p>'
#######################
# Tests for form i18n #
#######################
There were some problems with form translations in #3600
>>> from django.utils.translation import gettext_lazy, activate, deactivate
>>> class SomeForm(Form):
... username = CharField(max_length=10, label=gettext_lazy('Username'))
>>> f = SomeForm()
>>> print f.as_p()
<p><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>
>>> activate('de')
>>> print f.as_p()
<p><label for="id_username">Benutzername:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>
>>> deactivate()
""" """