diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2011-01-24 08:16:09 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2011-01-24 08:16:09 +0000 |
| commit | 3a6491cf56ccd8b5cdbc081034b9ab4a9dbc596b (patch) | |
| tree | fa4806288d2f1f8bb7e990b1a9837bf7c88261b0 | |
| parent | d053624aa8534c984e07d4d8d2ee867de013e2ec (diff) | |
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
| -rw-r--r-- | AUTHORS | 2 | ||||
| -rw-r--r-- | django/template/defaultfilters.py | 14 | ||||
| -rw-r--r-- | tests/regressiontests/defaultfilters/tests.py | 27 |
3 files changed, 36 insertions, 7 deletions
@@ -131,7 +131,7 @@ answer newbie questions, and generally made Django that much better: dackze+django@gmail.com Jim Dalton <jim.dalton@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 diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index 1e00effea0..8fabef642f 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -799,19 +799,21 @@ 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("%s MB") % filesize_number_format(bytes / (1024 * 1024)) if bytes < 1024 * 1024 * 1024 * 1024: - return ugettext("%.1f GB") % (bytes / (1024 * 1024 * 1024)) + return ugettext("%s GB") % filesize_number_format(bytes / (1024 * 1024 * 1024)) if bytes < 1024 * 1024 * 1024 * 1024 * 1024: - return ugettext("%.1f TB") % (bytes / (1024 * 1024 * 1024 * 1024)) - return ugettext("%.1f PB") % (bytes / (1024 * 1024 * 1024 * 1024 * 1024)) + return ugettext("%s TB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024)) + return ugettext("%s PB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024 * 1024)) filesizeformat.is_safe = True def pluralize(value, arg=u's'): diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py index 27014a7813..95140b7daf 100644 --- a/tests/regressiontests/defaultfilters/tests.py +++ b/tests/regressiontests/defaultfilters/tests.py @@ -443,6 +443,33 @@ 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(1024*1024*1024*1024), u'1,0 TB') + self.assertEqual(filesizeformat(1024*1024*1024*1024*1024), + u'1,0 PB') + self.assertEqual(filesizeformat(1024*1024*1024*1024*1024*2000), + u'2000,0 PB') + 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') |
