Skip to content

Commit b0f07f0

Browse files
committed
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 #460
1 parent 785e5dc commit b0f07f0

3 files changed

Lines changed: 32 additions & 7 deletions

File tree

‎src/humanize/filesize.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
__lazy_modules__ = {"humanize.i18n"}
66

77
from humanize.i18n import _gettext as _
8+
from humanize.i18n import decimal_separator
89

910
suffixes = {
1011
"decimal": (
@@ -106,5 +107,10 @@ def naturalsize(
106107
if exp < len(suffix) and abs(float(format % (abs_bytes / (base**exp)))) >= base:
107108
exp += 1
108109
space = "" if gnu else " "
109-
ret: str = format % (bytes_ / (base**exp)) + space + _(suffix[exp - 1])
110-
return ret
110+
formatted = format % (bytes_ / (base**exp))
111+
# Default locales keep ".". Only rewrite when a locale uses another
112+
# decimal separator so the common path stays a single format + concat.
113+
sep = decimal_separator()
114+
if sep != ".":
115+
formatted = formatted.replace(".", sep, 1)
116+
return formatted + space + _(suffix[exp - 1])

‎tests/test_filesize.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
import gettext
6+
57
import pytest
68

79
import humanize
@@ -101,3 +103,19 @@ def test_naturalsize(test_args: list[int] | list[int | bool], expected: str) ->
101103
test_args[0] = f"-{test_args[0]}"
102104

103105
assert humanize.naturalsize(*test_args) == "-" + expected
106+
107+
108+
def test_naturalsize_uses_locale_decimal_separator() -> None:
109+
"""Locales with a comma radix should not keep a dotted mantissa.
110+
111+
Uses a NullTranslations catalog so this does not depend on compiled .mo files.
112+
"""
113+
humanize.i18n._TRANSLATIONS["fr_FR"] = gettext.NullTranslations()
114+
humanize.i18n._CURRENT.locale = "fr_FR"
115+
try:
116+
assert humanize.naturalsize(1_500_000) == "1,5 MB"
117+
assert humanize.naturalsize(3000, gnu=True) == "2,9K"
118+
assert humanize.naturalsize(3000, binary=True) == "2,9 KiB"
119+
finally:
120+
humanize.i18n.deactivate()
121+
assert humanize.naturalsize(1_500_000) == "1.5 MB"

‎tests/test_i18n.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,11 @@ def test_intword_i18n(locale: str, number: int, expected_result: str) -> None:
241241
("fr_FR", 42, "42 octets"),
242242
("si_LK", 1, "බයිට් 1"),
243243
("si_LK", 42, "බයිට් 42"),
244-
("fr_FR", 42_000, "42.0 Ko"),
245-
("fr_FR", 42_000_000, "42.0 Mo"),
246-
("fr_FR", 42_000_000_000, "42.0 Go"),
247-
("fr_FR", -42_000, "-42.0 Ko"),
244+
("fr_FR", 42_000, "42,0 Ko"),
245+
("fr_FR", 42_000_000, "42,0 Mo"),
246+
("fr_FR", 42_000_000_000, "42,0 Go"),
247+
("fr_FR", -42_000, "-42,0 Ko"),
248+
("fr_FR", 1_500_000, "1,5 Mo"),
248249
],
249250
)
250251
def test_naturalsize_i18n(locale: str, value: float, expected_result: str) -> None:
@@ -264,7 +265,7 @@ def test_naturalsize_i18n_binary() -> None:
264265
except FileNotFoundError:
265266
pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh")
266267
else:
267-
assert humanize.naturalsize(3000, binary=True) == "2.9 Kio"
268+
assert humanize.naturalsize(3000, binary=True) == "2,9 Kio"
268269
finally:
269270
humanize.i18n.deactivate()
270271

0 commit comments

Comments
 (0)