Add i18n support for naturalsize() and French translation#294
Add i18n support for naturalsize() and French translation#294Yibomao wants to merge 4 commits intopython-humanize:mainfrom
naturalsize() and French translation#294Conversation
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
naturalsize() and French translation
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 ===============================
| from math import log | ||
|
|
||
| suffixes = { | ||
| from humanize.i18n import gettext as _ |
There was a problem hiding this comment.
| from humanize.i18n import gettext as _ | |
| from humanize.i18n import _gettext as _ |
| _(" 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"), |
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.
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, |
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)) |
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: |
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).