Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve default locale path discovering. #146

Merged
merged 5 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -26,7 +29,7 @@ def activate(locale, path=None):
@param locale: language name, eg 'en_GB'
@param path: path to search for locales"""
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