Commit a116ae66 authored by Russell Keith-Magee's avatar Russell Keith-Magee
Browse files

[1.2.X] Fixed #14240 -- Enabled localization for the filesize filter. Thanks...

[1.2.X] Fixed #14240 -- Enabled localization for the filesize filter. Thanks to David Danier for the report and patch.

Backport of r15290 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent dec539f3
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -127,7 +127,7 @@ answer newbie questions, and generally made Django that much better:
    John D'Agostino <john.dagostino@gmail.com>
    dackze+django@gmail.com
    Mihai Damian <yang_damian@yahoo.com>
    David Danier <goliath.mailinglist@gmx.de>
    David Danier <david.danier@team23.de>
    Dirk Datzert <dummy@habmalnefrage.de>
    Jonathan Daugherty (cygnus) <http://www.cprogrammer.org/>
    dave@thebarproject.com
+6 −4
Original line number Diff line number Diff line
@@ -789,15 +789,17 @@ def filesizeformat(bytes):
    try:
        bytes = float(bytes)
    except (TypeError,ValueError,UnicodeDecodeError):
        return u"0 bytes"
        return ungettext("%(size)d byte", "%(size)d bytes", 0) % {'size': 0}

    filesize_number_format = lambda value: formats.number_format(round(value, 1), 1)

    if bytes < 1024:
        return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes}
    if bytes < 1024 * 1024:
        return ugettext("%.1f KB") % (bytes / 1024)
        return ugettext("%s KB") % filesize_number_format(bytes / 1024)
    if bytes < 1024 * 1024 * 1024:
        return ugettext("%.1f MB") % (bytes / (1024 * 1024))
    return ugettext("%.1f GB") % (bytes / (1024 * 1024 * 1024))
        return ugettext("%s MB") % filesize_number_format(bytes / (1024 * 1024))
    return ugettext("%s GB") % filesize_number_format(bytes / (1024 * 1024 * 1024))
filesizeformat.is_safe = True

def pluralize(value, arg=u's'):
+22 −0
Original line number Diff line number Diff line
@@ -439,6 +439,28 @@ class DefaultFiltersTests(unittest.TestCase):
        self.assertEqual(filesizeformat(u"\N{GREEK SMALL LETTER ALPHA}"),
                          u'0 bytes')

    def test_localized_filesizeformat(self):
        from django.utils.translation import activate, deactivate
        old_localize = settings.USE_L10N
        try:
            activate('de')
            settings.USE_L10N = True
            self.assertEqual(filesizeformat(1023), u'1023 Bytes')
            self.assertEqual(filesizeformat(1024), u'1,0 KB')
            self.assertEqual(filesizeformat(10*1024), u'10,0 KB')
            self.assertEqual(filesizeformat(1024*1024-1), u'1024,0 KB')
            self.assertEqual(filesizeformat(1024*1024), u'1,0 MB')
            self.assertEqual(filesizeformat(1024*1024*50), u'50,0 MB')
            self.assertEqual(filesizeformat(1024*1024*1024-1), u'1024,0 MB')
            self.assertEqual(filesizeformat(1024*1024*1024), u'1,0 GB')
            self.assertEqual(filesizeformat(complex(1,-1)), u'0 Bytes')
            self.assertEqual(filesizeformat(""), u'0 Bytes')
            self.assertEqual(filesizeformat(u"\N{GREEK SMALL LETTER ALPHA}"),
                              u'0 Bytes')
        finally:
            deactivate()
            settings.USE_L10N = old_localize

    def test_pluralize(self):
        self.assertEqual(pluralize(1), u'')
        self.assertEqual(pluralize(0), u's')