Skip to content

Commit 90d6d4c

Browse files
akxafh
andcommitted
Prefer LC_MONETARY when formatting currency
Co-authored-by: Alexis Hildebrandt <[email protected]>
1 parent 4d6658c commit 90d6d4c

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

babel/numbers.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
The default locale for the functions in this module is determined by the
88
following environment variables, in that order:
99
10-
* ``LC_NUMERIC``,
10+
* ``LC_MONETARY`` for currency related functions,
11+
* ``LC_NUMERIC``, and
1112
* ``LC_ALL``, and
1213
* ``LANG``
1314
@@ -31,6 +32,7 @@
3132
if TYPE_CHECKING:
3233
from typing_extensions import Literal
3334

35+
LC_MONETARY = default_locale(('LC_MONETARY', 'LC_NUMERIC'))
3436
LC_NUMERIC = default_locale('LC_NUMERIC')
3537

3638

@@ -120,9 +122,10 @@ def get_currency_name(
120122
:param currency: the currency code.
121123
:param count: the optional count. If provided the currency name
122124
will be pluralized to that number if possible.
123-
:param locale: the `Locale` object or locale identifier. Defaults to the system numeric locale.
125+
:param locale: the `Locale` object or locale identifier.
126+
Defaults to the system currency locale or numeric locale.
124127
"""
125-
loc = Locale.parse(locale or LC_NUMERIC)
128+
loc = Locale.parse(locale or LC_MONETARY)
126129
if count is not None:
127130
try:
128131
plural_form = loc.plural_form(count)
@@ -145,9 +148,10 @@ def get_currency_symbol(currency: str, locale: Locale | str | None = None) -> st
145148
u'$'
146149
147150
:param currency: the currency code.
148-
:param locale: the `Locale` object or locale identifier. Defaults to the system numeric locale.
151+
:param locale: the `Locale` object or locale identifier.
152+
Defaults to the system currency locale or numeric locale.
149153
"""
150-
return Locale.parse(locale or LC_NUMERIC).currency_symbols.get(currency, currency)
154+
return Locale.parse(locale or LC_MONETARY).currency_symbols.get(currency, currency)
151155

152156

153157
def get_currency_precision(currency: str) -> int:
@@ -184,9 +188,10 @@ def get_currency_unit_pattern(
184188
:param currency: the currency code.
185189
:param count: the optional count. If provided the unit
186190
pattern for that number will be returned.
187-
:param locale: the `Locale` object or locale identifier. Defaults to the system numeric locale.
191+
:param locale: the `Locale` object or locale identifier.
192+
Defaults to the system currency locale or numeric locale.
188193
"""
189-
loc = Locale.parse(locale or LC_NUMERIC)
194+
loc = Locale.parse(locale or LC_MONETARY)
190195
if count is not None:
191196
plural_form = loc.plural_form(count)
192197
try:
@@ -763,7 +768,8 @@ def format_currency(
763768
:param number: the number to format
764769
:param currency: the currency code
765770
:param format: the format string to use
766-
:param locale: the `Locale` object or locale identifier. Defaults to the system numeric locale.
771+
:param locale: the `Locale` object or locale identifier.
772+
Defaults to the system currency locale or numeric locale.
767773
:param currency_digits: use the currency's natural number of decimal digits
768774
:param format_type: the currency format type to use
769775
:param decimal_quantization: Truncate and round high-precision numbers to
@@ -774,7 +780,7 @@ def format_currency(
774780
The special value "default" will use the default numbering system of the locale.
775781
:raise `UnsupportedNumberingSystemError`: If the numbering system is not supported by the locale.
776782
"""
777-
locale = Locale.parse(locale or LC_NUMERIC)
783+
locale = Locale.parse(locale or LC_MONETARY)
778784

779785
if format_type == 'name':
780786
return _format_currency_long_name(
@@ -863,13 +869,14 @@ def format_compact_currency(
863869
:param number: the number to format
864870
:param currency: the currency code
865871
:param format_type: the compact format type to use. Defaults to "short".
866-
:param locale: the `Locale` object or locale identifier. Defaults to the system numeric locale.
872+
:param locale: the `Locale` object or locale identifier.
873+
Defaults to the system currency locale or numeric locale.
867874
:param fraction_digits: Number of digits after the decimal point to use. Defaults to `0`.
868875
:param numbering_system: The numbering system used for formatting number symbols. Defaults to "latn".
869876
The special value "default" will use the default numbering system of the locale.
870877
:raise `UnsupportedNumberingSystemError`: If the numbering system is not supported by the locale.
871878
"""
872-
locale = Locale.parse(locale or LC_NUMERIC)
879+
locale = Locale.parse(locale or LC_MONETARY)
873880
try:
874881
compact_format = locale.compact_currency_formats[format_type]
875882
except KeyError as error:

0 commit comments

Comments
 (0)