Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Malcolm Tredinnick authored April 02, 2007
11  django/newforms/util.py
... ...
@@ -1,11 +1,20 @@
1 1
 from django.conf import settings
2 2
 from django.utils.html import escape
  3
+from django.utils.functional import Promise, lazy
3 4
 
4 5
 # Converts a dictionary to a single string with key="value", XML-style with
5 6
 # a leading space. Assumes keys do not need to be XML-escaped.
6 7
 flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
7 8
 
8 9
 def smart_unicode(s):
  10
+    if isinstance(s, Promise):
  11
+        # The input is something from gettext_lazy or similar. We don't want to
  12
+        # translate it until render time, so defer the conversion.
  13
+        return smart_unicode_lazy(s)
  14
+    else:
  15
+        return smart_unicode_immediate(s)
  16
+
  17
+def smart_unicode_immediate(s):
9 18
     if not isinstance(s, basestring):
10 19
         if hasattr(s, '__unicode__'):
11 20
             s = unicode(s)
@@ -15,6 +24,8 @@ def smart_unicode(s):
15 24
         s = unicode(s, settings.DEFAULT_CHARSET)
16 25
     return s
17 26
 
  27
+smart_unicode_lazy = lazy(smart_unicode_immediate, unicode)
  28
+
18 29
 class StrAndUnicode(object):
19 30
     """
20 31
     A class whose __str__ returns its __unicode__ as a bytestring
16  tests/regressiontests/forms/regressions.py
@@ -10,4 +10,20 @@
10 10
 ...     f2 = CharField(widget=TextInput(attrs=extra_attrs))
11 11
 >>> TestForm(auto_id=False).as_p()
12 12
 u'<p>F1: <input type="text" class="special" name="f1" maxlength="10" /></p>\n<p>F2: <input type="text" class="special" name="f2" /></p>'
  13
+
  14
+####################### 
  15
+# Tests for form i18n # 
  16
+####################### 
  17
+There were some problems with form translations in #3600
  18
+ 
  19
+>>> from django.utils.translation import gettext_lazy, activate, deactivate
  20
+>>> class SomeForm(Form):
  21
+...     username = CharField(max_length=10, label=gettext_lazy('Username'))
  22
+>>> f = SomeForm()
  23
+>>> print f.as_p()
  24
+<p><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>
  25
+>>> activate('de')
  26
+>>> print f.as_p()
  27
+<p><label for="id_username">Benutzername:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>
  28
+>>> deactivate()
13 29
 """

0 notes on commit 4a3db28

Please sign in to comment.
Something went wrong with that request. Please try again.