Author: mtredinnick
Date: 2007-04-02 05:53:05 -0500 (Mon, 02 Apr 2007)
New Revision: 4904

Modified:
   django/trunk/django/newforms/util.py
   django/trunk/tests/regressiontests/forms/regressions.py
Log:
Fixed #3600 -- Made smart_unicode respect deferred evaluation in the case
of strings translated with gettext_lazy and friends.


Modified: django/trunk/django/newforms/util.py
===================================================================
--- django/trunk/django/newforms/util.py        2007-04-01 07:36:06 UTC (rev 
4903)
+++ django/trunk/django/newforms/util.py        2007-04-02 10:53:05 UTC (rev 
4904)
@@ -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 @@
         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

Modified: django/trunk/tests/regressiontests/forms/regressions.py
===================================================================
--- django/trunk/tests/regressiontests/forms/regressions.py     2007-04-01 
07:36:06 UTC (rev 4903)
+++ django/trunk/tests/regressiontests/forms/regressions.py     2007-04-02 
10:53:05 UTC (rev 4904)
@@ -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()
 """


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to