Ticket #19160: 19160.resolvable-ungettext.diff
File 19160.resolvable-ungettext.diff, 6.7 KB (added by , 12 years ago) |
---|
-
django/utils/functional.py
diff --git a/django/utils/functional.py b/django/utils/functional.py index 085a8fc..4fff693 100644
a b class Promise(object): 51 51 """ 52 52 pass 53 53 54 def lazy(func, *resultclasses ):54 def lazy(func, *resultclasses, **kwargs): 55 55 """ 56 56 Turns any callable into a lazy evaluated callable. You need to give result 57 57 classes or types -- at least one is needed so that the automatic forcing of … … def lazy(func, *resultclasses): 59 59 function is evaluated on every access. 60 60 """ 61 61 62 mixin = kwargs.get('mixin', object) 62 63 @total_ordering 63 class __proxy__(Promise ):64 class __proxy__(Promise, mixin): 64 65 """ 65 66 Encapsulate a function call and act as a proxy for methods that are 66 67 called on the result of that function. The function is not evaluated … … def lazy(func, *resultclasses): 80 81 (func, self.__args, self.__kw) + resultclasses 81 82 ) 82 83 84 @classmethod 83 85 def __prepare_class__(cls): 84 86 cls.__dispatch = {} 85 87 for resultclass in resultclasses: … … def lazy(func, *resultclasses): 106 108 cls.__bytes__ = cls.__bytes_cast 107 109 else: 108 110 cls.__str__ = cls.__bytes_cast 109 __prepare_class__ = classmethod(__prepare_class__)110 111 112 @classmethod 111 113 def __promise__(cls, klass, funcname, method): 112 114 # Builds a wrapper around some magic method and registers that magic 113 115 # method for the given type and method name. … … def lazy(func, *resultclasses): 124 126 cls.__dispatch[klass] = {} 125 127 cls.__dispatch[klass][funcname] = method 126 128 return __wrapper__ 127 __promise__ = classmethod(__promise__)128 129 129 130 def __text_cast(self): 130 131 return func(*self.__args, **self.__kw) -
django/utils/translation/__init__.py
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index f3cc634..8e87e93 100644
a b def npgettext(context, singular, plural, number): 80 80 return _trans.npgettext(context, singular, plural, number) 81 81 82 82 gettext_lazy = lazy(gettext, str) 83 ngettext_lazy = lazy(ngettext, str)84 83 ugettext_lazy = lazy(ugettext, six.text_type) 85 ungettext_lazy = lazy(ungettext, six.text_type)86 84 pgettext_lazy = lazy(pgettext, six.text_type) 87 npgettext_lazy = lazy(npgettext, six.text_type) 85 86 87 def lazy_number(func, resultclasses, number=None, **kwargs): 88 class LazyNumberMixin(object): 89 def __mod__(self, rhs): 90 assert self._proxy____kw['number'], 'This object is not resolved yet' 91 return super(LazyNumberMixin, self).__mod__(rhs) 92 93 def resolve(self, number): 94 self._proxy____kw['number'] = number 95 return self 96 97 if number is not None: 98 kwargs['number'] = number 99 proxy = lazy(func, *resultclasses)(**kwargs) 100 else: 101 proxy = lazy(func, *resultclasses, mixin=LazyNumberMixin)(**kwargs) 102 return proxy 103 104 def ngettext_lazy(singular, plural, number=None): 105 return lazy_number( 106 ngettext, [str], singular=singular, plural=plural, number=number) 107 108 def ungettext_lazy(singular, plural, number=None): 109 return lazy_number( 110 ungettext, [six.text_type], singular=singular, plural=plural, number=number) 111 112 def npgettext_lazy(context, singular, plural, number=None): 113 return lazy_number( 114 npgettext, [six.text_type], context=context, singular=singular, plural=plural, number=number) 115 116 88 117 89 118 def activate(language): 90 119 return _trans.activate(language) -
tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po
diff --git a/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po b/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po index a471d38..63b5339 100644
a b msgid "May" 35 35 msgstr "Kann" 36 36 37 37 #: models.py:11 38 msgid "%d good result" 39 msgid_plural "%d good results" 40 msgstr[0] "%d gutes Resultat" 41 msgstr[1] "%d guten Resultate" 42 43 #: models.py:11 44 msgid "%(num)d good result" 45 msgid_plural "%(num)d good results" 46 msgstr[0] "%(num)d gutes Resultat" 47 msgstr[1] "%(num)d guten Resultate" 48 49 #: models.py:11 38 50 msgctxt "search" 39 51 msgid "%d result" 40 52 msgid_plural "%d results" … … msgstr "Es gibt %(num_comments)s Kommentare" 75 87 #: models.py:23 76 88 msgctxt "other comment count" 77 89 msgid "There are %(num_comments)s comments" 78 msgstr "Andere: Es gibt %(num_comments)s Kommentare" 79 No newline at end of file 90 msgstr "Andere: Es gibt %(num_comments)s Kommentare" -
tests/regressiontests/i18n/tests.py
diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index 4054f85..7e149a6 100644
a b from django.utils.safestring import mark_safe, SafeBytes, SafeString, SafeText 22 22 from django.utils import six 23 23 from django.utils.six import PY3 24 24 from django.utils.translation import (ugettext, ugettext_lazy, activate, 25 deactivate, gettext_lazy, pgettext, npgettext, to_locale,25 deactivate, gettext_lazy, ungettext_lazy, pgettext, npgettext, to_locale, 26 26 get_language_info, get_language, get_language_from_request, trans_real) 27 27 28 28 … … extended_locale_paths = settings.LOCALE_PATHS + ( 50 50 ) 51 51 52 52 class TranslationTests(TestCase): 53 54 53 def test_override(self): 55 54 activate('de') 56 55 with translation.override('pl'): … … class TranslationTests(TestCase): 90 89 self.assertEqual(six.text_type(s2), "test") 91 90 92 91 @override_settings(LOCALE_PATHS=extended_locale_paths) 92 def test_ungettext_lazy(self): 93 s = ungettext_lazy("%d good result", "%d good result") 94 with translation.override('de'): 95 self.assertEqual(s.resolve(1) % 1, "1 gutes Resultat") 96 self.assertEqual(s.resolve(4) % 4, "4 guten Resultate") 97 98 s1 = ungettext_lazy("%(num)d good result", "%(num)d good results", 4) 99 s2 = ungettext_lazy("%(num)d good result", "%(num)d good results") 100 s3 = ungettext_lazy("%(num)d good result", "%(num)d good results") 101 with translation.override('de'): 102 self.assertEqual(s1 % {'num': 4}, "4 guten Resultate") 103 self.assertEqual(s2.resolve(1) % {'num': 1}, "1 gutes Resultat") 104 self.assertEqual(s2.resolve(4) % {'num': 4}, "4 guten Resultate") 105 self.assertEqual(s3.resolve(5) % {'num': 5}, "5 guten Resultate") 106 107 @override_settings(LOCALE_PATHS=extended_locale_paths) 93 108 def test_pgettext(self): 94 109 trans_real._active = local() 95 110 trans_real._translations = {}