Skip to content

Commit

Permalink
Merge pull request #146 from mondeja/i18n-dlp
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Aug 10, 2020
2 parents 3137dac + 7de59b6 commit cca5a74
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/humanize/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
_TRANSLATIONS = {None: gettext_module.NullTranslations()}
_CURRENT = local()

try:
_DEFAULT_LOCALE_PATH = os.path.join(os.path.dirname(__file__), "locale")
except AttributeError:
# in case that __file__ does not exist
_DEFAULT_LOCALE_PATH = None

def _get_default_locale_path():
try:
if __file__ is None:
return None
return os.path.join(os.path.dirname(__file__), "locale")
except NameError:
return None


def get_translation():
Expand All @@ -34,7 +37,7 @@ def activate(locale, path=None):
dict: Translations.
"""
if path is None:
path = _DEFAULT_LOCALE_PATH
path = _get_default_locale_path()

if path is None:
raise Exception(
Expand Down
42 changes: 42 additions & 0 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime as dt
import importlib

import humanize
import pytest


def test_i18n():
Expand All @@ -17,3 +19,43 @@ def test_i18n():
humanize.i18n.deactivate()
assert humanize.naturaltime(three_seconds) == "3 seconds ago"
assert humanize.ordinal(5) == "5th"


def test_default_locale_path_defined__file__():
i18n = importlib.import_module("humanize.i18n")
assert i18n._get_default_locale_path() is not None


def test_default_locale_path_null__file__():
i18n = importlib.import_module("humanize.i18n")
i18n.__file__ = None
assert i18n._get_default_locale_path() is None


def test_default_locale_path_undefined__file__():
i18n = importlib.import_module("humanize.i18n")
del i18n.__file__
i18n._get_default_locale_path() is None


class TestActivate:
expected_msg = (
"Humanize cannot determinate the default location of the"
" 'locale' folder. You need to pass the path explicitly."
)

def test_default_locale_path_null__file__(self):
i18n = importlib.import_module("humanize.i18n")
i18n.__file__ = None

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

def test_default_locale_path_undefined__file__(self):
i18n = importlib.import_module("humanize.i18n")
del i18n.__file__

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

0 comments on commit cca5a74

Please sign in to comment.