Skip to content

Commit e4e50d5

Browse files
committed
Add tests and fix Byte/Bytes localisation
1 parent e36e291 commit e4e50d5

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/humanize/filesize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ def naturalsize(
9191
abs_bytes = abs(bytes_)
9292

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

9696
if abs_bytes < base:
97-
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_)
9898

9999
exp = int(min(log(abs_bytes, base), len(suffix)))
100100
space = "" if gnu else " "

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,13 @@ msgid "%s and %s"
365365
msgstr "%s et %s"
366366

367367
# --- filesize units (naturalsize) ---
368-
msgid "Byte"
369-
msgstr "octet"
368+
#, python-format
369+
msgid "%d Byte"
370+
msgstr "%d octet"
370371

371-
msgid "Bytes"
372-
msgstr "octets"
372+
#, python-format
373+
msgid "%d Bytes"
374+
msgstr "%d octets"
373375

374376
msgid "kB"
375377
msgstr "Ko"

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)