Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Delayed the reading of settings.USE_I18N until the first use of the i18n

functions. This solves a few import problems we are seeing. Fixed #3687. Refs #2920.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@4905 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 286e676231c0de9eff97d23c1132c833d346a887 1 parent 4a3db28
Malcolm Tredinnick authored
98  django/utils/translation/__init__.py
... ...
@@ -1,8 +1,94 @@
1  
-from django.conf import settings
  1
+"""
  2
+Internationalization support.
  3
+"""
  4
+from django.utils.functional import lazy
2 5
 
3  
-if settings.USE_I18N:
4  
-    from trans_real import *
5  
-else:
6  
-    from trans_null import *
  6
+__all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext',
  7
+        'ngettext_lazy', 'string_concat', 'activate', 'deactivate',
  8
+        'get_language', 'get_language_bidi', 'get_date_formats',
  9
+        'get_partial_date_formats', 'check_for_language', 'to_locale',
  10
+        'get_language_from_request', 'install']
  11
+
  12
+# Here be dragons, so a short explanation of the logic won't hurt:
  13
+# We are trying to solve two problems: (1) access settings, in particular
  14
+# settings.USE_I18N, as late as possible, so that modules can be imported
  15
+# without having to first configure Django, and (2) if some other code creates
  16
+# a reference to one of these functions, don't break that reference when we
  17
+# replace the functions with their real counterparts (once we do access the
  18
+# settings).
  19
+
  20
+def delayed_loader(*args, **kwargs):
  21
+    """
  22
+    Replace each real_* function with the corresponding function from either
  23
+    trans_real or trans_null (e.g. real_gettext is replaced with
  24
+    trans_real.gettext or trans_null.gettext). This function is run once, the
  25
+    first time any i18n method is called. It replaces all the i18n methods at
  26
+    once at that time.
  27
+    """
  28
+    import traceback
  29
+    from django.conf import settings
  30
+    if settings.USE_I18N:
  31
+        import trans_real as trans
  32
+    else:
  33
+        import trans_null as trans
  34
+    caller = traceback.extract_stack(limit=2)[0][2]
  35
+    g = globals()
  36
+    for name in __all__:
  37
+        if hasattr(trans, name):
  38
+            g['real_%s' % name] = getattr(trans, name)
  39
+
  40
+    # Make the originally requested function call on the way out the door.
  41
+    return g[caller](*args, **kwargs)
  42
+
  43
+g = globals()
  44
+for name in __all__:
  45
+    g['real_%s' % name] = delayed_loader
  46
+del g, delayed_loader
  47
+
  48
+def gettext_noop(message):
  49
+    return real_gettext_noop(message)
  50
+
  51
+def gettext(message):
  52
+    return real_gettext(message)
  53
+
  54
+
  55
+def ngettext(singular, plural, number):
  56
+    return real_ngettext(singular, plural, number)
  57
+
  58
+def string_concat(*strings):
  59
+    return real_string_concat(*strings)
  60
+
  61
+ngettext_lazy = lazy(ngettext, str, unicode)
  62
+gettext_lazy = lazy(gettext, str, unicode)
  63
+string_concat = lazy(string_concat, str, unicode)
  64
+
  65
+def activate(language):
  66
+    return real_activate(language)
  67
+
  68
+def deactivate():
  69
+    return real_deactivate()
  70
+
  71
+def get_language():
  72
+    return real_get_language()
  73
+
  74
+def get_language_bidi():
  75
+    return real_get_language_bidi()
  76
+
  77
+def get_date_formats():
  78
+    return real_get_date_formats()
  79
+
  80
+def get_partial_date_formats():
  81
+    return real_get_partial_date_formats()
  82
+
  83
+def check_for_language(lang_code):
  84
+    return real_check_for_language(lang_code)
  85
+
  86
+def to_locale(language):
  87
+    return real_to_locale(language)
  88
+
  89
+def get_language_from_request(request):
  90
+    return real_get_language_from_request(request)
  91
+
  92
+def install():
  93
+    return real_install()
7 94
 
8  
-del settings
5  django/utils/translation/trans_real.py
@@ -3,7 +3,6 @@
3 3
 import os, re, sys
4 4
 import gettext as gettext_module
5 5
 from cStringIO import StringIO
6  
-from django.utils.functional import lazy
7 6
 
8 7
 try:
9 8
     import threading
@@ -277,9 +276,6 @@ def ngettext(singular, plural, number):
277 276
         _default = translation(settings.LANGUAGE_CODE)
278 277
     return _default.ngettext(singular, plural, number)
279 278
 
280  
-gettext_lazy = lazy(gettext, str)
281  
-ngettext_lazy = lazy(ngettext, str)
282  
-
283 279
 def check_for_language(lang_code):
284 280
     """
285 281
     Checks whether there is a global language file for the given language code.
@@ -493,4 +489,3 @@ def string_concat(*strings):
493 489
     """
494 490
     return ''.join([str(el) for el in strings])
495 491
 
496  
-string_concat = lazy(string_concat, str)

0 notes on commit 286e676

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