Skip to content

Comments

Add i18n support for naturalsize() and French translation#294

Merged
hugovk merged 6 commits intopython-humanize:mainfrom
Yibomao:fix-french-filesize-i18n
Feb 22, 2026
Merged

Add i18n support for naturalsize() and French translation#294
hugovk merged 6 commits intopython-humanize:mainfrom
Yibomao:fix-french-filesize-i18n

Conversation

@Yibomao
Copy link
Contributor

@Yibomao Yibomao commented Feb 16, 2026

Fixes #51

This PR adds i18n support to the naturalsize() function so that file size units
are translated when a locale is activated.

Changes:

  • Added gettext support to naturalsize()
  • Ensured French file size units (octets, Ko, Mo) are translated
  • Preserved default English behavior when no locale is active

All tests pass locally (715 passed, 22 skipped).

@hugovk hugovk added the changelog: Added For new features label Feb 16, 2026
@hugovk hugovk changed the title Fix French i18n support for naturalsize() Add i18n support for naturalsize() and French translation Feb 16, 2026
Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, but there's a bit too much going on here. Please don't make breaking changes to the API, and don't refactor it.

We only need to run the _() function at call time inside naturalsize(), and not at the module level.

All tests pass locally (715 passed, 22 skipped).

The CI disagrees:

============================== 5 errors in 0.35s ===============================

Comment on lines 11 to 32
_(" 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"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've included the space in the translated text here, but not in the .mo, so they won't match up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These binary "KiB", "MiB", ... values are missing from the .mo.

def naturalsize(
value: float | str,
binary: bool = False,
gnu: bool = False,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't remove this, it's a breaking API change.

bytes_ = float(value)
abs_bytes = abs(bytes_)
base = 1024 if binary else 1000
exp = int(log(bytes_value, base))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work for negative numbers:

>>> from math import log
>>> log(-1)
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    log(-1)
    ~~~^^^^
ValueError: expected a positive input


if bytes_value == 1:
return _("1 Byte")
if bytes_value < 1024:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't hardcode 1024, use base

@codecov
Copy link

codecov bot commented Feb 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.52%. Comparing base (471cd9b) to head (e4e50d5).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #294   +/-   ##
=======================================
  Coverage   99.51%   99.52%           
=======================================
  Files          11       11           
  Lines         831      848   +17     
=======================================
+ Hits          827      844   +17     
  Misses          4        4           
Flag Coverage Δ
macos-latest 97.40% <80.00%> (-0.43%) ⬇️
ubuntu-latest 97.40% <80.00%> (-0.43%) ⬇️
windows-latest 95.75% <90.00%> (-0.16%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hugovk hugovk merged commit b172d67 into python-humanize:main Feb 22, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: Added For new features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Filesize i18n not working for French

2 participants