Skip to content

Commit

Permalink
Bug 1078297: Need localized number formatting helper for jinja templates
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalchevrel authored and pmac committed Oct 10, 2014
1 parent f75bc9b commit 655649f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/l10n_utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jinja2
from babel.core import Locale, UnknownLocaleError
from babel.dates import format_date
from babel.numbers import format_number

from django.conf import settings
from django.utils.translation import get_language
Expand Down Expand Up @@ -94,3 +95,14 @@ def l10n_format_date(date, format='long'):
"""
locale = current_locale()
return format_date(date, locale=locale, format=format)



@jingo.register.filter
def l10n_format_number(number):
"""
Formats a number according to the current locale. Wraps around
babel.numbers.format_number.
"""
locale = current_locale()
return format_number(number, locale=locale)
10 changes: 10 additions & 0 deletions lib/l10n_utils/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,13 @@ def test_success(self, format_date, current_locale):
format_date.return_value)
format_date.assert_called_with(
'somedate', locale=current_locale.return_value, format='long')


class TestL10nFormatNumber(TestCase):
@patch('l10n_utils.helpers.current_locale')
@patch('l10n_utils.helpers.format_number')
def test_success(self, format_number, current_locale):
eq_(helpers.l10n_format_number(10000),
format_number.return_value)
format_number.assert_called_with(
10000, locale=current_locale.return_value)

0 comments on commit 655649f

Please sign in to comment.