diff --git a/src/humanize/filesize.py b/src/humanize/filesize.py index 386752b2..ebce042a 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]) + mantissa: str = format % (bytes_ / (base**exp)) + decimal_sep = decimal_separator() + if decimal_sep != ".": + mantissa = mantissa.replace(".", decimal_sep, 1) + + ret: str = mantissa + space + _(suffix[exp - 1]) return ret diff --git a/tests/test_filesize.py b/tests/test_filesize.py index 645751b9..47d75221 100644 --- a/tests/test_filesize.py +++ b/tests/test_filesize.py @@ -101,3 +101,13 @@ 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() -> None: + humanize.i18n.activate("fr_FR") + try: + assert humanize.naturalsize(1_500_000) == "1,5 Mo" + 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..639a89c6 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -304,10 +304,10 @@ 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"), ], ) def test_naturalsize_i18n(locale: str, value: float, expected_result: str) -> None: @@ -327,7 +327,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()