Skip to content

Commit 19b43d3

Browse files
authored
Fix i18n _get_default_locale_path (#204)
2 parents 4e1c96b + b5f9af5 commit 19b43d3

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

src/humanize/i18n.py

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

55
import gettext as gettext_module
6-
import os.path
76
from threading import local
7+
from typing import TYPE_CHECKING
8+
9+
if TYPE_CHECKING:
10+
import os
11+
import pathlib
812

913
__all__ = ["activate", "deactivate", "decimal_separator", "thousands_separator"]
1014

@@ -32,14 +36,16 @@
3236
}
3337

3438

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:
39+
def _get_default_locale_path() -> pathlib.Path | None:
40+
package = __spec__ and __spec__.parent
41+
if not package:
4142
return None
4243

44+
import importlib.resources
45+
46+
with importlib.resources.as_file(importlib.resources.files(package)) as pkg:
47+
return pkg / "locale"
48+
4349

4450
def get_translation() -> gettext_module.NullTranslations:
4551
try:
@@ -48,14 +54,16 @@ def get_translation() -> gettext_module.NullTranslations:
4854
return _TRANSLATIONS[None]
4955

5056

51-
def activate(locale: str, path: str | None = None) -> gettext_module.NullTranslations:
57+
def activate(
58+
locale: str, path: str | os.PathLike[str] | None = None
59+
) -> gettext_module.NullTranslations:
5260
"""Activate internationalisation.
5361
5462
Set `locale` as current locale. Search for locale in directory `path`.
5563
5664
Args:
5765
locale (str): Language name, e.g. `en_GB`.
58-
path (str): Path to search for locales.
66+
path (str | pathlib.Path): Path to search for locales.
5967
6068
Returns:
6169
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)