Skip to content

Commit 9627694

Browse files
author
Daniel Gillet
committed
Fix i18n _get_default_locale_path
Fixes #105 Use importlib.resources available since Python 3.9 to find the location of the locale folder containing the translations.
1 parent c5dcf10 commit 9627694

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

src/humanize/i18n.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
from __future__ import annotations
44

55
import gettext as gettext_module
6-
import os.path
6+
import importlib.resources
7+
import os
8+
import pathlib
79
from threading import local
810

911
__all__ = ["activate", "deactivate", "decimal_separator", "thousands_separator"]
@@ -32,14 +34,14 @@
3234
}
3335

3436

35-
def _get_default_locale_path() -> str | None:
36-
try:
37-
if __file__ is None:
38-
return None
39-
return os.path.join(os.path.dirname(__file__), "locale")
40-
except NameError:
37+
def _get_default_locale_path() -> pathlib.Path | None:
38+
package = __spec__ and __spec__.parent
39+
if not package:
4140
return None
4241

42+
with importlib.resources.as_file(importlib.resources.files(package)) as pkg:
43+
return pkg / "locale"
44+
4345

4446
def get_translation() -> gettext_module.NullTranslations:
4547
try:
@@ -48,14 +50,16 @@ def get_translation() -> gettext_module.NullTranslations:
4850
return _TRANSLATIONS[None]
4951

5052

51-
def activate(locale: str, path: str | None = None) -> gettext_module.NullTranslations:
53+
def activate(
54+
locale: str, path: str | os.PathLike[str] | None = None
55+
) -> gettext_module.NullTranslations:
5256
"""Activate internationalisation.
5357
5458
Set `locale` as current locale. Search for locale in directory `path`.
5559
5660
Args:
5761
locale (str): Language name, e.g. `en_GB`.
58-
path (str): Path to search for locales.
62+
path (str | pathlib.Path): Path to search for locales.
5963
6064
Returns:
6165
dict: Translations.

tests/test_i18n.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,20 @@ def test_ordinal_genders(
156156
humanize.i18n.deactivate()
157157

158158

159-
def test_default_locale_path_defined__file__() -> None:
159+
def test_default_locale_path_defined__spec__() -> None:
160160
i18n = importlib.import_module("humanize.i18n")
161161
assert i18n._get_default_locale_path() is not None
162162

163163

164-
def test_default_locale_path_null__file__() -> None:
164+
def test_default_locale_path_none__spec__(monkeypatch: pytest.MonkeyPatch) -> None:
165165
i18n = importlib.import_module("humanize.i18n")
166-
i18n.__file__ = None
166+
monkeypatch.setattr(i18n, "__spec__", None)
167167
assert i18n._get_default_locale_path() is None
168168

169169

170-
def test_default_locale_path_undefined__file__() -> None:
170+
def test_default_locale_path_undefined__file__(monkeypatch: pytest.MonkeyPatch) -> None:
171171
i18n = importlib.import_module("humanize.i18n")
172-
del i18n.__file__
172+
monkeypatch.delattr(i18n, "__spec__")
173173
assert i18n._get_default_locale_path() is None
174174

175175

@@ -179,17 +179,21 @@ class TestActivate:
179179
" 'locale' folder. You need to pass the path explicitly."
180180
)
181181

182-
def test_default_locale_path_null__file__(self) -> None:
182+
def test_default_locale_path_null__spec__(
183+
self, monkeypatch: pytest.MonkeyPatch
184+
) -> None:
183185
i18n = importlib.import_module("humanize.i18n")
184-
i18n.__file__ = None
186+
monkeypatch.setattr(i18n, "__spec__", None)
185187

186188
with pytest.raises(Exception) as excinfo:
187189
i18n.activate("ru_RU")
188190
assert str(excinfo.value) == self.expected_msg
189191

190-
def test_default_locale_path_undefined__file__(self) -> None:
192+
def test_default_locale_path_undefined__spec__(
193+
self, monkeypatch: pytest.MonkeyPatch
194+
) -> None:
191195
i18n = importlib.import_module("humanize.i18n")
192-
del i18n.__file__
196+
monkeypatch.delattr(i18n, "__spec__")
193197

194198
with pytest.raises(Exception) as excinfo:
195199
i18n.activate("ru_RU")

0 commit comments

Comments
 (0)