Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14240 -- Enabled localization for the filesize filter. Thanks …

…to David Danier for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15290 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3a6491cf56ccd8b5cdbc081034b9ab4a9dbc596b 1 parent d053624
Russell Keith-Magee authored January 24, 2011
2  AUTHORS
@@ -131,7 +131,7 @@ answer newbie questions, and generally made Django that much better:
131 131
     dackze+django@gmail.com
132 132
     Jim Dalton <jim.dalton@gmail.com>
133 133
     Mihai Damian <yang_damian@yahoo.com>
134  
-    David Danier <goliath.mailinglist@gmx.de>
  134
+    David Danier <david.danier@team23.de>
135 135
     Dirk Datzert <dummy@habmalnefrage.de>
136 136
     Jonathan Daugherty (cygnus) <http://www.cprogrammer.org/>
137 137
     dave@thebarproject.com
14  django/template/defaultfilters.py
@@ -799,19 +799,21 @@ def filesizeformat(bytes):
799 799
     try:
800 800
         bytes = float(bytes)
801 801
     except (TypeError,ValueError,UnicodeDecodeError):
802  
-        return u"0 bytes"
  802
+        return ungettext("%(size)d byte", "%(size)d bytes", 0) % {'size': 0}
  803
+
  804
+    filesize_number_format = lambda value: formats.number_format(round(value, 1), 1)
803 805
 
804 806
     if bytes < 1024:
805 807
         return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes}
806 808
     if bytes < 1024 * 1024:
807  
-        return ugettext("%.1f KB") % (bytes / 1024)
  809
+        return ugettext("%s KB") % filesize_number_format(bytes / 1024)
808 810
     if bytes < 1024 * 1024 * 1024:
809  
-        return ugettext("%.1f MB") % (bytes / (1024 * 1024))
  811
+        return ugettext("%s MB") % filesize_number_format(bytes / (1024 * 1024))
810 812
     if bytes < 1024 * 1024 * 1024 * 1024:
811  
-        return ugettext("%.1f GB") % (bytes / (1024 * 1024 * 1024))
  813
+        return ugettext("%s GB") % filesize_number_format(bytes / (1024 * 1024 * 1024))
812 814
     if bytes < 1024 * 1024 * 1024 * 1024 * 1024:
813  
-        return ugettext("%.1f TB") % (bytes / (1024 * 1024 * 1024 * 1024))
814  
-    return ugettext("%.1f PB") % (bytes / (1024 * 1024 * 1024 * 1024 * 1024))
  815
+        return ugettext("%s TB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024))
  816
+    return ugettext("%s PB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024 * 1024))
815 817
 filesizeformat.is_safe = True
816 818
 
817 819
 def pluralize(value, arg=u's'):
27  tests/regressiontests/defaultfilters/tests.py
@@ -443,6 +443,33 @@ def test_filesizeformat(self):
443 443
         self.assertEqual(filesizeformat(u"\N{GREEK SMALL LETTER ALPHA}"),
444 444
                           u'0 bytes')
445 445
 
  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
+
446 473
     def test_pluralize(self):
447 474
         self.assertEqual(pluralize(1), u'')
448 475
         self.assertEqual(pluralize(0), u's')

0 notes on commit 3a6491c

Please sign in to comment.
Something went wrong with that request. Please try again.