Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to LC_MONETARY when formatting currency #1051

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 8 additions & 7 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
The default locale for the functions in this module is determined by the
following environment variables, in that order:

* ``LC_NUMERIC``,
* ``LC_NUMERIC`` or ``LC_MONETARY`` for currency related functions,
Copy link
Member

Choose a reason for hiding this comment

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

This documentation doesn't seem to match the new behavior; it's not an "or" if the envvar is set. This should probably say

Suggested change
* ``LC_NUMERIC``,
* ``LC_NUMERIC`` or ``LC_MONETARY`` for currency related functions,
* ``LC_MONETARY`` for currency related functions,
* ``LC_NUMERIC`` for other numbers,

or something along those lines.

* ``LC_ALL``, and
* ``LANG``

Expand All @@ -31,6 +31,7 @@
if TYPE_CHECKING:
from typing_extensions import Literal

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


Expand Down Expand Up @@ -108,7 +109,7 @@ def normalize_currency(currency: str, locale: Locale | str | None = None) -> str
def get_currency_name(
currency: str,
count: float | decimal.Decimal | None = None,
locale: Locale | str | None = LC_NUMERIC,
locale: Locale | str | None = LC_MONETARY,
) -> str:
"""Return the name used by the locale for the specified currency.

Expand Down Expand Up @@ -138,7 +139,7 @@ def get_currency_name(
return loc.currencies.get(currency, currency)


def get_currency_symbol(currency: str, locale: Locale | str | None = LC_NUMERIC) -> str:
def get_currency_symbol(currency: str, locale: Locale | str | None = LC_MONETARY) -> str:
"""Return the symbol used by the locale for the specified currency.

>>> get_currency_symbol('USD', locale='en_US')
Expand Down Expand Up @@ -167,7 +168,7 @@ def get_currency_precision(currency: str) -> int:
def get_currency_unit_pattern(
currency: str,
count: float | decimal.Decimal | None = None,
locale: Locale | str | None = LC_NUMERIC,
locale: Locale | str | None = LC_MONETARY,
) -> str:
"""
Return the unit pattern used for long display of a currency value
Expand Down Expand Up @@ -670,7 +671,7 @@ def format_currency(
number: float | decimal.Decimal | str,
currency: str,
format: str | NumberPattern | None = None,
locale: Locale | str | None = LC_NUMERIC,
locale: Locale | str | None = LC_MONETARY,
currency_digits: bool = True,
format_type: Literal["name", "standard", "accounting"] = "standard",
decimal_quantization: bool = True,
Expand Down Expand Up @@ -792,7 +793,7 @@ def _format_currency_long_name(
number: float | decimal.Decimal | str,
currency: str,
format: str | NumberPattern | None = None,
locale: Locale | str | None = LC_NUMERIC,
locale: Locale | str | None = LC_MONETARY,
currency_digits: bool = True,
format_type: Literal["name", "standard", "accounting"] = "standard",
decimal_quantization: bool = True,
Expand Down Expand Up @@ -835,7 +836,7 @@ def format_compact_currency(
currency: str,
*,
format_type: Literal["short"] = "short",
locale: Locale | str | None = LC_NUMERIC,
locale: Locale | str | None = LC_MONETARY,
fraction_digits: int = 0,
numbering_system: Literal["default"] | str = "latn",
) -> str:
Expand Down