Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,8 @@ def format_currency(
locale=locale, currency_digits=currency_digits,
decimal_quantization=decimal_quantization, group_separator=group_separator,
numbering_system=numbering_system)
if locale is None:
locale = default_locale()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It bugs me that default_locale() determines the locale as a string, parses it to a Locale, and converts it to its string identifier. Then immediately after, it's parsed back to a Locale.

But I can't think of a good way around this, other than a breaking change in default_locale().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
locale = default_locale()
locale = LC_NUMERIC

Though I'm not sure if it makes sense to just fix this one function. A lot of the other format_* functions cannot deal with explicitly passed None as the locale either:

from babel.numbers import format_compact_currency
format_compact_currency('12', 'EUR', locale=None)

I think if we're gonna fix this, we should fix all of them at the same time, otherwise we're gonna have functions with the same type annotation behaving differently which could lead to confusion.

locale = Locale.parse(locale)
if format:
pattern = parse_pattern(format)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ def test_format_currency():
assert (numbers.format_currency(0, 'USD', locale='es_AR')
== 'US$0,00') # other

def test_format_currency_with_none_locale():
assert (numbers.format_currency(0, "USD", locale=None)
== "0,00\xa0$US")
assert (numbers.format_currency(1099.98, "EUR", locale=None)
== "1\u202f099,98\xa0€")

def test_format_currency_format_type():
assert (numbers.format_currency(1099.98, 'USD', locale='en_US',
Expand Down