Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Fixed #14306 -- Cleaned up django.utils.translation module a bit to b…

…e quicker. Thanks for the report and initial patch, Anssi Kääriäinen.

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 534792d commit bf8c93f2cf4a0b020760f1ad059e9d1e053e48ad @jezdez jezdez committed
68 django/utils/translation/
@@ -20,42 +20,48 @@
# 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 @@ def ungettext(singular, plural, number):
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):
6 django/utils/translation/
@@ -80,10 +80,14 @@ def merge(self, other):
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 "<DjangoTranslation lang:%s>" % self.__language
@@ -214,7 +218,7 @@ def get_language():
t = _active.get(currentThread(), None)
if t is not None:
- return to_language(t.language())
+ return t.to_language()
except AttributeError:
# If we don't have a real translation object, assume it's the default language.

0 comments on commit bf8c93f

Please sign in to comment.
Something went wrong with that request. Please try again.