diff --git a/plumbum/cli/i18n.py b/plumbum/cli/i18n.py index bd1fece0c..ff50ec317 100644 --- a/plumbum/cli/i18n.py +++ b/plumbum/cli/i18n.py @@ -1,21 +1,38 @@ +import os import gettext -import pkg_resources + +# If not installed with setuptools, this might not be available +try: + import pkg_resources +except ImportError: + pkg_resources = None + try: from typing import Tuple, List, Callable except ImportError: pass +local_dir = os.path.basename(__file__) + def get_translation_for(package_name): # type: (str) -> gettext.NullTranslations - 'find and return gettext translation for package' + '''Find and return gettext translation for package + (Try to find folder manually if setuptools does not exist) + ''' + if '.' in package_name: package_name = '.'.join(package_name.split('.')[:-1]) localedir = None - for localedir in pkg_resources.resource_filename(package_name, 'i18n'), None: + + if pkg_resources is None: + mydir = os.path.join(local_dir, 'i18n') + else: + mydir = pkg_resources.resource_filename(package_name, 'i18n') + + for localedir in mydir, None: localefile = gettext.find(package_name, localedir) if localefile: break - else: - pass + return gettext.translation(package_name, localedir=localedir, fallback=True)