1 """ |
1 """ |
2 Internationalization support. |
2 Internationalization support. |
3 """ |
3 """ |
4 from django.utils.functional import lazy |
|
5 from django.utils.encoding import force_unicode |
4 from django.utils.encoding import force_unicode |
|
5 from django.utils.functional import lazy, curry |
|
6 |
6 |
7 |
7 __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext', |
8 __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext', |
8 'ngettext_lazy', 'string_concat', 'activate', 'deactivate', |
9 'ngettext_lazy', 'string_concat', 'activate', 'deactivate', |
9 'get_language', 'get_language_bidi', 'get_date_formats', |
10 'get_language', 'get_language_bidi', 'get_date_formats', |
10 'get_partial_date_formats', 'check_for_language', 'to_locale', |
11 'get_partial_date_formats', 'check_for_language', 'to_locale', |
17 # without having to first configure Django, and (2) if some other code creates |
18 # without having to first configure Django, and (2) if some other code creates |
18 # a reference to one of these functions, don't break that reference when we |
19 # a reference to one of these functions, don't break that reference when we |
19 # replace the functions with their real counterparts (once we do access the |
20 # replace the functions with their real counterparts (once we do access the |
20 # settings). |
21 # settings). |
21 |
22 |
22 def delayed_loader(*args, **kwargs): |
23 def delayed_loader(real_name, *args, **kwargs): |
23 """ |
24 """ |
24 Replace each real_* function with the corresponding function from either |
25 Call the real, underlying function. We have a level of indirection here so |
25 trans_real or trans_null (e.g. real_gettext is replaced with |
26 that modules can use the translation bits without actually requiring |
26 trans_real.gettext or trans_null.gettext). This function is run once, the |
27 Django's settings bits to be configured before import. |
27 first time any i18n method is called. It replaces all the i18n methods at |
|
28 once at that time. |
|
29 """ |
28 """ |
30 import traceback |
|
31 from django.conf import settings |
29 from django.conf import settings |
32 if settings.USE_I18N: |
30 if settings.USE_I18N: |
33 import trans_real as trans |
31 from django.utils.translation import trans_real as trans |
34 else: |
32 else: |
35 import trans_null as trans |
33 from django.utils.translation import trans_null as trans |
36 caller = traceback.extract_stack(limit=2)[0][2] |
|
37 g = globals() |
|
38 for name in __all__: |
|
39 if hasattr(trans, name): |
|
40 g['real_%s' % name] = getattr(trans, name) |
|
41 |
34 |
42 # Make the originally requested function call on the way out the door. |
35 # Make the originally requested function call on the way out the door. |
43 return g['real_%s' % caller](*args, **kwargs) |
36 return getattr(trans, real_name)(*args, **kwargs) |
44 |
37 |
45 g = globals() |
38 g = globals() |
46 for name in __all__: |
39 for name in __all__: |
47 g['real_%s' % name] = delayed_loader |
40 g['real_%s' % name] = curry(delayed_loader, name) |
48 del g, delayed_loader |
41 del g, delayed_loader |
49 |
42 |
50 def gettext_noop(message): |
43 def gettext_noop(message): |
51 return real_gettext_noop(message) |
44 return real_gettext_noop(message) |
52 |
45 |
100 return real_templatize(src) |
93 return real_templatize(src) |
101 |
94 |
102 def deactivate_all(): |
95 def deactivate_all(): |
103 return real_deactivate_all() |
96 return real_deactivate_all() |
104 |
97 |
105 def string_concat(*strings): |
98 def _string_concat(*strings): |
106 """ |
99 """ |
107 Lazy variant of string concatenation, needed for translations that are |
100 Lazy variant of string concatenation, needed for translations that are |
108 constructed from multiple parts. |
101 constructed from multiple parts. |
109 """ |
102 """ |
110 return u''.join([force_unicode(s) for s in strings]) |
103 return u''.join([force_unicode(s) for s in strings]) |
111 string_concat = lazy(string_concat, unicode) |
104 string_concat = lazy(_string_concat, unicode) |