Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/humanize/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__lazy_modules__ = {"humanize.i18n"}

from humanize.i18n import _gettext as _
from humanize.i18n import decimal_separator

suffixes = {
"decimal": (
Expand Down Expand Up @@ -106,5 +107,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 = 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])
18 changes: 18 additions & 0 deletions tests/test_filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

import gettext

import pytest

import humanize
Expand Down Expand Up @@ -101,3 +103,19 @@ 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:
"""Locales with a comma radix should not keep a dotted mantissa.

Uses a NullTranslations catalog so this does not depend on compiled .mo files.
"""
humanize.i18n._TRANSLATIONS["fr_FR"] = gettext.NullTranslations()
humanize.i18n._CURRENT.locale = "fr_FR"
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"
11 changes: 6 additions & 5 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,11 @@ def test_intword_i18n(locale: str, number: int, expected_result: str) -> None:
("fr_FR", 42, "42 octets"),
("si_LK", 1, "බයිට් 1"),
("si_LK", 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:
Expand All @@ -264,7 +265,7 @@ def test_naturalsize_i18n_binary() -> None:
except FileNotFoundError:
pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh")
else:
assert humanize.naturalsize(3000, binary=True) == "2.9 Kio"
assert humanize.naturalsize(3000, binary=True) == "2,9 Kio"
finally:
humanize.i18n.deactivate()

Expand Down