Ticket #16938: 16938.empty-string-locale-format.diff

File 16938.empty-string-locale-format.diff, 2.6 KB (added by Julien Phalip, 13 years ago)
  • django/utils/formats.py

    diff --git a/django/utils/formats.py b/django/utils/formats.py
    index 3babccb..e283490 100644
    a b def get_format(format_type, lang=None, use_l10n=None):  
    7171            lang = get_language()
    7272        cache_key = (format_type, lang)
    7373        try:
    74             return _format_cache[cache_key] or getattr(settings, format_type)
     74            cached = _format_cache[cache_key]
     75            if cached is not None:
     76                return cached
     77            else:
     78                # Return the general setting by default
     79                return getattr(settings, format_type)
    7580        except KeyError:
    7681            for module in get_format_modules(lang):
    7782                try:
  • tests/regressiontests/i18n/tests.py

    diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
    index bfed0ec..186923d 100644
    a b from django.test import TestCase, RequestFactory  
    1414from django.test.utils import override_settings
    1515from django.utils import translation
    1616from django.utils.formats import (get_format, date_format, time_format,
    17     localize, localize_input, iter_format_modules, get_format_modules)
     17    localize, localize_input, iter_format_modules, get_format_modules,
     18    number_format)
    1819from django.utils.importlib import import_module
    1920from django.utils.numberformat import format as nformat
    2021from django.utils.safestring import mark_safe, SafeString, SafeUnicode
    class FormattingTests(TestCase):  
    397398                self.assertEqual(u'66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
    398399                self.assertEqual(u'100000.0', Template('{{ f|floatformat }}').render(self.ctxt))
    399400
     401    def test_empty_string_format(self):
     402        """
     403        Ensure that the active locale's formats take precedence over the
     404        default settings even if they are empty strings.
     405        Refs #16938.
     406        """
     407        from django.conf.locale.fr import formats as fr_formats
     408        backup = fr_formats.THOUSAND_SEPARATOR
     409        fr_formats.THOUSAND_SEPARATOR = ''
     410        with self.settings(USE_L10N = True, USE_THOUSAND_SEPARATOR=True,
     411                           THOUSAND_SEPARATOR='!'):
     412            with translation.override('fr'):
     413                self.assertEqual(number_format(999999), '999999')
     414                # Even a second time (after the format has been cached)...
     415                self.assertEqual(number_format(999999), '999999')
     416        fr_formats.THOUSAND_SEPARATOR = backup
     417
    400418    def test_l10n_enabled(self):
    401419        settings.USE_L10N = True
    402420        # Catalan locale
Back to Top