| 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'] |
|---|
| 8 | | del settings |
|---|
| | 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() |
|---|
| | 94 | |
|---|