Django

Code

Changeset 4904

Show
Ignore:
Timestamp:
04/02/07 05:53:05 (2 years ago)
Author:
mtredinnick
Message:

Fixed #3600 -- Made smart_unicode respect deferred evaluation in the case
of strings translated with gettext_lazy and friends.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/util.py

    r4522 r4904  
    11from django.conf import settings 
    22from django.utils.html import escape 
     3from django.utils.functional import Promise, lazy 
    34 
    45# Converts a dictionary to a single string with key="value", XML-style with 
     
    78 
    89def 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 
     17def smart_unicode_immediate(s): 
    918    if not isinstance(s, basestring): 
    1019        if hasattr(s, '__unicode__'): 
     
    1524        s = unicode(s, settings.DEFAULT_CHARSET) 
    1625    return s 
     26 
     27smart_unicode_lazy = lazy(smart_unicode_immediate, unicode) 
    1728 
    1829class StrAndUnicode(object): 
  • django/trunk/tests/regressiontests/forms/regressions.py

    r4894 r4904  
    1111>>> TestForm(auto_id=False).as_p() 
    1212u'<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#######################  
     17There 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() 
    1329"""