Skip to content

Commit cf33fbf

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 a950045 commit cf33fbf

3 files changed

Lines changed: 36 additions & 7 deletions

File tree

‎src/humanize/filesize.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from humanize.i18n import _gettext as _
88
from humanize.i18n import _gettext_noop as N_
9+
from humanize.i18n import decimal_separator
910

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

‎tests/test_filesize.py‎

Lines changed: 22 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,23 @@ 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(
109+
monkeypatch: pytest.MonkeyPatch,
110+
) -> None:
111+
"""Locales with a comma radix should not keep a dotted mantissa.
112+
113+
Uses a NullTranslations catalog so this does not depend on compiled .mo files.
114+
"""
115+
monkeypatch.setitem(
116+
humanize.i18n._TRANSLATIONS, "fr_FR", gettext.NullTranslations()
117+
)
118+
monkeypatch.setattr(humanize.i18n._CURRENT, "locale", "fr_FR", raising=False)
119+
try:
120+
assert humanize.naturalsize(1_500_000) == "1,5 MB"
121+
assert humanize.naturalsize(3000, gnu=True) == "2,9K"
122+
assert humanize.naturalsize(3000, binary=True) == "2,9 KiB"
123+
finally:
124+
humanize.i18n.deactivate()
125+
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
@@ -304,10 +304,11 @@ def test_intword_i18n(locale: str, number: int, expected_result: str) -> None:
304304
("si_LK", 42, "බයිට් 42"),
305305
("bn_BD", 1, "1 বাইট"),
306306
("bn_BD", 42, "42 বাইট"),
307-
("fr_FR", 42_000, "42.0 Ko"),
308-
("fr_FR", 42_000_000, "42.0 Mo"),
309-
("fr_FR", 42_000_000_000, "42.0 Go"),
310-
("fr_FR", -42_000, "-42.0 Ko"),
307+
("fr_FR", 42_000, "42,0 Ko"),
308+
("fr_FR", 42_000_000, "42,0 Mo"),
309+
("fr_FR", 42_000_000_000, "42,0 Go"),
310+
("fr_FR", -42_000, "-42,0 Ko"),
311+
("fr_FR", 1_500_000, "1,5 Mo"),
311312
],
312313
)
313314
def test_naturalsize_i18n(locale: str, value: float, expected_result: str) -> None:
@@ -327,7 +328,7 @@ def test_naturalsize_i18n_binary() -> None:
327328
except FileNotFoundError:
328329
pytest.skip("Generate .mo with tox -e compile-translations")
329330
else:
330-
assert humanize.naturalsize(3000, binary=True) == "2.9 Kio"
331+
assert humanize.naturalsize(3000, binary=True) == "2,9 Kio"
331332
finally:
332333
humanize.i18n.deactivate()
333334

0 commit comments

Comments
 (0)