Skip to content

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

Open
Yibomao wants to merge 4 commits intopython-humanize:mainfrom
Yibomao:fix-french-filesize-i18n
Open

Add i18n support for naturalsize() and French translation#294
Yibomao wants to merge 4 commits intopython-humanize:mainfrom
Yibomao:fix-french-filesize-i18n

Conversation

@Yibomao
Copy link

@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 ===============================

from math import log

suffixes = {
from humanize.i18n import gettext as _
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
from humanize.i18n import gettext as _
from humanize.i18n import _gettext as _

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

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