Skip to content

Commit ecc61d9

Browse files
committed
Honor custom catalog paths when activating translations
1 parent 785e5dc commit ecc61d9

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

‎src/humanize/i18n.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def _get_default_locale_path() -> pathlib.Path | None:
5151

5252

5353
def get_translation() -> gettext_module.NullTranslations:
54-
return _TRANSLATIONS.get(getattr(_CURRENT, "locale", None), _TRANSLATIONS[None])
54+
return getattr(_CURRENT, "translation", _TRANSLATIONS[None])
5555

5656

5757
def activate(
@@ -74,8 +74,10 @@ def activate(
7474
"""
7575
if locale is None or locale.startswith("en"):
7676
_CURRENT.locale = None
77+
_CURRENT.translation = _TRANSLATIONS[None]
7778
return _TRANSLATIONS[None]
7879

80+
default_path = path is None
7981
if path is None:
8082
path = _get_default_locale_path()
8183

@@ -85,16 +87,21 @@ def activate(
8587
"You need to pass the path explicitly."
8688
)
8789
raise FileNotFoundError(msg)
88-
if locale not in _TRANSLATIONS:
90+
if default_path and locale in _TRANSLATIONS:
91+
translation = _TRANSLATIONS[locale]
92+
else:
8993
translation = gettext_module.translation("humanize", path, [locale])
90-
_TRANSLATIONS[locale] = translation
94+
if default_path:
95+
_TRANSLATIONS[locale] = translation
9196
_CURRENT.locale = locale
92-
return _TRANSLATIONS[locale]
97+
_CURRENT.translation = translation
98+
return translation
9399

94100

95101
def deactivate() -> None:
96102
"""Deactivate internationalisation."""
97103
_CURRENT.locale = None
104+
_CURRENT.translation = _TRANSLATIONS[None]
98105

99106

100107
def _gettext(message: str) -> str:

‎tests/test_i18n.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import gettext
77
import importlib
88
import shutil
9+
import struct
910
import subprocess
1011
from pathlib import Path
1112

@@ -20,6 +21,34 @@
2021
NOW = dt.datetime.now(tz=dt.UTC)
2122

2223

24+
def test_activate_uses_catalog_from_requested_path(tmp_path: Path) -> None:
25+
def write_catalog(root: Path, translated: str) -> None:
26+
directory = root / "zz" / "LC_MESSAGES"
27+
directory.mkdir(parents=True)
28+
original = b"one"
29+
translation = translated.encode("utf-8")
30+
data = struct.pack("<7I", 0x950412DE, 0, 1, 28, 36, 0, 0)
31+
data += struct.pack("<2I", len(original), 44)
32+
data += struct.pack("<2I", len(translation), 48)
33+
data += original + b"\0" + translation + b"\0"
34+
(directory / "humanize.mo").write_bytes(data)
35+
36+
first = tmp_path / "first"
37+
second = tmp_path / "second"
38+
write_catalog(first, "first")
39+
write_catalog(second, "second")
40+
try:
41+
first_translation = humanize.activate("zz", first)
42+
assert humanize.apnumber(1) == "first"
43+
second_translation = humanize.activate("zz", second)
44+
assert humanize.apnumber(1) == "second"
45+
assert first_translation is not second_translation
46+
humanize.activate("zz", first)
47+
assert humanize.apnumber(1) == "first"
48+
finally:
49+
humanize.deactivate()
50+
51+
2352
@pytest.mark.parametrize("locale, one", [("de_DE", "eins"), ("fr_FR", "un")])
2453
def test_update_translations(
2554
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, locale: str, one: str

0 commit comments

Comments
 (0)