diff --git a/src/humanize/filesize.py b/src/humanize/filesize.py index 13e5edd..c495fed 100644 --- a/src/humanize/filesize.py +++ b/src/humanize/filesize.py @@ -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", } @@ -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 diff --git a/src/humanize/locale/fr_FR/LC_MESSAGES/humanize.po b/src/humanize/locale/fr_FR/LC_MESSAGES/humanize.po index 6aae582..e9a4220 100644 --- a/src/humanize/locale/fr_FR/LC_MESSAGES/humanize.po +++ b/src/humanize/locale/fr_FR/LC_MESSAGES/humanize.po @@ -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" diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 59e896c..3a4c2b9 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -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", [