Skip to content

Commit bada0f8

Browse files
BenYang12hugovk
andauthored
Fix translation loss during catalog updates (#391)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent e46732e commit bada0f8

5 files changed

Lines changed: 121 additions & 17 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,12 @@ FileNotFoundError: [Errno 2] No translation file found for domain: 'humanize'
233233
How to add new phrases to existing locale files:
234234

235235
```sh
236-
xgettext --from-code=UTF-8 -o humanize.pot -k'_' -k'N_' -k'P_:1c,2' -k'NS_:1,2' -k'_ngettext:1,2' -l python src/humanize/*.py # extract new phrases
237-
msgmerge -U src/humanize/locale/ru_RU/LC_MESSAGES/humanize.po humanize.pot # add them to locale files
236+
scripts/update-translations.sh
238237
```
239238

239+
This extracts new phrases from the source code into `humanize.pot`, merges them into
240+
each locale's `.po` file and compiles the binary `.mo` catalogs.
241+
240242
How to add a new locale:
241243

242244
```sh

‎scripts/update-translations.sh‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
set -e
22

33
# extract new phrases
4-
xgettext --from-code=UTF-8 -o humanize.pot -k'_' -k'N_' -k'P_:1c,2' -k'NS_:1,2' -k'_ngettext:1,2' -l python src/humanize/*.py
4+
xgettext \
5+
--from-code=UTF-8 \
6+
-o humanize.pot \
7+
-k'_' \
8+
-k'N_' \
9+
-k'P_:1c,2' \
10+
-k'PS_:1c,2' \
11+
-k'NS_:1,2' \
12+
-k'_ngettext:1,2' \
13+
-l python \
14+
src/humanize/*.py
515

616
for d in src/humanize/locale/*/; do
717
locale="$(basename $d)"

‎src/humanize/i18n.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,19 @@ def num_name(n):
179179
return singular, plural
180180

181181

182+
def _pgettext_noop(msgctxt: str, message: str) -> tuple[str, str]:
183+
"""Mark a contextual translation without translating it.
184+
185+
Args:
186+
msgctxt (str): Context of the translation.
187+
message (str): Text to translate in the future.
188+
189+
Returns:
190+
tuple: Original context and text, unchanged.
191+
"""
192+
return msgctxt, message
193+
194+
182195
def thousands_separator() -> str:
183196
"""Return the thousands separator for a locale, default to comma.
184197

‎src/humanize/number.py‎

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import bisect
88

99
from .i18n import _gettext as _
10+
from .i18n import _gettext_noop as N_
1011
from .i18n import _ngettext, decimal_separator, thousands_separator
1112
from .i18n import _ngettext_noop as NS_
1213
from .i18n import _pgettext as P_
14+
from .i18n import _pgettext_noop as PS_
1315

1416
TYPE_CHECKING = False
1517
if TYPE_CHECKING:
@@ -35,18 +37,43 @@
3537
}
3638
_SUPERSCRIPT_TRANS = str.maketrans(_SUPERSCRIPT_MAP)
3739

38-
_ORDINAL_SUFFIXES = ("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th")
40+
_ORDINAL_SUFFIXES = {
41+
"male": (
42+
PS_("0 (male)", "th"),
43+
PS_("1 (male)", "st"),
44+
PS_("2 (male)", "nd"),
45+
PS_("3 (male)", "rd"),
46+
PS_("4 (male)", "th"),
47+
PS_("5 (male)", "th"),
48+
PS_("6 (male)", "th"),
49+
PS_("7 (male)", "th"),
50+
PS_("8 (male)", "th"),
51+
PS_("9 (male)", "th"),
52+
),
53+
"female": (
54+
PS_("0 (female)", "th"),
55+
PS_("1 (female)", "st"),
56+
PS_("2 (female)", "nd"),
57+
PS_("3 (female)", "rd"),
58+
PS_("4 (female)", "th"),
59+
PS_("5 (female)", "th"),
60+
PS_("6 (female)", "th"),
61+
PS_("7 (female)", "th"),
62+
PS_("8 (female)", "th"),
63+
PS_("9 (female)", "th"),
64+
),
65+
}
3966
_APNUMBER_WORDS = (
40-
"zero",
41-
"one",
42-
"two",
43-
"three",
44-
"four",
45-
"five",
46-
"six",
47-
"seven",
48-
"eight",
49-
"nine",
67+
N_("zero"),
68+
N_("one"),
69+
N_("two"),
70+
N_("three"),
71+
N_("four"),
72+
N_("five"),
73+
N_("six"),
74+
N_("seven"),
75+
N_("eight"),
76+
N_("nine"),
5077
)
5178

5279

@@ -110,7 +137,7 @@ def ordinal(value: NumberOrString, gender: str = "male") -> str:
110137
return str(value)
111138
gender = "male" if gender == "male" else "female"
112139
digit = 0 if value % 100 in (11, 12, 13) else value % 10
113-
return f"{value}{P_(f'{digit} ({gender})', _ORDINAL_SUFFIXES[digit])}"
140+
return f"{value}{P_(*_ORDINAL_SUFFIXES[gender][digit])}"
114141

115142

116143
def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:

‎tests/test_i18n.py‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,72 @@
33
from __future__ import annotations
44

55
import datetime as dt
6+
import gettext
67
import importlib
7-
import pathlib
8+
import shutil
9+
import subprocess
10+
from pathlib import Path
811

912
import pytest
1013
from freezegun import freeze_time
1114

1215
import humanize
1316

14-
LOCALE_DIR = pathlib.Path(humanize.i18n.__file__).parent / "locale"
17+
LOCALE_DIR = Path(humanize.i18n.__file__).parent / "locale"
1518

1619
with freeze_time("2020-02-02"):
1720
NOW = dt.datetime.now(tz=dt.timezone.utc)
1821

1922

23+
@pytest.mark.parametrize("locale, one", [("de_DE", "eins"), ("fr_FR", "un")])
24+
def test_update_translations(
25+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, locale: str, one: str
26+
) -> None:
27+
for command in ("bash", "xgettext", "msgmerge", "msgfmt"):
28+
if shutil.which(command) is None:
29+
pytest.skip(f"Translation updates require {command}")
30+
31+
root = Path(__file__).resolve().parents[1]
32+
source = root / "src" / "humanize"
33+
destination = tmp_path / "src" / "humanize"
34+
messages = destination / "locale" / locale / "LC_MESSAGES"
35+
messages.mkdir(parents=True)
36+
for path in source.glob("*.py"):
37+
shutil.copy2(path, destination)
38+
catalog = messages / "humanize.po"
39+
shutil.copy2(source / "locale" / locale / "LC_MESSAGES" / "humanize.po", catalog)
40+
binary = catalog.with_suffix(".mo")
41+
subprocess.run(["msgfmt", "--check", "-o", str(binary), str(catalog)], check=True)
42+
43+
results = []
44+
try:
45+
for updated in (False, True):
46+
if updated:
47+
subprocess.run(
48+
["bash", str(root / "scripts" / "update-translations.sh")],
49+
cwd=tmp_path,
50+
check=True,
51+
)
52+
with binary.open("rb") as stream:
53+
translation = gettext.GNUTranslations(stream)
54+
# Replace the cached catalog so the second pass uses the updated one.
55+
monkeypatch.setitem(humanize.i18n._TRANSLATIONS, locale, translation)
56+
humanize.activate(locale)
57+
results.append(
58+
[humanize.apnumber(value) for value in range(10)]
59+
+ [
60+
humanize.ordinal(value, gender=gender)
61+
for gender in ("male", "female")
62+
for value in range(10)
63+
]
64+
)
65+
finally:
66+
humanize.deactivate()
67+
68+
assert results[0][1] == one
69+
assert results[1] == results[0]
70+
71+
2072
@freeze_time("2020-02-02")
2173
def test_i18n() -> None:
2274
three_seconds = NOW - dt.timedelta(seconds=3)

0 commit comments

Comments
 (0)