Skip to content

Commit

Permalink
Fix empty locale errors in format_number/percent
Browse files Browse the repository at this point in the history
If our format_number() and format_percent() wrappers (around the babel
functions) aren't passed a locale argument, they will use c.locale. But
in some cases, c.locale will return an empty string, which will then be
used when calling the babel functions. The locale-parsing in babel is
unable to handle an empty string, and will throw an exception.

This commit changes these wrappers slightly so that if c.locale is also
empty, the wrappers will instead call the babel functions without a
locale argument, which will cause it to default to the LC_NUMERIC
environment variable.
  • Loading branch information
Deimos committed Jul 11, 2016
1 parent 40dd0c2 commit 0ac24dd
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions r2/r2/lib/template_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,11 +663,20 @@ def format_number(number, locale=None):
if not locale:
locale = c.locale

return babel.numbers.format_number(number, locale=locale)
if locale:
return babel.numbers.format_number(number, locale=locale)
else:
return babel.numbers.format_number(number)


def format_percent(ratio, locale=None):
return babel.numbers.format_percent(ratio, locale=locale or c.locale)
if not locale:
locale = c.locale

if locale:
return babel.numbers.format_percent(ratio, locale=locale)
else:
return babel.numbers.format_percent(ratio)


def html_datetime(date):
Expand Down

0 comments on commit 0ac24dd

Please sign in to comment.