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',
|
20 | 20 | # replace the functions with their real counterparts (once we do access the |
21 | 21 | # settings). |
22 | 22 | |
23 | | def delayed_loader(real_name, *args, **kwargs): |
| 23 | class TransProvider(object): |
24 | 24 | """ |
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__. |
28 | 34 | """ |
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) |
34 | 43 | |
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() |
37 | 45 | |
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. |
| 47 | del TransProvider |
42 | 48 | |
43 | 49 | def gettext_noop(message): |
44 | | return real_gettext_noop(message) |
| 50 | global _trans_provider |
| 51 | return _trans_provider.gettext_noop(message) |
45 | 52 | |
46 | 53 | ugettext_noop = gettext_noop |
47 | 54 | |
48 | 55 | def gettext(message): |
49 | | return real_gettext(message) |
| 56 | global _trans_provider |
| 57 | return _trans_provider.gettext(message) |
50 | 58 | |
51 | 59 | def ngettext(singular, plural, number): |
52 | | return real_ngettext(singular, plural, number) |
| 60 | global _trans_provider |
| 61 | return _trans_provider.ngettext(singular, plural, number) |
53 | 62 | |
54 | 63 | def ugettext(message): |
55 | | return real_ugettext(message) |
| 64 | global _trans_provider |
| 65 | return _trans_provider.ugettext(message) |
56 | 66 | |
57 | 67 | def ungettext(singular, plural, number): |
58 | | return real_ungettext(singular, plural, number) |
| 68 | global _trans_provider |
| 69 | return _trans_provider.ungettext(singular, plural, number) |
59 | 70 | |
60 | 71 | ngettext_lazy = lazy(ngettext, str) |
61 | 72 | gettext_lazy = lazy(gettext, str) |
… |
… |
ungettext_lazy = lazy(ungettext, unicode)
|
63 | 74 | ugettext_lazy = lazy(ugettext, unicode) |
64 | 75 | |
65 | 76 | def activate(language): |
66 | | return real_activate(language) |
| 77 | global _trans_provider |
| 78 | return _trans_provider.activate(language) |
67 | 79 | |
68 | 80 | def deactivate(): |
69 | | return real_deactivate() |
| 81 | global _trans_provider |
| 82 | return _trans_provider.deactivate() |
70 | 83 | |
71 | 84 | def get_language(): |
72 | | return real_get_language() |
| 85 | global _trans_provider |
| 86 | return _trans_provider.get_language() |
73 | 87 | |
74 | 88 | def get_language_bidi(): |
75 | | return real_get_language_bidi() |
| 89 | global _trans_provider |
| 90 | return _trans_provider.get_language_bidi() |
76 | 91 | |
77 | 92 | def get_date_formats(): |
78 | | return real_get_date_formats() |
| 93 | global _trans_provider |
| 94 | return _trans_provider.get_date_formats() |
79 | 95 | |
80 | 96 | def get_partial_date_formats(): |
81 | | return real_get_partial_date_formats() |
| 97 | global _trans_provider |
| 98 | return _trans_provider.get_partial_date_formats() |
82 | 99 | |
83 | 100 | def 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) |
85 | 103 | |
86 | 104 | def to_locale(language): |
87 | | return real_to_locale(language) |
| 105 | global _trans_provider |
| 106 | return _trans_provider.to_locale(language) |
88 | 107 | |
89 | 108 | def 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) |
91 | 111 | |
92 | 112 | def templatize(src): |
93 | | return real_templatize(src) |
| 113 | global _trans_provider |
| 114 | return _trans_provider.templatize(src) |
94 | 115 | |
95 | 116 | def deactivate_all(): |
96 | | return real_deactivate_all() |
| 117 | global _trans_provider |
| 118 | return _trans_provider.deactivate_all() |
97 | 119 | |
98 | 120 | def _string_concat(*strings): |
99 | 121 | """ |
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):
|
80 | 80 | |
81 | 81 | def set_language(self, language): |
82 | 82 | self.__language = language |
| 83 | self.__to_language = to_language(language) |
83 | 84 | |
84 | 85 | def language(self): |
85 | 86 | return self.__language |
| 87 | |
| 88 | def to_language(self): |
| 89 | return self.__to_language |
86 | 90 | |
87 | 91 | def __repr__(self): |
88 | 92 | return "<DjangoTranslation lang:%s>" % self.__language |
… |
… |
def get_language():
|
214 | 218 | t = _active.get(currentThread(), None) |
215 | 219 | if t is not None: |
216 | 220 | try: |
217 | | return to_language(t.language()) |
| 221 | return t.to_language() |
218 | 222 | except AttributeError: |
219 | 223 | pass |
220 | 224 | # If we don't have a real translation object, assume it's the default language. |