Add i18n support for naturalsize() and French translation#294
Open
Yibomao wants to merge 4 commits intopython-humanize:mainfrom
Open
Add i18n support for naturalsize() and French translation#294Yibomao wants to merge 4 commits intopython-humanize:mainfrom
naturalsize() and French translation#294Yibomao wants to merge 4 commits intopython-humanize:mainfrom
Conversation
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
naturalsize() and French translation
hugovk
requested changes
Feb 16, 2026
Member
There was a problem hiding this comment.
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"), |
Member
There was a problem hiding this comment.
You've included the space in the translated text here, but not in the .mo, so they won't match up.
Member
There was a problem hiding this comment.
These binary "KiB", "MiB", ... values are missing from the .mo.
| def naturalsize( | ||
| value: float | str, | ||
| binary: bool = False, | ||
| gnu: bool = False, |
Member
There was a problem hiding this comment.
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)) |
Member
There was a problem hiding this comment.
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #51
This PR adds i18n support to the naturalsize() function so that file size units
are translated when a locale is activated.
Changes:
All tests pass locally (715 passed, 22 skipped).