Skip to content

Commit cc380b9

Browse files
author
cmlenz
committed
BabelDjango: Add template tags for date and number formatting.
git-svn-id: http://svn.edgewall.org/repos/babel/contrib/django@293 59ecc08e-a131-0410-9ea7-d4c0f28ac310
1 parent 06a5661 commit cc380b9

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

README.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Django web framework:
66

77
* A message extraction plugin for Django templates.
88
* A middleware class that adds the Babel `Locale` object to requests.
9+
* A set of template tags for date and number formatting.
910

1011
For more information please visit the wiki page for this package:
1112

babeldjango/templatetags/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2007 Edgewall Software
4+
# All rights reserved.
5+
#
6+
# This software is licensed as described in the file COPYING, which
7+
# you should have received as part of this distribution. The terms
8+
# are also available at http://babel.edgewall.org/wiki/License.
9+
#
10+
# This software consists of voluntary contributions made by many
11+
# individuals. For the exact contribution history, see the revision
12+
# history and logs, available at http://babel.edgewall.org/log/.

babeldjango/templatetags/babel.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2007 Edgewall Software
4+
# All rights reserved.
5+
#
6+
# This software is licensed as described in the file COPYING, which
7+
# you should have received as part of this distribution. The terms
8+
# are also available at http://babel.edgewall.org/wiki/License.
9+
#
10+
# This software consists of voluntary contributions made by many
11+
# individuals. For the exact contribution history, see the revision
12+
# history and logs, available at http://babel.edgewall.org/log/.
13+
14+
from django.conf import settings
15+
from django.template import Library
16+
try:
17+
from pytz import timezone
18+
except ImportError:
19+
timezone = None
20+
21+
from babeldjango.middleware import get_current_locale
22+
23+
babel = __import__('babel', {}, {}, ['core', 'support'])
24+
Format = babel.support.Format
25+
Locale = babel.core.Locale
26+
27+
register = Library()
28+
29+
def _get_format():
30+
locale = get_current_locale()
31+
if not locale:
32+
locale = Locale.parse(settings.LANGUAGE_CODE)
33+
if timezone:
34+
tzinfo = timezone(settings.TIME_ZONE)
35+
else:
36+
tzinfo = None
37+
return Format(locale, tzinfo)
38+
39+
def datefmt(date=None, format='medium'):
40+
return _get_format().date(date, format=format)
41+
datefmt = register.filter(datefmt)
42+
43+
def datetimefmt(datetime=None, format='medium'):
44+
return _get_format().datetime(datetime, format=format)
45+
datetimefmt = register.filter(datetimefmt)
46+
47+
def timefmt(time=None, format='medium'):
48+
return _get_format().time(time, format=format)
49+
timefmt = register.filter(timefmt)
50+
51+
def numberfmt(number):
52+
return _get_format().number(number)
53+
numberfmt = register.filter(numberfmt)
54+
55+
def decimalfmt(number, format=None):
56+
return _get_format().decimal(number, format=format)
57+
decimalfmt = register.filter(decimalfmt)
58+
59+
def currencyfmt(number, currency):
60+
return _get_format().currency(number, currency)
61+
currencyfmt = register.filter(currencyfmt)
62+
63+
def percentfmt(number, format=None):
64+
return _get_format().percent(number, format=format)
65+
percentfmt = register.filter(percentfmt)
66+
67+
def scientificfmt(number):
68+
return _get_format().scientific(number)
69+
scientificfmt = register.filter(scientificfmt)

0 commit comments

Comments
 (0)