Optional 'locale' arg to format_money

- Allows override of system locale when generating reports
This commit is contained in:
Oliver Walters 2026-06-19 06:50:30 +00:00
parent 01fb74af25
commit 20a6ec10ee
2 changed files with 10 additions and 2 deletions

View File

@ -187,6 +187,7 @@ def format_money(
decimal_places: Optional[int] = None,
fmt: Optional[str] = None,
include_symbol: bool = True,
locale: Optional[str] = None,
) -> str:
"""Format money object according to the currently set local.
@ -195,6 +196,7 @@ def format_money(
decimal_places (int): Number of decimal places to use
fmt (str): Format pattern according LDML / the babel format pattern syntax (https://babel.pocoo.org/en/latest/numbers.html)
include_symbol (bool): Whether to include the currency symbol in the formatted output
locale (str): Optional locale override (e.g. 'en-us', 'en-gb'). Defaults to the server LANGUAGE_CODE setting.
Returns:
str: The formatted string
@ -202,8 +204,9 @@ def format_money(
Raises:
ValueError: format string is incorrectly specified
"""
language = (None) or settings.LANGUAGE_CODE
language = locale or settings.LANGUAGE_CODE
locale = Locale.parse(translation.to_locale(language))
if fmt:
pattern = parse_pattern(fmt)
else:

View File

@ -260,6 +260,7 @@ def render_currency(
min_decimal_places: Optional[int] = None,
max_decimal_places: Optional[int] = None,
include_symbol: bool = True,
locale: Optional[str] = None,
):
"""Render a currency / Money object to a formatted string (e.g. for reports).
@ -271,6 +272,7 @@ def render_currency(
min_decimal_places: The minimum number of decimal places to render to. If unspecified, uses the PRICING_DECIMAL_PLACES_MIN setting.
max_decimal_places: The maximum number of decimal places to render to. If unspecified, uses the PRICING_DECIMAL_PLACES setting.
include_symbol: If True, include the currency symbol in the output
locale: Optional locale override (e.g. 'en-us', 'en-gb'). Controls symbol vs. code rendering. Defaults to server LANGUAGE_CODE.
"""
if money in [None, '']:
return '-'
@ -327,7 +329,10 @@ def render_currency(
decimal_places = min(decimal_places, max_decimal_places)
return format_money(
money, decimal_places=decimal_places, include_symbol=include_symbol
money,
decimal_places=decimal_places,
include_symbol=include_symbol,
locale=locale,
)