From d80d2e8103a5d8a96863cebf433d61afa45e319e Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Mon, 27 Sep 2010 16:21:16 +0000 Subject: [PATCH] Fixed #14053 -- Also localize long integers. Thanks, David Danier. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13920 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/formats.py | 4 ++-- tests/regressiontests/i18n/tests.py | 24 +++++++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/django/utils/formats.py b/django/utils/formats.py index 0ec79f1590..e64cc4e95a 100644 --- a/django/utils/formats.py +++ b/django/utils/formats.py @@ -99,7 +99,7 @@ def localize(value): Checks if value is a localizable type (date, number...) and returns it formatted as a string using current locale format """ - if isinstance(value, (decimal.Decimal, float, int)): + if isinstance(value, (decimal.Decimal, float, int, long)): return number_format(value) elif isinstance(value, datetime.datetime): return date_format(value, 'DATETIME_FORMAT') @@ -115,7 +115,7 @@ def localize_input(value, default=None): Checks if an input value is a localizable type and returns it formatted with the appropriate formatting string of the current locale. """ - if isinstance(value, (decimal.Decimal, float, int)): + if isinstance(value, (decimal.Decimal, float, int, long)): return number_format(value) elif isinstance(value, datetime.datetime): value = datetime_safe.new_datetime(value) diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index 01f6aa0d3f..f58c69e9b6 100644 --- a/tests/regressiontests/i18n/tests.py +++ b/tests/regressiontests/i18n/tests.py @@ -122,12 +122,14 @@ class FormattingTests(TestCase): self.d = datetime.date(2009, 12, 31) self.dt = datetime.datetime(2009, 12, 31, 20, 50) self.t = datetime.time(10, 15, 48) + self.l = 10000L self.ctxt = Context({ 'n': self.n, 't': self.t, 'd': self.d, 'dt': self.dt, - 'f': self.f + 'f': self.f, + 'l': self.l, }) def tearDown(self): @@ -152,6 +154,7 @@ class FormattingTests(TestCase): self.assertEqual(u'6B6B6B6B6A6', nformat(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B')) self.assertEqual(u'-66666.6', nformat(-66666.666, decimal_sep='.', decimal_pos=1)) self.assertEqual(u'-66666.0', nformat(int('-66666'), decimal_sep='.', decimal_pos=1)) + self.assertEqual(u'10000.0', nformat(self.l, decimal_sep='.', decimal_pos=1)) # date filter self.assertEqual(u'31.12.2009 в 20:50', Template('{{ dt|date:"d.m.Y в H:i" }}').render(self.ctxt)) @@ -165,16 +168,17 @@ class FormattingTests(TestCase): settings.USE_L10N = False activate('ca') try: - self.assertEqual('N j, Y', get_format('DATE_FORMAT')) + self.assertEqual(u'N j, Y', get_format('DATE_FORMAT')) self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK')) - self.assertEqual('.', get_format('DECIMAL_SEPARATOR')) + self.assertEqual(u'.', get_format('DECIMAL_SEPARATOR')) self.assertEqual(u'10:15 a.m.', time_format(self.t)) self.assertEqual(u'des. 31, 2009', date_format(self.d)) self.assertEqual(u'desembre 2009', date_format(self.d, 'YEAR_MONTH_FORMAT')) self.assertEqual(u'12/31/2009 8:50 p.m.', date_format(self.dt, 'SHORT_DATETIME_FORMAT')) - self.assertEqual('No localizable', localize('No localizable')) - self.assertEqual('66666.666', localize(self.n)) - self.assertEqual('99999.999', localize(self.f)) + self.assertEqual(u'No localizable', localize('No localizable')) + self.assertEqual(u'66666.666', localize(self.n)) + self.assertEqual(u'99999.999', localize(self.f)) + self.assertEqual(u'10000', localize(self.l)) self.assertEqual(u'des. 31, 2009', localize(self.d)) self.assertEqual(u'des. 31, 2009, 8:50 p.m.', localize(self.dt)) self.assertEqual(u'66666.666', Template('{{ n }}').render(self.ctxt)) @@ -245,16 +249,19 @@ class FormattingTests(TestCase): settings.USE_THOUSAND_SEPARATOR = True self.assertEqual(u'66.666,666', localize(self.n)) self.assertEqual(u'99.999,999', localize(self.f)) + self.assertEqual(u'10.000', localize(self.l)) settings.USE_THOUSAND_SEPARATOR = False self.assertEqual(u'66666,666', localize(self.n)) self.assertEqual(u'99999,999', localize(self.f)) + self.assertEqual(u'10000', localize(self.l)) self.assertEqual(u'31 de desembre de 2009', localize(self.d)) self.assertEqual(u'31 de desembre de 2009 a les 20:50', localize(self.dt)) settings.USE_THOUSAND_SEPARATOR = True self.assertEqual(u'66.666,666', Template('{{ n }}').render(self.ctxt)) self.assertEqual(u'99.999,999', Template('{{ f }}').render(self.ctxt)) + self.assertEqual(u'10.000', Template('{{ l }}').render(self.ctxt)) form3 = I18nForm({ 'decimal_field': u'66.666,666', @@ -324,21 +331,24 @@ class FormattingTests(TestCase): self.assertEqual(u'Dec. 31, 2009', date_format(self.d)) self.assertEqual(u'December 2009', date_format(self.d, 'YEAR_MONTH_FORMAT')) self.assertEqual(u'12/31/2009 8:50 p.m.', date_format(self.dt, 'SHORT_DATETIME_FORMAT')) - self.assertEqual('No localizable', localize('No localizable')) + self.assertEqual(u'No localizable', localize('No localizable')) settings.USE_THOUSAND_SEPARATOR = True self.assertEqual(u'66,666.666', localize(self.n)) self.assertEqual(u'99,999.999', localize(self.f)) + self.assertEqual(u'10,000', localize(self.l)) settings.USE_THOUSAND_SEPARATOR = False self.assertEqual(u'66666.666', localize(self.n)) self.assertEqual(u'99999.999', localize(self.f)) + self.assertEqual(u'10000', localize(self.l)) self.assertEqual(u'Dec. 31, 2009', localize(self.d)) self.assertEqual(u'Dec. 31, 2009, 8:50 p.m.', localize(self.dt)) settings.USE_THOUSAND_SEPARATOR = True self.assertEqual(u'66,666.666', Template('{{ n }}').render(self.ctxt)) self.assertEqual(u'99,999.999', Template('{{ f }}').render(self.ctxt)) + self.assertEqual(u'10,000', Template('{{ l }}').render(self.ctxt)) settings.USE_THOUSAND_SEPARATOR = False self.assertEqual(u'66666.666', Template('{{ n }}').render(self.ctxt))