diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
index 1500a05..3d1bff4 100644
a
|
b
|
def date(value, arg=None):
|
706 | 706 | arg = settings.DATE_FORMAT |
707 | 707 | try: |
708 | 708 | return formats.date_format(value, arg) |
709 | | except AttributeError: |
| 709 | except (AttributeError, UnicodeEncodeError): |
710 | 710 | try: |
711 | 711 | return format(value, arg) |
712 | 712 | except AttributeError: |
… |
… |
def time(value, arg=None):
|
722 | 722 | arg = settings.TIME_FORMAT |
723 | 723 | try: |
724 | 724 | return formats.time_format(value, arg) |
725 | | except AttributeError: |
| 725 | except (AttributeError, UnicodeEncodeError): |
726 | 726 | try: |
727 | 727 | return dateformat.time_format(value, arg) |
728 | 728 | except AttributeError: |
diff --git a/django/utils/formats.py b/django/utils/formats.py
index 372998f..600c5cc 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 | # format_cache is a mapping from (format_type, lang) to the format string. |
| 11 | # By using the cache, it is possible to avoid running get_format_modules |
| 12 | # repeatedly. |
| 13 | _format_cache = {} |
| 14 | |
10 | 15 | def get_format_modules(reverse=False): |
11 | 16 | """ |
12 | 17 | Returns an iterator over the format modules found in the project and Django |
13 | 18 | """ |
| 19 | lang = get_language() |
14 | 20 | modules = [] |
15 | | if not check_for_language(get_language()) or not settings.USE_L10N: |
| 21 | if not check_for_language(lang) or not settings.USE_L10N: |
16 | 22 | return modules |
17 | | locale = to_locale(get_language()) |
| 23 | locale = to_locale(lang) |
18 | 24 | if settings.FORMAT_MODULE_PATH: |
19 | 25 | format_locations = [settings.FORMAT_MODULE_PATH + '.%s'] |
20 | 26 | else: |
… |
… |
def get_format(format_type):
|
40 | 46 | language (locale), defaults to the format in the settings. |
41 | 47 | format_type is the name of the format, e.g. 'DATE_FORMAT' |
42 | 48 | """ |
43 | | format_type = smart_str(format_type) |
| 49 | global _format_cache |
44 | 50 | if settings.USE_L10N: |
45 | | for module in get_format_modules(): |
46 | | try: |
47 | | return getattr(module, format_type) |
48 | | except AttributeError: |
49 | | pass |
| 51 | lang = get_language() |
| 52 | try: |
| 53 | return _format_cache[(format_type, lang)] \ |
| 54 | or getattr(settings, format_type) |
| 55 | except KeyError: |
| 56 | for module in get_format_modules(): |
| 57 | try: |
| 58 | val = getattr(module, format_type) |
| 59 | _format_cache[(format_type, lang)] = val |
| 60 | return val |
| 61 | except AttributeError: |
| 62 | pass |
| 63 | _format_cache[(format_type, lang)] = None |
50 | 64 | return getattr(settings, format_type) |
51 | 65 | |
52 | 66 | def date_format(value, format=None): |
diff --git a/django/utils/numberformat.py b/django/utils/numberformat.py
index 129c27f..d6dbef8 100644
a
|
b
|
|
1 | 1 | from django.conf import settings |
| 2 | from django.utils.safestring import mark_safe |
| 3 | |
2 | 4 | |
3 | 5 | def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''): |
4 | 6 | """ |
… |
… |
def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''):
|
11 | 13 | * thousand_sep: Thousand separator symbol (for example ",") |
12 | 14 | |
13 | 15 | """ |
| 16 | use_grouping = settings.USE_L10N and settings.USE_THOUSAND_SEPARATOR \ |
| 17 | and grouping |
| 18 | # Make the common case fast: |
| 19 | if isinstance(number, int) and not use_grouping and not decimal_pos: |
| 20 | return mark_safe(unicode(number)) |
14 | 21 | # sign |
15 | 22 | if float(number) < 0: |
16 | 23 | sign = '-' |
17 | 24 | else: |
18 | 25 | sign = '' |
19 | | # decimal part |
20 | 26 | str_number = unicode(number) |
21 | 27 | if str_number[0] == '-': |
22 | 28 | str_number = str_number[1:] |
| 29 | # decimal part |
23 | 30 | if '.' in str_number: |
24 | 31 | int_part, dec_part = str_number.split('.') |
25 | 32 | if decimal_pos: |
… |
… |
def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''):
|
30 | 37 | dec_part = dec_part + ('0' * (decimal_pos - len(dec_part))) |
31 | 38 | if dec_part: dec_part = decimal_sep + dec_part |
32 | 39 | # grouping |
33 | | if settings.USE_L10N and settings.USE_THOUSAND_SEPARATOR and grouping: |
| 40 | if use_grouping: |
34 | 41 | int_part_gd = '' |
35 | 42 | for cnt, digit in enumerate(int_part[::-1]): |
36 | 43 | if cnt and not cnt % grouping: |
37 | 44 | int_part_gd += thousand_sep |
38 | 45 | int_part_gd += digit |
39 | 46 | int_part = int_part_gd[::-1] |
40 | | |
41 | 47 | return sign + int_part + dec_part |
42 | 48 | |
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
index 6d81726..2a14252 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 | # Cache the real translations provider in a global variable. Accessing settings |
| 24 | # is slow in 1.2 |
| 25 | _trans_provider = None |
| 26 | |
23 | 27 | def delayed_loader(real_name, *args, **kwargs): |
24 | 28 | """ |
25 | 29 | Call the real, underlying function. We have a level of indirection here so |
26 | 30 | that modules can use the translation bits without actually requiring |
27 | 31 | Django's settings bits to be configured before import. |
28 | 32 | """ |
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 |
| 33 | global _trans_provider |
| 34 | if not _trans_provider: |
| 35 | from django.conf import settings |
| 36 | if settings.USE_I18N: |
| 37 | from django.utils.translation import trans_real as trans |
| 38 | else: |
| 39 | from django.utils.translation import trans_null as trans |
| 40 | _trans_provider = trans |
34 | 41 | |
35 | 42 | # Make the originally requested function call on the way out the door. |
36 | | return getattr(trans, real_name)(*args, **kwargs) |
| 43 | return getattr(_trans_provider, real_name)(*args, **kwargs) |
37 | 44 | |
38 | 45 | g = globals() |
39 | 46 | for name in __all__: |