Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/humanize/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from functools import total_ordering

from .i18n import _gettext as _
from .i18n import _ngettext
from .i18n import _ngettext, decimal_separator
from .number import intcomma

TYPE_CHECKING = False
Expand Down Expand Up @@ -657,16 +657,18 @@ def precisedelta(
if fmt_value > 0 or (not texts and unit == min_unit):
_fmt_value = 2 if 1 < fmt_value < 2 else int(fmt_value)
fmt_txt = _ngettext(singular_txt, plural_txt, _fmt_value)
shown: float | str = fmt_value
if unit == min_unit and math.modf(fmt_value)[0] > 0:
fmt_txt = fmt_txt.replace("%d", format)
fmt_txt = fmt_txt.replace("%d", "%s")
shown = (format % fmt_value).replace(".", decimal_separator())
elif unit == YEARS:
if math.modf(fmt_value)[0] == 0:
fmt_value = int(fmt_value)
fmt_txt = fmt_txt.replace("%d", "%s")
texts.append(fmt_txt % intcomma(fmt_value))
continue

texts.append(fmt_txt % fmt_value)
texts.append(fmt_txt % shown)

if unit == min_unit:
break
Expand Down
25 changes: 25 additions & 0 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,31 @@ def test_naturalsize_i18n(locale: str, value: float, expected_result: str) -> No
humanize.i18n.deactivate()


@pytest.mark.parametrize(
"locale, value, kwargs, expected_result",
[
("en_US", 3661.5, {}, "1 hour, 1 minute and 1.50 seconds"),
("de_DE", 3661.5, {}, "1 Stunde, 1 Minute und 1,50 Sekunden"),
("fr_FR", 3661.5, {}, "1 heure, 1 minute et 1,50 secondes"),
("de_DE", 1.5, {}, "1,50 Sekunden"),
("de_DE", 3661.5, {"format": "%0.1f"}, "1 Stunde, 1 Minute und 1,5 Sekunden"),
("de_DE", 90, {}, "1 Minute und 30 Sekunden"),
("es_ES", 3661.5, {}, "1 hora, 1 minuto y 1.50 segundos"),
],
)
def test_precisedelta_i18n_decimal_separator(
locale: str, value: float, kwargs: dict[str, str], expected_result: str
) -> None:
try:
humanize.i18n.activate(locale)
except FileNotFoundError:
pytest.skip("Generate .mo with tox -e compile-translations")
else:
assert humanize.precisedelta(value, **kwargs) == expected_result
finally:
humanize.i18n.deactivate()


def test_naturalsize_i18n_binary() -> None:
try:
humanize.i18n.activate("fr_FR")
Expand Down