From bf8c93f2cf4a0b020760f1ad059e9d1e053e48ad Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Mon, 27 Sep 2010 15:25:38 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#14306=20--=20Cleaned=20up=20django.uti?= =?UTF-8?q?ls.translation=20module=20a=20bit=20to=20be=20quicker.=20Thanks?= =?UTF-8?q?=20for=20the=20report=20and=20initial=20patch,=20Anssi=20K?= =?UTF-8?q?=C3=A4=C3=A4ri=C3=A4inen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@13899 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/translation/__init__.py | 68 ++++++++++++++------------ django/utils/translation/trans_real.py | 6 ++- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index 6d81726a4e..4477c291f4 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -20,42 +20,48 @@ __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext', # replace the functions with their real counterparts (once we do access the # settings). -def delayed_loader(real_name, *args, **kwargs): +class Trans(object): """ - Call the real, underlying function. We have a level of indirection here so - that modules can use the translation bits without actually requiring - Django's settings bits to be configured before import. + The purpose of this class is to store the actual translation function upon + receiving the first call to that function. After this is done, changes to + 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__. """ - from django.conf import settings - if settings.USE_I18N: - from django.utils.translation import trans_real as trans - else: - from django.utils.translation import trans_null as trans + def __getattr__(self, real_name): + from django.conf import settings + if settings.USE_I18N: + from django.utils.translation import trans_real as trans + else: + 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. - return getattr(trans, real_name)(*args, **kwargs) +_trans = Trans() -g = globals() -for name in __all__: - g['real_%s' % name] = curry(delayed_loader, name) -del g, delayed_loader +# The Trans class is no more needed, so remove it from the namespace. +del Trans def gettext_noop(message): - return real_gettext_noop(message) + return _trans.gettext_noop(message) ugettext_noop = gettext_noop def gettext(message): - return real_gettext(message) + return _trans.gettext(message) def ngettext(singular, plural, number): - return real_ngettext(singular, plural, number) + return _trans.ngettext(singular, plural, number) def ugettext(message): - return real_ugettext(message) + return _trans.ugettext(message) def ungettext(singular, plural, number): - return real_ungettext(singular, plural, number) + return _trans.ungettext(singular, plural, number) ngettext_lazy = lazy(ngettext, str) gettext_lazy = lazy(gettext, str) @@ -63,37 +69,37 @@ ungettext_lazy = lazy(ungettext, unicode) ugettext_lazy = lazy(ugettext, unicode) def activate(language): - return real_activate(language) + return _trans.activate(language) def deactivate(): - return real_deactivate() + return _trans.deactivate() def get_language(): - return real_get_language() + return _trans.get_language() def get_language_bidi(): - return real_get_language_bidi() + return _trans.get_language_bidi() def get_date_formats(): - return real_get_date_formats() + return _trans.get_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): - return real_check_for_language(lang_code) + return _trans.check_for_language(lang_code) def to_locale(language): - return real_to_locale(language) + return _trans.to_locale(language) def get_language_from_request(request): - return real_get_language_from_request(request) + return _trans.get_language_from_request(request) def templatize(src): - return real_templatize(src) + return _trans.templatize(src) def deactivate_all(): - return real_deactivate_all() + return _trans.deactivate_all() def _string_concat(*strings): """ diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index 98b8d940b3..75f6e202fa 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -80,10 +80,14 @@ class DjangoTranslation(gettext_module.GNUTranslations): def set_language(self, language): self.__language = language + self.__to_language = to_language(language) def language(self): return self.__language + def to_language(self): + return self.__to_language + def __repr__(self): return "" % self.__language @@ -214,7 +218,7 @@ def get_language(): t = _active.get(currentThread(), None) if t is not None: try: - return to_language(t.language()) + return t.to_language() except AttributeError: pass # If we don't have a real translation object, assume it's the default language.