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
This commit is contained in:
Jannis Leidel 2010-09-27 16:21:16 +00:00
parent 45d348fdd9
commit d80d2e8103
2 changed files with 19 additions and 9 deletions

View File

@ -99,7 +99,7 @@ def localize(value):
Checks if value is a localizable type (date, number...) and returns it Checks if value is a localizable type (date, number...) and returns it
formatted as a string using current locale format 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) return number_format(value)
elif isinstance(value, datetime.datetime): elif isinstance(value, datetime.datetime):
return date_format(value, 'DATETIME_FORMAT') 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 Checks if an input value is a localizable type and returns it
formatted with the appropriate formatting string of the current locale. 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) return number_format(value)
elif isinstance(value, datetime.datetime): elif isinstance(value, datetime.datetime):
value = datetime_safe.new_datetime(value) value = datetime_safe.new_datetime(value)

View File

@ -122,12 +122,14 @@ class FormattingTests(TestCase):
self.d = datetime.date(2009, 12, 31) self.d = datetime.date(2009, 12, 31)
self.dt = datetime.datetime(2009, 12, 31, 20, 50) self.dt = datetime.datetime(2009, 12, 31, 20, 50)
self.t = datetime.time(10, 15, 48) self.t = datetime.time(10, 15, 48)
self.l = 10000L
self.ctxt = Context({ self.ctxt = Context({
'n': self.n, 'n': self.n,
't': self.t, 't': self.t,
'd': self.d, 'd': self.d,
'dt': self.dt, 'dt': self.dt,
'f': self.f 'f': self.f,
'l': self.l,
}) })
def tearDown(self): 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'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.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'-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 # date filter
self.assertEqual(u'31.12.2009 в 20:50', Template('{{ dt|date:"d.m.Y в H:i" }}').render(self.ctxt)) 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 settings.USE_L10N = False
activate('ca') activate('ca')
try: 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(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'10:15 a.m.', time_format(self.t))
self.assertEqual(u'des. 31, 2009', date_format(self.d)) 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'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(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'))
self.assertEqual('66666.666', localize(self.n)) self.assertEqual(u'66666.666', localize(self.n))
self.assertEqual('99999.999', localize(self.f)) 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', localize(self.d))
self.assertEqual(u'des. 31, 2009, 8:50 p.m.', localize(self.dt)) self.assertEqual(u'des. 31, 2009, 8:50 p.m.', localize(self.dt))
self.assertEqual(u'66666.666', Template('{{ n }}').render(self.ctxt)) self.assertEqual(u'66666.666', Template('{{ n }}').render(self.ctxt))
@ -245,16 +249,19 @@ class FormattingTests(TestCase):
settings.USE_THOUSAND_SEPARATOR = True settings.USE_THOUSAND_SEPARATOR = True
self.assertEqual(u'66.666,666', localize(self.n)) self.assertEqual(u'66.666,666', localize(self.n))
self.assertEqual(u'99.999,999', localize(self.f)) self.assertEqual(u'99.999,999', localize(self.f))
self.assertEqual(u'10.000', localize(self.l))
settings.USE_THOUSAND_SEPARATOR = False settings.USE_THOUSAND_SEPARATOR = False
self.assertEqual(u'66666,666', localize(self.n)) self.assertEqual(u'66666,666', localize(self.n))
self.assertEqual(u'99999,999', localize(self.f)) 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', localize(self.d))
self.assertEqual(u'31 de desembre de 2009 a les 20:50', localize(self.dt)) self.assertEqual(u'31 de desembre de 2009 a les 20:50', localize(self.dt))
settings.USE_THOUSAND_SEPARATOR = True settings.USE_THOUSAND_SEPARATOR = True
self.assertEqual(u'66.666,666', Template('{{ n }}').render(self.ctxt)) 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'99.999,999', Template('{{ f }}').render(self.ctxt))
self.assertEqual(u'10.000', Template('{{ l }}').render(self.ctxt))
form3 = I18nForm({ form3 = I18nForm({
'decimal_field': u'66.666,666', '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'Dec. 31, 2009', date_format(self.d))
self.assertEqual(u'December 2009', date_format(self.d, 'YEAR_MONTH_FORMAT')) 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(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 settings.USE_THOUSAND_SEPARATOR = True
self.assertEqual(u'66,666.666', localize(self.n)) self.assertEqual(u'66,666.666', localize(self.n))
self.assertEqual(u'99,999.999', localize(self.f)) self.assertEqual(u'99,999.999', localize(self.f))
self.assertEqual(u'10,000', localize(self.l))
settings.USE_THOUSAND_SEPARATOR = False settings.USE_THOUSAND_SEPARATOR = False
self.assertEqual(u'66666.666', localize(self.n)) self.assertEqual(u'66666.666', localize(self.n))
self.assertEqual(u'99999.999', localize(self.f)) 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', localize(self.d))
self.assertEqual(u'Dec. 31, 2009, 8:50 p.m.', localize(self.dt)) self.assertEqual(u'Dec. 31, 2009, 8:50 p.m.', localize(self.dt))
settings.USE_THOUSAND_SEPARATOR = True settings.USE_THOUSAND_SEPARATOR = True
self.assertEqual(u'66,666.666', Template('{{ n }}').render(self.ctxt)) 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'99,999.999', Template('{{ f }}').render(self.ctxt))
self.assertEqual(u'10,000', Template('{{ l }}').render(self.ctxt))
settings.USE_THOUSAND_SEPARATOR = False settings.USE_THOUSAND_SEPARATOR = False
self.assertEqual(u'66666.666', Template('{{ n }}').render(self.ctxt)) self.assertEqual(u'66666.666', Template('{{ n }}').render(self.ctxt))