summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-06-03 05:35:06 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-06-03 05:35:06 +0000
commitfa689ae9c6eca0e7c68994713ce1f8fedbeb7b6e (patch)
treeb6284894808663f57f28cc19d03d5fc6474e97c8
parentbb97eea9ec88978ecd5dca0a17d9b3ba344f9152 (diff)
downloaddjango-fa689ae9c6eca0e7c68994713ce1f8fedbeb7b6e.tar.gz
unicode: Implemented string interpolation for lazy objects.
git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5420 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/utils/functional.py14
-rw-r--r--django/utils/translation/__init__.py2
-rw-r--r--tests/regressiontests/i18n/__init__.py0
-rw-r--r--tests/regressiontests/i18n/models.py0
-rw-r--r--tests/regressiontests/i18n/tests.py17
5 files changed, 30 insertions, 3 deletions
diff --git a/django/utils/functional.py b/django/utils/functional.py
index 8703c15efe..cd34809837 100644
--- a/django/utils/functional.py
+++ b/django/utils/functional.py
@@ -32,9 +32,11 @@ def lazy(func, *resultclasses):
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 lazy(func, *resultclasses):
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)
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
index 84fa76c57a..13fc8a847a 100644
--- a/django/utils/translation/__init__.py
+++ b/django/utils/translation/__init__.py
@@ -70,7 +70,7 @@ ngettext_lazy = lazy(ngettext, str)
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)
diff --git a/tests/regressiontests/i18n/__init__.py b/tests/regressiontests/i18n/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/i18n/__init__.py
diff --git a/tests/regressiontests/i18n/models.py b/tests/regressiontests/i18n/models.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/i18n/models.py
diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
new file mode 100644
index 0000000000..278957dad4
--- /dev/null
+++ b/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()
+
+"""