FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix i18n _get_default_locale_path · python-humanize/humanize@ff870da · GitHub

Commit ff870da

Browse files
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 ff870da

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

‎src/humanize/i18n.py‎

Lines changed: 12 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,13 @@
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+
if __package__ == "":
4139
return None
4240

41+
with importlib.resources.as_file(importlib.resources.files(__package__)) as pkg:
42+
return pkg / "locale"
43+
4344

4445
def get_translation() -> gettext_module.NullTranslations:
4546
try:
@@ -48,14 +49,16 @@ def get_translation() -> gettext_module.NullTranslations:
4849
return _TRANSLATIONS[None]
4950

5051

51-
def activate(locale: str, path: str | None = None) -> gettext_module.NullTranslations:
52+
def activate(
53+
locale: str, path: str | os.PathLike[str] | None = None
54+
) -> gettext_module.NullTranslations:
5255
"""Activate internationalisation.
5356
5457
Set `locale` as current locale. Search for locale in directory `path`.
5558
5659
Args:
5760
locale (str): Language name, e.g. `en_GB`.
58-
path (str): Path to search for locales.
61+
path (str | pathlib.Path): Path to search for locales.
5962
6063
Returns:
6164
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__package__() -> 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_null__package__(monkeypatch: pytest.MonkeyPatch) -> None:
165165
i18n = importlib.import_module("humanize.i18n")
166-
i18n.__file__ = None
166+
monkeypatch.setattr(i18n, "__package__", "")
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, "__package__")
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__package__(
183+
self, monkeypatch: pytest.MonkeyPatch
184+
) -> None:
183185
i18n = importlib.import_module("humanize.i18n")
184-
i18n.__file__ = None
186+
monkeypatch.setattr(i18n, "__package__", "")
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__file__(
193+
self, monkeypatch: pytest.MonkeyPatch
194+
) -> None:
191195
i18n = importlib.import_module("humanize.i18n")
192-
del i18n.__file__
196+
monkeypatch.delattr(i18n, "__package__")
193197

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

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL