Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 4a3db287f1c86f7e66408432b381190bc9d0557d 1 parent b6f085a
@malcolmt malcolmt authored
View
11 django/newforms/util.py
@@ -1,11 +1,20 @@
from django.conf import settings
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
# 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()])
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 hasattr(s, '__unicode__'):
s = unicode(s)
@@ -15,6 +24,8 @@ def smart_unicode(s):
s = unicode(s, settings.DEFAULT_CHARSET)
return s
+smart_unicode_lazy = lazy(smart_unicode_immediate, unicode)
+
class StrAndUnicode(object):
"""
A class whose __str__ returns its __unicode__ as a bytestring
View
16 tests/regressiontests/forms/regressions.py
@@ -10,4 +10,20 @@
... f2 = CharField(widget=TextInput(attrs=extra_attrs))
>>> 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>'
+
+#######################
+# 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()
"""
Please sign in to comment.
Something went wrong with that request. Please try again.