Skip to content

Commit b172d67

Browse files
Yibomaohugovk
andauthored
Add i18n support for naturalsize() and French translation (#294)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 471cd9b commit b172d67

File tree

3 files changed

+129
-23
lines changed

3 files changed

+129
-23
lines changed

src/humanize/filesize.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,32 @@
44

55
from math import log
66

7+
from humanize.i18n import _gettext as _
8+
79
suffixes = {
810
"decimal": (
9-
" kB",
10-
" MB",
11-
" GB",
12-
" TB",
13-
" PB",
14-
" EB",
15-
" ZB",
16-
" YB",
17-
" RB",
18-
" QB",
11+
"kB",
12+
"MB",
13+
"GB",
14+
"TB",
15+
"PB",
16+
"EB",
17+
"ZB",
18+
"YB",
19+
"RB",
20+
"QB",
1921
),
2022
"binary": (
21-
" KiB",
22-
" MiB",
23-
" GiB",
24-
" TiB",
25-
" PiB",
26-
" EiB",
27-
" ZiB",
28-
" YiB",
29-
" RiB",
30-
" QiB",
23+
"KiB",
24+
"MiB",
25+
"GiB",
26+
"TiB",
27+
"PiB",
28+
"EiB",
29+
"ZiB",
30+
"YiB",
31+
"RiB",
32+
"QiB",
3133
),
3234
"gnu": "KMGTPEZYRQ",
3335
}
@@ -89,11 +91,12 @@ def naturalsize(
8991
abs_bytes = abs(bytes_)
9092

9193
if abs_bytes == 1 and not gnu:
92-
return f"{int(bytes_)} Byte"
94+
return _("%d Byte") % int(bytes_)
9395

9496
if abs_bytes < base:
95-
return f"{int(bytes_)}B" if gnu else f"{int(bytes_)} Bytes"
97+
return f"{int(bytes_)}B" if gnu else _("%d Bytes") % int(bytes_)
9698

9799
exp = int(min(log(abs_bytes, base), len(suffix)))
98-
ret: str = format % (bytes_ / (base**exp)) + suffix[exp - 1]
100+
space = "" if gnu else " "
101+
ret: str = format % (bytes_ / (base**exp)) + space + _(suffix[exp - 1])
99102
return ret

src/humanize/locale/fr_FR/LC_MESSAGES/humanize.po

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,73 @@ msgstr "hier"
363363
#, python-format
364364
msgid "%s and %s"
365365
msgstr "%s et %s"
366+
367+
# --- filesize units (naturalsize) ---
368+
#, python-format
369+
msgid "%d Byte"
370+
msgstr "%d octet"
371+
372+
#, python-format
373+
msgid "%d Bytes"
374+
msgstr "%d octets"
375+
376+
msgid "kB"
377+
msgstr "Ko"
378+
379+
msgid "MB"
380+
msgstr "Mo"
381+
382+
msgid "GB"
383+
msgstr "Go"
384+
385+
msgid "TB"
386+
msgstr "To"
387+
388+
msgid "PB"
389+
msgstr "Po"
390+
391+
msgid "EB"
392+
msgstr "Eo"
393+
394+
msgid "ZB"
395+
msgstr "Zo"
396+
397+
msgid "YB"
398+
msgstr "Yo"
399+
400+
msgid "RB"
401+
msgstr "Ro"
402+
403+
msgid "QB"
404+
msgstr "Qo"
405+
406+
# --- binary filesize units ---
407+
msgid "KiB"
408+
msgstr "Kio"
409+
410+
msgid "MiB"
411+
msgstr "Mio"
412+
413+
msgid "GiB"
414+
msgstr "Gio"
415+
416+
msgid "TiB"
417+
msgstr "Tio"
418+
419+
msgid "PiB"
420+
msgstr "Pio"
421+
422+
msgid "EiB"
423+
msgstr "Eio"
424+
425+
msgid "ZiB"
426+
msgstr "Zio"
427+
428+
msgid "YiB"
429+
msgstr "Yio"
430+
431+
msgid "RiB"
432+
msgstr "Rio"
433+
434+
msgid "QiB"
435+
msgstr "Qio"

tests/test_i18n.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,39 @@ def test_intword_i18n(locale: str, number: int, expected_result: str) -> None:
157157
humanize.i18n.deactivate()
158158

159159

160+
@pytest.mark.parametrize(
161+
"locale, value, expected_result",
162+
[
163+
("fr_FR", 1, "1 octet"),
164+
("fr_FR", 42, "42 octets"),
165+
("fr_FR", 42_000, "42.0 Ko"),
166+
("fr_FR", 42_000_000, "42.0 Mo"),
167+
("fr_FR", 42_000_000_000, "42.0 Go"),
168+
("fr_FR", -42_000, "-42.0 Ko"),
169+
],
170+
)
171+
def test_naturalsize_i18n(locale: str, value: float, expected_result: str) -> None:
172+
try:
173+
humanize.i18n.activate(locale)
174+
except FileNotFoundError:
175+
pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh")
176+
else:
177+
assert humanize.naturalsize(value) == expected_result
178+
finally:
179+
humanize.i18n.deactivate()
180+
181+
182+
def test_naturalsize_i18n_binary() -> None:
183+
try:
184+
humanize.i18n.activate("fr_FR")
185+
except FileNotFoundError:
186+
pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh")
187+
else:
188+
assert humanize.naturalsize(3000, binary=True) == "2.9 Kio"
189+
finally:
190+
humanize.i18n.deactivate()
191+
192+
160193
@pytest.mark.parametrize(
161194
"locale, expected_result",
162195
[

0 commit comments

Comments
 (0)