Skip to content

Commit

Permalink
Prefer LC_MONETARY when formatting currency
Browse files Browse the repository at this point in the history
Co-authored-by: Alexis Hildebrandt <afh@surryhill.net>
  • Loading branch information
akx and afh committed Jan 14, 2025
1 parent 4d6658c commit 90d6d4c
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
The default locale for the functions in this module is determined by the
following environment variables, in that order:
* ``LC_NUMERIC``,
* ``LC_MONETARY`` for currency related functions,
* ``LC_NUMERIC``, and
* ``LC_ALL``, and
* ``LANG``
Expand All @@ -31,6 +32,7 @@
if TYPE_CHECKING:
from typing_extensions import Literal

LC_MONETARY = default_locale(('LC_MONETARY', 'LC_NUMERIC'))
LC_NUMERIC = default_locale('LC_NUMERIC')


Expand Down Expand Up @@ -120,9 +122,10 @@ def get_currency_name(
:param currency: the currency code.
:param count: the optional count. If provided the currency name
will be pluralized to that number if possible.
:param locale: the `Locale` object or locale identifier. Defaults to the system numeric locale.
:param locale: the `Locale` object or locale identifier.
Defaults to the system currency locale or numeric locale.
"""
loc = Locale.parse(locale or LC_NUMERIC)
loc = Locale.parse(locale or LC_MONETARY)
if count is not None:
try:
plural_form = loc.plural_form(count)
Expand All @@ -145,9 +148,10 @@ def get_currency_symbol(currency: str, locale: Locale | str | None = None) -> st
u'$'
:param currency: the currency code.
:param locale: the `Locale` object or locale identifier. Defaults to the system numeric locale.
:param locale: the `Locale` object or locale identifier.
Defaults to the system currency locale or numeric locale.
"""
return Locale.parse(locale or LC_NUMERIC).currency_symbols.get(currency, currency)
return Locale.parse(locale or LC_MONETARY).currency_symbols.get(currency, currency)


def get_currency_precision(currency: str) -> int:
Expand Down Expand Up @@ -184,9 +188,10 @@ def get_currency_unit_pattern(
:param currency: the currency code.
:param count: the optional count. If provided the unit
pattern for that number will be returned.
:param locale: the `Locale` object or locale identifier. Defaults to the system numeric locale.
:param locale: the `Locale` object or locale identifier.
Defaults to the system currency locale or numeric locale.
"""
loc = Locale.parse(locale or LC_NUMERIC)
loc = Locale.parse(locale or LC_MONETARY)
if count is not None:
plural_form = loc.plural_form(count)
try:
Expand Down Expand Up @@ -763,7 +768,8 @@ def format_currency(
:param number: the number to format
:param currency: the currency code
:param format: the format string to use
:param locale: the `Locale` object or locale identifier. Defaults to the system numeric locale.
:param locale: the `Locale` object or locale identifier.
Defaults to the system currency locale or numeric locale.
:param currency_digits: use the currency's natural number of decimal digits
:param format_type: the currency format type to use
:param decimal_quantization: Truncate and round high-precision numbers to
Expand All @@ -774,7 +780,7 @@ def format_currency(
The special value "default" will use the default numbering system of the locale.
:raise `UnsupportedNumberingSystemError`: If the numbering system is not supported by the locale.
"""
locale = Locale.parse(locale or LC_NUMERIC)
locale = Locale.parse(locale or LC_MONETARY)

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

0 comments on commit 90d6d4c

Please sign in to comment.