|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import datetime as dt |
| 6 | +import gettext |
6 | 7 | import importlib |
7 | | -import pathlib |
| 8 | +import shutil |
| 9 | +import subprocess |
| 10 | +from pathlib import Path |
8 | 11 |
|
9 | 12 | import pytest |
10 | 13 | from freezegun import freeze_time |
11 | 14 |
|
12 | 15 | import humanize |
13 | 16 |
|
14 | | -LOCALE_DIR = pathlib.Path(humanize.i18n.__file__).parent / "locale" |
| 17 | +LOCALE_DIR = Path(humanize.i18n.__file__).parent / "locale" |
15 | 18 |
|
16 | 19 | with freeze_time("2020-02-02"): |
17 | 20 | NOW = dt.datetime.now(tz=dt.timezone.utc) |
18 | 21 |
|
19 | 22 |
|
| 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 | + |
20 | 72 | @freeze_time("2020-02-02") |
21 | 73 | def test_i18n() -> None: |
22 | 74 | three_seconds = NOW - dt.timedelta(seconds=3) |
|
0 commit comments