| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,9 @@ | |||
| 3 | 3 | from __future__ import annotations | |
| 4 | 4 | ||
| 5 | 5 | import gettext as gettext_module | |
| 6 | - import os.path | ||
| 6 | + import importlib.resources | ||
| 7 | + import os | ||
| 8 | + import pathlib | ||
| 7 | 9 | from threading import local | |
| 8 | 10 | ||
| 9 | 11 | __all__ = ["activate", "deactivate", "decimal_separator", "thousands_separator"] | |
@@ -32,14 +34,13 @@ | |||
| 32 | 34 | } | |
| 33 | 35 | ||
| 34 | 36 | ||
| 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__ == "": | ||
| 41 | 39 | return None | |
| 42 | 40 | ||
| 41 | + with importlib.resources.as_file(importlib.resources.files(__package__)) as pkg: | ||
| 42 | + return pkg / "locale" | ||
| 43 | + | ||
| 43 | 44 | ||
| 44 | 45 | def get_translation() -> gettext_module.NullTranslations: | |
| 45 | 46 | try: | |
@@ -48,14 +49,16 @@ def get_translation() -> gettext_module.NullTranslations: | |||
| 48 | 49 | return _TRANSLATIONS[None] | |
| 49 | 50 | ||
| 50 | 51 | ||
| 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: | ||
| 52 | 55 | """Activate internationalisation. | |
| 53 | 56 | ||
| 54 | 57 | Set `locale` as current locale. Search for locale in directory `path`. | |
| 55 | 58 | ||
| 56 | 59 | Args: | |
| 57 | 60 | 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. | ||
| 59 | 62 | ||
| 60 | 63 | Returns: | |
| 61 | 64 | dict: Translations. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -156,20 +156,20 @@ def test_ordinal_genders( | |||
| 156 | 156 | humanize.i18n.deactivate() | |
| 157 | 157 | ||
| 158 | 158 | ||
| 159 | - def test_default_locale_path_defined__file__() -> None: | ||
| 159 | + def test_default_locale_path_defined__package__() -> None: | ||
| 160 | 160 | i18n = importlib.import_module("humanize.i18n") | |
| 161 | 161 | assert i18n._get_default_locale_path() is not None | |
| 162 | 162 | ||
| 163 | 163 | ||
| 164 | - def test_default_locale_path_null__file__() -> None: | ||
| 164 | + def test_default_locale_path_null__package__(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| 165 | 165 | i18n = importlib.import_module("humanize.i18n") | |
| 166 | - i18n.__file__ = None | ||
| 166 | + monkeypatch.setattr(i18n, "__package__", "") | ||
| 167 | 167 | assert i18n._get_default_locale_path() is None | |
| 168 | 168 | ||
| 169 | 169 | ||
| 170 | - def test_default_locale_path_undefined__file__() -> None: | ||
| 170 | + def test_default_locale_path_undefined__file__(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| 171 | 171 | i18n = importlib.import_module("humanize.i18n") | |
| 172 | - del i18n.__file__ | ||
| 172 | + monkeypatch.delattr(i18n, "__package__") | ||
| 173 | 173 | assert i18n._get_default_locale_path() is None | |
| 174 | 174 | ||
| 175 | 175 | ||
@@ -179,17 +179,21 @@ class TestActivate: | |||
| 179 | 179 | " 'locale' folder. You need to pass the path explicitly." | |
| 180 | 180 | ) | |
| 181 | 181 | ||
| 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: | ||
| 183 | 185 | i18n = importlib.import_module("humanize.i18n") | |
| 184 | - i18n.__file__ = None | ||
| 186 | + monkeypatch.setattr(i18n, "__package__", "") | ||
| 185 | 187 | ||
| 186 | 188 | with pytest.raises(Exception) as excinfo: | |
| 187 | 189 | i18n.activate("ru_RU") | |
| 188 | 190 | assert str(excinfo.value) == self.expected_msg | |
| 189 | 191 | ||
| 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: | ||
| 191 | 195 | i18n = importlib.import_module("humanize.i18n") | |
| 192 | - del i18n.__file__ | ||
| 196 | + monkeypatch.delattr(i18n, "__package__") | ||
| 193 | 197 | ||
| 194 | 198 | with pytest.raises(Exception) as excinfo: | |
| 195 | 199 | i18n.activate("ru_RU") | |
| Back | FazBrowse Home | New Git URL |
0 commit comments