Skip to content

Commit

Permalink
fix: django money in arabic
Browse files Browse the repository at this point in the history
  • Loading branch information
omargawdat committed Feb 27, 2025
1 parent daf9003 commit a8ae747
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions common/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class CoreConfig(AppConfig):
name = "common"

def ready(self):
import common.money_patches # noqa

# this code is for importing admins
for app_config in apps.get_app_configs():
admin_dir = Path(app_config.path) / "admin"
Expand Down
51 changes: 51 additions & 0 deletions common/money_patches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from django.utils import numberformat

original_format = numberformat.format


def patched_number_format(
number,
decimal_sep,
decimal_pos=None,
grouping=0,
thousand_sep="",
force_grouping=False, # noqa: FBT002
use_l10n=None,
):
"""
Fix for Django Money formatting in Arabic locale - django-money
"""
str_number = str(number)

if "\u200f" in str_number and "." in str_number:
if "\xa0" in str_number:
num_part, currency_part = str_number.split("\xa0", 1)

if "." in num_part and num_part.count(".") > 1:
parts = num_part.split(".")
num_part = parts[0] + "." + parts[1]

formatted_num = original_format(
num_part,
decimal_sep,
decimal_pos,
grouping,
thousand_sep,
force_grouping,
use_l10n,
)

return f"{formatted_num}\xa0{currency_part}"

return original_format(
str_number,
decimal_sep,
decimal_pos,
grouping,
thousand_sep,
force_grouping,
use_l10n,
)


numberformat.format = patched_number_format
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ ignore = [
"ISC001",
"T201",
"PGH003",
"PLR0913"

]

Expand Down

0 comments on commit a8ae747

Please sign in to comment.