From cf33fbf048f3b084fa0e129c533224dfbf327d03 Mon Sep 17 00:00:00 2001 From: ANSHUL SINGH <72524975+ekanshul@users.noreply.github.com> Date: Tue, 20 Oct 2026 17:36:00 +0000 Subject: [PATCH] Honor the locale decimal separator in naturalsize() intcomma() and intword() already swap "." for decimal_separator(). naturalsize() left the mantissa dotted, so fr_FR / de_DE / it_IT showed "1.5 Mo" instead of "1,5 Mo". Rewrite only when the locale separator is not ".", so the default English path stays a single format + concat. Fixes python-humanize/humanize#460 --- src/humanize/filesize.py | 10 ++++++++-- tests/test_filesize.py | 22 ++++++++++++++++++++++ tests/test_i18n.py | 11 ++++++----- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/humanize/filesize.py b/src/humanize/filesize.py index 386752b2..eed75a55 100644 --- a/src/humanize/filesize.py +++ b/src/humanize/filesize.py @@ -6,6 +6,7 @@ from humanize.i18n import _gettext as _ from humanize.i18n import _gettext_noop as N_ +from humanize.i18n import decimal_separator suffixes = { "decimal": ( @@ -107,5 +108,10 @@ def naturalsize( if exp < len(suffix) and abs(float(format % (abs_bytes / (base**exp)))) >= base: exp += 1 space = "" if gnu else " " - ret: str = format % (bytes_ / (base**exp)) + space + _(suffix[exp - 1]) - return ret + formatted: str = format % (bytes_ / (base**exp)) + # Default locales keep ".". Only rewrite when a locale uses another + # decimal separator so the common path stays a single format + concat. + sep = decimal_separator() + if sep != ".": + formatted = formatted.replace(".", sep, 1) + return formatted + space + _(suffix[exp - 1]) diff --git a/tests/test_filesize.py b/tests/test_filesize.py index 645751b9..89869693 100644 --- a/tests/test_filesize.py +++ b/tests/test_filesize.py @@ -2,6 +2,8 @@ from __future__ import annotations +import gettext + import pytest import humanize @@ -101,3 +103,23 @@ def test_naturalsize(test_args: list[int] | list[int | bool], expected: str) -> test_args[0] = f"-{test_args[0]}" assert humanize.naturalsize(*test_args) == "-" + expected + + +def test_naturalsize_uses_locale_decimal_separator( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Locales with a comma radix should not keep a dotted mantissa. + + Uses a NullTranslations catalog so this does not depend on compiled .mo files. + """ + monkeypatch.setitem( + humanize.i18n._TRANSLATIONS, "fr_FR", gettext.NullTranslations() + ) + monkeypatch.setattr(humanize.i18n._CURRENT, "locale", "fr_FR", raising=False) + try: + assert humanize.naturalsize(1_500_000) == "1,5 MB" + assert humanize.naturalsize(3000, gnu=True) == "2,9K" + assert humanize.naturalsize(3000, binary=True) == "2,9 KiB" + finally: + humanize.i18n.deactivate() + assert humanize.naturalsize(1_500_000) == "1.5 MB" diff --git a/tests/test_i18n.py b/tests/test_i18n.py index aa9655e6..057b55e9 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -304,10 +304,11 @@ def test_intword_i18n(locale: str, number: int, expected_result: str) -> None: ("si_LK", 42, "බයිට් 42"), ("bn_BD", 1, "1 বাইট"), ("bn_BD", 42, "42 বাইট"), - ("fr_FR", 42_000, "42.0 Ko"), - ("fr_FR", 42_000_000, "42.0 Mo"), - ("fr_FR", 42_000_000_000, "42.0 Go"), - ("fr_FR", -42_000, "-42.0 Ko"), + ("fr_FR", 42_000, "42,0 Ko"), + ("fr_FR", 42_000_000, "42,0 Mo"), + ("fr_FR", 42_000_000_000, "42,0 Go"), + ("fr_FR", -42_000, "-42,0 Ko"), + ("fr_FR", 1_500_000, "1,5 Mo"), ], ) def test_naturalsize_i18n(locale: str, value: float, expected_result: str) -> None: @@ -327,7 +328,7 @@ def test_naturalsize_i18n_binary() -> None: except FileNotFoundError: pytest.skip("Generate .mo with tox -e compile-translations") else: - assert humanize.naturalsize(3000, binary=True) == "2.9 Kio" + assert humanize.naturalsize(3000, binary=True) == "2,9 Kio" finally: humanize.i18n.deactivate()