Ticket #14240: django_localize_filesizeformat_number.r14561.diff

File django_localize_filesizeformat_number.r14561.diff, 4.0 KB (added by David Danier <david.danier@…>, 14 years ago)
  • django/template/defaultfilters.py

     
    819819    try:
    820820        bytes = float(bytes)
    821821    except (TypeError,ValueError,UnicodeDecodeError):
    822         return u"0 bytes"
     822        return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': 0}
    823823
     824    filesize_number_format = lambda value: formats.number_format(round(value, 1), 1)
     825
    824826    if bytes < 1024:
    825827        return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes}
    826828    if bytes < 1024 * 1024:
    827         return ugettext("%.1f KB") % (bytes / 1024)
     829        return ugettext("%s KB") % filesize_number_format(bytes / 1024)
    828830    if bytes < 1024 * 1024 * 1024:
    829         return ugettext("%.1f MB") % (bytes / (1024 * 1024))
     831        return ugettext("%s MB") % filesize_number_format(bytes / (1024 * 1024))
    830832    if bytes < 1024 * 1024 * 1024 * 1024:
    831         return ugettext("%.1f GB") % (bytes / (1024 * 1024 * 1024))
     833        return ugettext("%s GB") % filesize_number_format(bytes / (1024 * 1024 * 1024))
    832834    if bytes < 1024 * 1024 * 1024 * 1024 * 1024:
    833         return ugettext("%.1f TB") % (bytes / (1024 * 1024 * 1024 * 1024))
    834     return ugettext("%.1f PB") % (bytes / (1024 * 1024 * 1024 * 1024 * 1024))
     835        return ugettext("%s TB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024))
     836    return ugettext("%s PB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024 * 1024))
    835837filesizeformat.is_safe = True
    836838
    837839def pluralize(value, arg=u's'):
  • tests/regressiontests/defaultfilters/tests.py

     
    443443        self.assertEqual(filesizeformat(u"\N{GREEK SMALL LETTER ALPHA}"),
    444444                          u'0 bytes')
    445445
     446    def test_localized_filesizeformat(self):
     447        from django.utils.translation import activate, deactivate
     448        old_localize = settings.USE_L10N
     449        try:
     450            activate('de')
     451            settings.USE_L10N = True
     452            self.assertEqual(filesizeformat(1023), u'1023 Bytes')
     453            self.assertEqual(filesizeformat(1024), u'1,0 KB')
     454            self.assertEqual(filesizeformat(10*1024), u'10,0 KB')
     455            self.assertEqual(filesizeformat(1024*1024-1), u'1024,0 KB')
     456            self.assertEqual(filesizeformat(1024*1024), u'1,0 MB')
     457            self.assertEqual(filesizeformat(1024*1024*50), u'50,0 MB')
     458            self.assertEqual(filesizeformat(1024*1024*1024-1), u'1024,0 MB')
     459            self.assertEqual(filesizeformat(1024*1024*1024), u'1,0 GB')
     460            self.assertEqual(filesizeformat(1024*1024*1024*1024), u'1,0 TB')
     461            self.assertEqual(filesizeformat(1024*1024*1024*1024*1024),
     462                              u'1,0 PB')
     463            self.assertEqual(filesizeformat(1024*1024*1024*1024*1024*2000),
     464                              u'2000,0 PB')
     465            self.assertEqual(filesizeformat(complex(1,-1)), u'0 Bytes')
     466            self.assertEqual(filesizeformat(""), u'0 Bytes')
     467            self.assertEqual(filesizeformat(u"\N{GREEK SMALL LETTER ALPHA}"),
     468                              u'0 Bytes')
     469        finally:
     470            deactivate()
     471            settings.USE_L10N = old_localize
     472
    446473    def test_pluralize(self):
    447474        self.assertEqual(pluralize(1), u'')
    448475        self.assertEqual(pluralize(0), u's')
  • AUTHORS

     
    129129    John D'Agostino <john.dagostino@gmail.com>
    130130    dackze+django@gmail.com
    131131    Mihai Damian <yang_damian@yahoo.com>
    132     David Danier <goliath.mailinglist@gmx.de>
     132    David Danier <david.danier@team23.de>
    133133    Dirk Datzert <dummy@habmalnefrage.de>
    134134    Jonathan Daugherty (cygnus) <http://www.cprogrammer.org/>
    135135    dave@thebarproject.com
Back to Top