Ticket #12924: translation-wtf-party.diff

File translation-wtf-party.diff, 5.3 KB (added by Alex Gaynor, 9 years ago)

Initial version, seems to be working, fixes all teh tickets about pickling translatable strings as well.

  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 8329792..9235a67 100644
    a b signals.class_prepared.connect(do_pending_lookups) 
    8787class RelatedField(object):
    8888    def contribute_to_class(self, cls, name):
    8989        sup = super(RelatedField, self)
    90 
    91         # Add an accessor to allow easy determination of the related query path for this field
    92         self.related_query_name = curry(self._get_related_query_name, cls._meta)
     90       
     91        # Store the opts for related_query_name()
     92        self.opts = cls._meta
    9393
    9494        if hasattr(sup, 'contribute_to_class'):
    9595            sup.contribute_to_class(cls, name)
    class RelatedField(object): 
    174174            return []
    175175        raise TypeError("Related Field has invalid lookup: %s" % lookup_type)
    176176
    177     def _get_related_query_name(self, opts):
     177    def related_query_name(self):
    178178        # This method defines the name that can be used to identify this
    179179        # related object in a table-spanning query. It uses the lower-cased
    180180        # object_name by default, but this can be overridden with the
    181181        # "related_name" option.
    182         return self.rel.related_name or opts.object_name.lower()
     182        return self.rel.related_name or self.opts.object_name.lower()
    183183
    184184class SingleRelatedObjectDescriptor(object):
    185185    # This class provides the functionality that makes the related-object
  • django/utils/functional.py

    diff --git a/django/utils/functional.py b/django/utils/functional.py
    index e52ab76..6482e44 100644
    a b def lazy(func, *resultclasses): 
    147147    the lazy evaluation code is triggered. Results are not memoized; the
    148148    function is evaluated on every access.
    149149    """
     150    if len(resultclasses) == 1 and isinstance(resultclasses[0], tuple):
     151        resultclasses = resultclasses[0]
     152   
    150153    class __proxy__(Promise):
    151154        """
    152155        Encapsulate a function call and act as a proxy for methods that are
    def lazy(func, *resultclasses): 
    161164            self.__kw = kw
    162165            if self.__dispatch is None:
    163166                self.__prepare_class__()
    164 
     167       
     168        def __reduce_ex__(self, protocol):
     169            return (lazy, (self.__func, resultclasses), self.__dict__)
     170       
    165171        def __prepare_class__(cls):
    166172            cls.__dispatch = {}
    167173            for resultclass in resultclasses:
  • django/utils/translation/__init__.py

    diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
    index c0a0df9..da1c938 100644
    a b  
    11"""
    22Internationalization support.
    33"""
    4 from django.utils.functional import lazy
     4from django.utils.functional import lazy, curry
    55from django.utils.encoding import force_unicode
    66
    77__all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext',
    __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext', 
    1919# replace the functions with their real counterparts (once we do access the
    2020# settings).
    2121
    22 def delayed_loader(*args, **kwargs):
     22def delayed_loader(real_name, *args, **kwargs):
    2323    """
    2424    Replace each real_* function with the corresponding function from either
    2525    trans_real or trans_null (e.g. real_gettext is replaced with
    def delayed_loader(*args, **kwargs): 
    2727    first time any i18n method is called. It replaces all the i18n methods at
    2828    once at that time.
    2929    """
    30     import traceback
    3130    from django.conf import settings
     31
    3232    if settings.USE_I18N:
    33         import trans_real as trans
     33        import django.utils.translation.trans_real as trans
    3434    else:
    35         import trans_null as trans
    36     caller = traceback.extract_stack(limit=2)[0][2]
    37     g = globals()
    38     for name in __all__:
    39         if hasattr(trans, name):
    40             g['real_%s' % name] = getattr(trans, name)
     35        import django.utils.translation.trans_null as trans
    4136
    4237    # Make the originally requested function call on the way out the door.
    43     return g['real_%s' % caller](*args, **kwargs)
     38    return getattr(trans, real_name)(*args, **kwargs)
    4439
    4540g = globals()
    4641for name in __all__:
    47     g['real_%s' % name] = delayed_loader
     42    g['real_%s' % name] = curry(delayed_loader, name)
    4843del g, delayed_loader
    4944
    5045def gettext_noop(message):
    def templatize(src): 
    10297def deactivate_all():
    10398    return real_deactivate_all()
    10499
    105 def string_concat(*strings):
     100def _string_concat(*strings):
    106101    """
    107102    Lazy variant of string concatenation, needed for translations that are
    108103    constructed from multiple parts.
    109104    """
    110105    return u''.join([force_unicode(s) for s in strings])
    111 string_concat = lazy(string_concat, unicode)
     106string_concat = lazy(_string_concat, unicode)
  • tests/regressiontests/i18n/tests.py

    diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
    index 31150a6..941f66f 100644
    a b class TranslationTests(TestCase): 
    4646        unicode(string_concat(...)) should not raise a TypeError - #4796
    4747        """
    4848        import django.utils.translation
    49         self.assertEqual(django.utils.translation, reload(django.utils.translation))
    5049        self.assertEqual(u'django', unicode(django.utils.translation.string_concat("dja", "ngo")))
    5150
    5251    def test_safe_status(self):
Back to Top