From fa689ae9c6eca0e7c68994713ce1f8fedbeb7b6e Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sun, 3 Jun 2007 05:35:06 +0000 Subject: unicode: Implemented string interpolation for lazy objects. git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5420 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/functional.py | 14 ++++++++++++-- django/utils/translation/__init__.py | 2 +- tests/regressiontests/i18n/__init__.py | 0 tests/regressiontests/i18n/models.py | 0 tests/regressiontests/i18n/tests.py | 17 +++++++++++++++++ 5 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/regressiontests/i18n/__init__.py create mode 100644 tests/regressiontests/i18n/models.py create mode 100644 tests/regressiontests/i18n/tests.py 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 diff --git a/tests/regressiontests/i18n/models.py b/tests/regressiontests/i18n/models.py new file mode 100644 index 0000000000..e69de29bb2 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() + +""" -- cgit v1.2.1