From 6c1d0c7dfc87189374b5b9095bfdc97ed218a702 Mon Sep 17 00:00:00 2001 From: Daniel Ching Date: Sun, 19 Jun 2022 20:33:06 -0500 Subject: [PATCH 1/3] BUG: Use %d for year translations convert to string for intcomma after This patch fixes a bug introduced in 3.14.0, where the format string was changed from %d to %s to add separators to the year. However, this needs to happen after translation because the translator uses the format strings as part of the translation. Closes #21 --- src/humanize/time.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/humanize/time.py b/src/humanize/time.py index 373657d..22dbb1e 100644 --- a/src/humanize/time.py +++ b/src/humanize/time.py @@ -202,7 +202,7 @@ def naturaldelta( else: return _ngettext("1 year, %d day", "1 year, %d days", days) % days - return _ngettext("%s year", "%s years", years) % intcomma(years) + return _ngettext("%d year", "%d years", years).replace("%d", "%s") % intcomma(years) def naturaltime( From cfdfb81de8bb8bc02b57ba9ecbacc2f66252ac17 Mon Sep 17 00:00:00 2001 From: Daniel Ching Date: Tue, 21 Jun 2022 21:23:32 -0500 Subject: [PATCH 2/3] TST: Add translation test for naturaldelta --- tests/test_i18n.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 8b64696..b4c57cb 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -52,6 +52,23 @@ def test_intcomma() -> None: humanize.i18n.deactivate() assert humanize.intcomma(number) == "10,000,000" +def test_naturaldelta() -> None: + seconds = 1234 * 365 * 24 * 60 * 60 + + assert humanize.naturaldelta(seconds) == "1,234 years" + + try: + humanize.i18n.activate("fr_FR") + assert humanize.naturaldelta(seconds) == "1 234 ans" + humanize.i18n.activate("es_ES") + assert humanize.naturaldelta(seconds) == "1,234 aƱos" + + except FileNotFoundError: + pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh") + + finally: + humanize.i18n.deactivate() + assert humanize.naturaldelta(seconds) == "1,234 years" @pytest.mark.parametrize( ("locale", "number", "expected_result"), From e34f1ccc070e322f327d314e167144281e5a5af3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Jun 2022 02:24:05 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_i18n.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_i18n.py b/tests/test_i18n.py index b4c57cb..e4f30f0 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -52,6 +52,7 @@ def test_intcomma() -> None: humanize.i18n.deactivate() assert humanize.intcomma(number) == "10,000,000" + def test_naturaldelta() -> None: seconds = 1234 * 365 * 24 * 60 * 60 @@ -70,6 +71,7 @@ def test_naturaldelta() -> None: humanize.i18n.deactivate() assert humanize.naturaldelta(seconds) == "1,234 years" + @pytest.mark.parametrize( ("locale", "number", "expected_result"), (