Ticket #14306: translations_cleanup.diff

File translations_cleanup.diff, 5.7 KB (added by akaariai, 5 years ago)
  • django/utils/translation/__init__.py

    diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
    index 6d81726..dc76721 100644
    a b __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext', 
    2020# replace the functions with their real counterparts (once we do access the
    2121# settings).
    2222
    23 def delayed_loader(real_name, *args, **kwargs):
     23class TransProvider(object):
    2424    """
    25     Call the real, underlying function.  We have a level of indirection here so
    26     that modules can use the translation bits without actually requiring
    27     Django's settings bits to be configured before import.
     25    The purpose of this class is to store the actual translation function upon
     26    receiving the first call to that function. After this is done, changes to
     27    USE_I18N will have no effect to which function is served upon request. If
     28    your tests rely on changing USE_I18N, you can delete all the functions
     29    from _trans_provider.__dict__.
     30   
     31    Note that storing the function with setattr will have a noticeable
     32    performance effect, as access to the function goes the normal path,
     33    instead of using __getattr__.
    2834    """
    29     from django.conf import settings
    30     if settings.USE_I18N:
    31         from django.utils.translation import trans_real as trans
    32     else:
    33         from django.utils.translation import trans_null as trans
     35    def __getattr__(self, name):
     36        from django.conf import settings
     37        if settings.USE_I18N:
     38            from django.utils.translation import trans_real as trans_provider
     39        else:
     40            from django.utils.translation import trans_null as trans_provider
     41        setattr(self, name, getattr(trans_provider, name))
     42        return getattr(trans_provider, name)
    3443
    35     # Make the originally requested function call on the way out the door.
    36     return getattr(trans, real_name)(*args, **kwargs)
     44_trans_provider = TransProvider()
    3745
    38 g = globals()
    39 for name in __all__:
    40     g['real_%s' % name] = curry(delayed_loader, name)
    41 del g, delayed_loader
     46# The TransProvider class is no more needed, so remove it from the namespace.
     47del TransProvider
    4248
    4349def gettext_noop(message):
    44     return real_gettext_noop(message)
     50    global _trans_provider
     51    return _trans_provider.gettext_noop(message)
    4552
    4653ugettext_noop = gettext_noop
    4754
    4855def gettext(message):
    49     return real_gettext(message)
     56    global _trans_provider
     57    return _trans_provider.gettext(message)
    5058
    5159def ngettext(singular, plural, number):
    52     return real_ngettext(singular, plural, number)
     60    global _trans_provider
     61    return _trans_provider.ngettext(singular, plural, number)
    5362
    5463def ugettext(message):
    55     return real_ugettext(message)
     64    global _trans_provider
     65    return _trans_provider.ugettext(message)
    5666
    5767def ungettext(singular, plural, number):
    58     return real_ungettext(singular, plural, number)
     68    global _trans_provider
     69    return _trans_provider.ungettext(singular, plural, number)
    5970
    6071ngettext_lazy = lazy(ngettext, str)
    6172gettext_lazy = lazy(gettext, str)
    ungettext_lazy = lazy(ungettext, unicode) 
    6374ugettext_lazy = lazy(ugettext, unicode)
    6475
    6576def activate(language):
    66     return real_activate(language)
     77    global _trans_provider
     78    return _trans_provider.activate(language)
    6779
    6880def deactivate():
    69     return real_deactivate()
     81    global _trans_provider
     82    return _trans_provider.deactivate()
    7083
    7184def get_language():
    72     return real_get_language()
     85    global _trans_provider
     86    return _trans_provider.get_language()
    7387
    7488def get_language_bidi():
    75     return real_get_language_bidi()
     89    global _trans_provider
     90    return _trans_provider.get_language_bidi()
    7691
    7792def get_date_formats():
    78     return real_get_date_formats()
     93    global _trans_provider
     94    return _trans_provider.get_date_formats()
    7995
    8096def get_partial_date_formats():
    81     return real_get_partial_date_formats()
     97    global _trans_provider
     98    return _trans_provider.get_partial_date_formats()
    8299
    83100def check_for_language(lang_code):
    84     return real_check_for_language(lang_code)
     101    global _trans_provider
     102    return _trans_provider.check_for_language(lang_code)
    85103
    86104def to_locale(language):
    87     return real_to_locale(language)
     105    global _trans_provider
     106    return _trans_provider.to_locale(language)
    88107
    89108def get_language_from_request(request):
    90     return real_get_language_from_request(request)
     109    global _trans_provider
     110    return _trans_provider.get_language_from_request(request)
    91111
    92112def templatize(src):
    93     return real_templatize(src)
     113    global _trans_provider
     114    return _trans_provider.templatize(src)
    94115
    95116def deactivate_all():
    96     return real_deactivate_all()
     117    global _trans_provider
     118    return _trans_provider.deactivate_all()
    97119
    98120def _string_concat(*strings):
    99121    """
  • django/utils/translation/trans_real.py

    diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
    index 98b8d94..bd74f6d 100644
    a b class DjangoTranslation(gettext_module.GNUTranslations): 
    8080
    8181    def set_language(self, language):
    8282        self.__language = language
     83        self.__to_language = to_language(language)
    8384
    8485    def language(self):
    8586        return self.__language
     87   
     88    def to_language(self):
     89        return self.__to_language
    8690
    8791    def __repr__(self):
    8892        return "<DjangoTranslation lang:%s>" % self.__language
    def get_language(): 
    214218    t = _active.get(currentThread(), None)
    215219    if t is not None:
    216220        try:
    217             return to_language(t.language())
     221            return t.to_language()
    218222        except AttributeError:
    219223            pass
    220224    # If we don't have a real translation object, assume it's the default language.
Back to Top