Fixed #14306 -- Cleaned up django.utils.translation module a bit to be quicker. Thanks for the report and initial patch, Anssi Kääriäinen.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13899 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-09-27 15:25:38 +00:00
parent 534792d055
commit bf8c93f2cf
2 changed files with 42 additions and 32 deletions

View File

@ -20,42 +20,48 @@ __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext',
# replace the functions with their real counterparts (once we do access the # replace the functions with their real counterparts (once we do access the
# settings). # settings).
def delayed_loader(real_name, *args, **kwargs): class Trans(object):
""" """
Call the real, underlying function. We have a level of indirection here so The purpose of this class is to store the actual translation function upon
that modules can use the translation bits without actually requiring receiving the first call to that function. After this is done, changes to
Django's settings bits to be configured before import. USE_I18N will have no effect to which function is served upon request. If
your tests rely on changing USE_I18N, you can delete all the functions
from _trans.__dict__.
Note that storing the function with setattr will have a noticeable
performance effect, as access to the function goes the normal path,
instead of using __getattr__.
""" """
def __getattr__(self, real_name):
from django.conf import settings from django.conf import settings
if settings.USE_I18N: if settings.USE_I18N:
from django.utils.translation import trans_real as trans from django.utils.translation import trans_real as trans
else: else:
from django.utils.translation import trans_null as trans from django.utils.translation import trans_null as trans
setattr(self, real_name, getattr(trans, real_name))
return getattr(trans, real_name)
# Make the originally requested function call on the way out the door. _trans = Trans()
return getattr(trans, real_name)(*args, **kwargs)
g = globals() # The Trans class is no more needed, so remove it from the namespace.
for name in __all__: del Trans
g['real_%s' % name] = curry(delayed_loader, name)
del g, delayed_loader
def gettext_noop(message): def gettext_noop(message):
return real_gettext_noop(message) return _trans.gettext_noop(message)
ugettext_noop = gettext_noop ugettext_noop = gettext_noop
def gettext(message): def gettext(message):
return real_gettext(message) return _trans.gettext(message)
def ngettext(singular, plural, number): def ngettext(singular, plural, number):
return real_ngettext(singular, plural, number) return _trans.ngettext(singular, plural, number)
def ugettext(message): def ugettext(message):
return real_ugettext(message) return _trans.ugettext(message)
def ungettext(singular, plural, number): def ungettext(singular, plural, number):
return real_ungettext(singular, plural, number) return _trans.ungettext(singular, plural, number)
ngettext_lazy = lazy(ngettext, str) ngettext_lazy = lazy(ngettext, str)
gettext_lazy = lazy(gettext, str) gettext_lazy = lazy(gettext, str)
@ -63,37 +69,37 @@ ungettext_lazy = lazy(ungettext, unicode)
ugettext_lazy = lazy(ugettext, unicode) ugettext_lazy = lazy(ugettext, unicode)
def activate(language): def activate(language):
return real_activate(language) return _trans.activate(language)
def deactivate(): def deactivate():
return real_deactivate() return _trans.deactivate()
def get_language(): def get_language():
return real_get_language() return _trans.get_language()
def get_language_bidi(): def get_language_bidi():
return real_get_language_bidi() return _trans.get_language_bidi()
def get_date_formats(): def get_date_formats():
return real_get_date_formats() return _trans.get_date_formats()
def get_partial_date_formats(): def get_partial_date_formats():
return real_get_partial_date_formats() return _trans.get_partial_date_formats()
def check_for_language(lang_code): def check_for_language(lang_code):
return real_check_for_language(lang_code) return _trans.check_for_language(lang_code)
def to_locale(language): def to_locale(language):
return real_to_locale(language) return _trans.to_locale(language)
def get_language_from_request(request): def get_language_from_request(request):
return real_get_language_from_request(request) return _trans.get_language_from_request(request)
def templatize(src): def templatize(src):
return real_templatize(src) return _trans.templatize(src)
def deactivate_all(): def deactivate_all():
return real_deactivate_all() return _trans.deactivate_all()
def _string_concat(*strings): def _string_concat(*strings):
""" """

View File

@ -80,10 +80,14 @@ class DjangoTranslation(gettext_module.GNUTranslations):
def set_language(self, language): def set_language(self, language):
self.__language = language self.__language = language
self.__to_language = to_language(language)
def language(self): def language(self):
return self.__language return self.__language
def to_language(self):
return self.__to_language
def __repr__(self): def __repr__(self):
return "<DjangoTranslation lang:%s>" % self.__language return "<DjangoTranslation lang:%s>" % self.__language
@ -214,7 +218,7 @@ def get_language():
t = _active.get(currentThread(), None) t = _active.get(currentThread(), None)
if t is not None: if t is not None:
try: try:
return to_language(t.language()) return t.to_language()
except AttributeError: except AttributeError:
pass pass
# If we don't have a real translation object, assume it's the default language. # If we don't have a real translation object, assume it's the default language.