Skip to content
Merged
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
49 changes: 26 additions & 23 deletions src/humanize/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@

from math import log

from humanize.i18n import _gettext as _

suffixes = {
"decimal": (
" kB",
" MB",
" GB",
" TB",
" PB",
" EB",
" ZB",
" YB",
" RB",
" QB",
"kB",
"MB",
"GB",
"TB",
"PB",
"EB",
"ZB",
"YB",
"RB",
"QB",
),
"binary": (
" KiB",
" MiB",
" GiB",
" TiB",
" PiB",
" EiB",
" ZiB",
" YiB",
" RiB",
" QiB",
"KiB",
"MiB",
"GiB",
"TiB",
"PiB",
"EiB",
"ZiB",
"YiB",
"RiB",
"QiB",
),
"gnu": "KMGTPEZYRQ",
}
Expand Down Expand Up @@ -89,11 +91,12 @@ def naturalsize(
abs_bytes = abs(bytes_)

if abs_bytes == 1 and not gnu:
return f"{int(bytes_)} Byte"
return _("%d Byte") % int(bytes_)

if abs_bytes < base:
return f"{int(bytes_)}B" if gnu else f"{int(bytes_)} Bytes"
return f"{int(bytes_)}B" if gnu else _("%d Bytes") % int(bytes_)

exp = int(min(log(abs_bytes, base), len(suffix)))
ret: str = format % (bytes_ / (base**exp)) + suffix[exp - 1]
space = "" if gnu else " "
ret: str = format % (bytes_ / (base**exp)) + space + _(suffix[exp - 1])
return ret
70 changes: 70 additions & 0 deletions src/humanize/locale/fr_FR/LC_MESSAGES/humanize.po
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,73 @@ msgstr "hier"
#, python-format
msgid "%s and %s"
msgstr "%s et %s"

# --- filesize units (naturalsize) ---
#, python-format
msgid "%d Byte"
msgstr "%d octet"

#, python-format
msgid "%d Bytes"
msgstr "%d octets"

msgid "kB"
msgstr "Ko"

msgid "MB"
msgstr "Mo"

msgid "GB"
msgstr "Go"

msgid "TB"
msgstr "To"

msgid "PB"
msgstr "Po"

msgid "EB"
msgstr "Eo"

msgid "ZB"
msgstr "Zo"

msgid "YB"
msgstr "Yo"

msgid "RB"
msgstr "Ro"

msgid "QB"
msgstr "Qo"

# --- binary filesize units ---
msgid "KiB"
msgstr "Kio"

msgid "MiB"
msgstr "Mio"

msgid "GiB"
msgstr "Gio"

msgid "TiB"
msgstr "Tio"

msgid "PiB"
msgstr "Pio"

msgid "EiB"
msgstr "Eio"

msgid "ZiB"
msgstr "Zio"

msgid "YiB"
msgstr "Yio"

msgid "RiB"
msgstr "Rio"

msgid "QiB"
msgstr "Qio"
33 changes: 33 additions & 0 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,39 @@ def test_intword_i18n(locale: str, number: int, expected_result: str) -> None:
humanize.i18n.deactivate()


@pytest.mark.parametrize(
"locale, value, expected_result",
[
("fr_FR", 1, "1 octet"),
("fr_FR", 42, "42 octets"),
("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:
try:
humanize.i18n.activate(locale)
except FileNotFoundError:
pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh")
else:
assert humanize.naturalsize(value) == expected_result
finally:
humanize.i18n.deactivate()


def test_naturalsize_i18n_binary() -> None:
try:
humanize.i18n.activate("fr_FR")
except FileNotFoundError:
pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh")
else:
assert humanize.naturalsize(3000, binary=True) == "2.9 Kio"
finally:
humanize.i18n.deactivate()


@pytest.mark.parametrize(
"locale, expected_result",
[
Expand Down
Loading