diff --git a/src/humanize/time.py b/src/humanize/time.py index ea91b888..d34c1d81 100644 --- a/src/humanize/time.py +++ b/src/humanize/time.py @@ -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 @@ -657,8 +657,10 @@ 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) @@ -666,7 +668,7 @@ def precisedelta( texts.append(fmt_txt % intcomma(fmt_value)) continue - texts.append(fmt_txt % fmt_value) + texts.append(fmt_txt % shown) if unit == min_unit: break diff --git a/tests/test_i18n.py b/tests/test_i18n.py index aa9655e6..df5934f2 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -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")