diff --git a/django/utils/formats.py b/django/utils/formats.py
index 372998f..1c5f381 100644
a
|
b
|
from django.utils.importlib import import_module
|
7 | 7 | from django.utils.encoding import smart_str |
8 | 8 | from django.utils import dateformat, numberformat, datetime_safe |
9 | 9 | |
10 | | def get_format_modules(reverse=False): |
| 10 | # format_cache is a mapping from format_type to a 1-tuple. This allows |
| 11 | # us to separate 3 distinct cases: |
| 12 | # (None,) the format_type was checked previously, but |
| 13 | # no value was found. |
| 14 | # (<format value>, ) the previously fetched value for format_type |
| 15 | # is format_value |
| 16 | # if a format_type key is not in _format_cache, then format_type |
| 17 | # hasn't yet been fetched. |
| 18 | _format_cache = {} |
| 19 | |
| 20 | def get_format_modules(reverse=False, lang=None): |
11 | 21 | """ |
12 | 22 | Returns an iterator over the format modules found in the project and Django |
13 | 23 | """ |
| 24 | if lang is None: |
| 25 | lang = get_language() |
14 | 26 | modules = [] |
15 | | if not check_for_language(get_language()) or not settings.USE_L10N: |
| 27 | if not check_for_language(lang) or not settings.USE_L10N: |
16 | 28 | return modules |
17 | | locale = to_locale(get_language()) |
| 29 | locale = to_locale(lang) |
18 | 30 | if settings.FORMAT_MODULE_PATH: |
19 | 31 | format_locations = [settings.FORMAT_MODULE_PATH + '.%s'] |
20 | 32 | else: |
… |
… |
def get_format_modules(reverse=False):
|
34 | 46 | modules.reverse() |
35 | 47 | return modules |
36 | 48 | |
37 | | def get_format(format_type): |
| 49 | _use_l10n = None |
| 50 | def get_format(format_type, lang=None): |
38 | 51 | """ |
39 | 52 | For a specific format type, returns the format for the current |
40 | 53 | language (locale), defaults to the format in the settings. |
41 | 54 | format_type is the name of the format, e.g. 'DATE_FORMAT' |
42 | 55 | """ |
43 | | format_type = smart_str(format_type) |
44 | | if settings.USE_L10N: |
45 | | for module in get_format_modules(): |
46 | | try: |
47 | | return getattr(module, format_type) |
48 | | except AttributeError: |
| 56 | # format_type = smart_str(format_type) |
| 57 | global _format_cache |
| 58 | global _use_l10n |
| 59 | if _use_l10n is None: |
| 60 | _use_l10n = settings.USE_L10N |
| 61 | |
| 62 | if _use_l10n: |
| 63 | if lang is None: |
| 64 | lang = get_language() |
| 65 | cached_value = _format_cache.get((format_type, lang), None) |
| 66 | if cached_value: |
| 67 | if cached_value[0]: |
| 68 | return cached_value[0] |
| 69 | else: |
| 70 | # format_type was previously checked, but didn't have a value |
49 | 71 | pass |
50 | | return getattr(settings, format_type) |
| 72 | else: |
| 73 | for module in get_format_modules(lang=lang): |
| 74 | try: |
| 75 | val = getattr(module, format_type) |
| 76 | _format_cache[(format_type, lang)] = (val,) |
| 77 | return val |
| 78 | except AttributeError: |
| 79 | pass |
| 80 | _format_cache[(format_type, lang)] = (None,) |
| 81 | cached_value = _format_cache.get(format_type) |
| 82 | if not cached_value: |
| 83 | cached_value = (getattr(settings, format_type),) |
| 84 | _format_cache[format_type] = cached_value |
| 85 | return cached_value[0] |
51 | 86 | |
52 | 87 | def date_format(value, format=None): |
53 | 88 | """ |
… |
… |
def number_format(value, decimal_pos=None):
|
66 | 101 | """ |
67 | 102 | Formats a numeric value using localization settings |
68 | 103 | """ |
| 104 | lang = get_language() |
69 | 105 | return numberformat.format( |
70 | 106 | value, |
71 | | get_format('DECIMAL_SEPARATOR'), |
| 107 | get_format('DECIMAL_SEPARATOR', lang=lang), |
72 | 108 | decimal_pos, |
73 | | get_format('NUMBER_GROUPING'), |
74 | | get_format('THOUSAND_SEPARATOR'), |
| 109 | get_format('NUMBER_GROUPING', lang=lang), |
| 110 | get_format('THOUSAND_SEPARATOR', lang=lang), |
75 | 111 | ) |
76 | 112 | |
77 | 113 | def localize(value): |
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
index 6d81726..7d8eec3 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 | _trans = None |
| 24 | |
23 | 25 | def delayed_loader(real_name, *args, **kwargs): |
24 | 26 | """ |
25 | 27 | Call the real, underlying function. We have a level of indirection here so |
26 | 28 | that modules can use the translation bits without actually requiring |
27 | 29 | Django's settings bits to be configured before import. |
28 | 30 | """ |
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 |
34 | | |
| 31 | global _trans |
| 32 | if not _trans: |
| 33 | from django.conf import settings |
| 34 | if settings.USE_I18N: |
| 35 | from django.utils.translation import trans_real as trans |
| 36 | else: |
| 37 | from django.utils.translation import trans_null as trans |
| 38 | _trans = trans |
35 | 39 | # Make the originally requested function call on the way out the door. |
36 | | return getattr(trans, real_name)(*args, **kwargs) |
| 40 | return getattr(_trans, real_name)(*args, **kwargs) |
37 | 41 | |
38 | 42 | g = globals() |
39 | 43 | for name in __all__: |