Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

unicode: Implemented string interpolation for lazy objects.

git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5420 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit fa689ae9c6eca0e7c68994713ce1f8fedbeb7b6e 1 parent bb97eea
@malcolmt malcolmt authored
View
14 django/utils/functional.py
@@ -32,9 +32,11 @@ def __init__(self, args, kw):
self.__dispatch[resultclass] = {}
for (k, v) in resultclass.__dict__.items():
setattr(self, k, self.__promise__(resultclass, k, v))
- if unicode in resultclasses:
+ self._delegate_str = str in resultclasses
+ self._delegate_unicode = unicode in resultclasses
+ assert not (self._delegate_str and self._delegate_unicode), "Cannot call lazy() with both str and unicode return types."
+ if self._delegate_unicode:
self.__unicode__ = self.__unicode_cast
- self._delegate_str = str in resultclasses
def __promise__(self, klass, funcname, func):
# Builds a wrapper around some magic method and registers that magic
@@ -62,6 +64,14 @@ def __str__(self):
else:
return Promise.__str__(self)
+ def __mod__(self, rhs):
+ if self._delegate_str:
+ return str(self) % rhs
+ elif self._delegate_unicode:
+ return unicode(self) % rhs
+ else:
+ raise AssertionError('__mod__ not supported for non-string types')
+
def __wrapper__(*args, **kw):
# Creates the proxy object, instead of the actual value.
return __proxy__(args, kw)
View
2  django/utils/translation/__init__.py
@@ -70,7 +70,7 @@ def string_concat(*strings):
gettext_lazy = lazy(gettext, str)
ungettext_lazy = lazy(ungettext, unicode)
ugettext_lazy = lazy(ugettext, unicode)
-string_concat = lazy(string_concat, str, unicode)
+string_concat = lazy(string_concat, unicode)
def activate(language):
return real_activate(language)
View
0  tests/regressiontests/i18n/__init__.py
No changes.
View
0  tests/regressiontests/i18n/models.py
No changes.
View
17 tests/regressiontests/i18n/tests.py
@@ -0,0 +1,17 @@
+# coding: utf-8
+
+ur"""
+>>> from django.utils.translation import ugettext_lazy, activate, deactivate
+>>> s = ugettext_lazy('Add %(name)s')
+>>> d = {'name': 'Ringo'}
+>>> s % d
+u'Add Ringo'
+>>> activate('de')
+>>> s % d
+u'Ringo hinzuf\xfcgen'
+>>> activate('pl')
+>>> s % d
+u'Dodaj Ringo'
+>>> deactivate()
+
+"""
Please sign in to comment.
Something went wrong with that request. Please try again.