diff --git a/plumbum/cli/i18n.py b/plumbum/cli/i18n.py index 1921fe5b..57294f5f 100644 --- a/plumbum/cli/i18n.py +++ b/plumbum/cli/i18n.py @@ -1,6 +1,9 @@ from __future__ import annotations import locale +import os + +DIR = os.path.abspath(os.path.dirname(__file__)) # High performance method for English (no translation needed) loc = locale.getlocale()[0] @@ -23,7 +26,6 @@ def get_translation_for( else: import gettext - import os # If not installed with setuptools, this might not be available try: @@ -31,25 +33,26 @@ def get_translation_for( except ImportError: pkg_resources = None # type: ignore[assignment] - local_dir = os.path.basename(__file__) - def get_translation_for(package_name: str) -> gettext.NullTranslations: # type: ignore[misc] """Find and return gettext translation for package (Try to find folder manually if setuptools does not exist) """ + assert loc is not None if "." in package_name: package_name = ".".join(package_name.split(".")[:-1]) localedir = None if pkg_resources is None: - mydir = os.path.join(local_dir, "i18n") + mydir = os.path.join(DIR, "i18n") else: mydir = pkg_resources.resource_filename(package_name, "i18n") for localedir in mydir, None: - localefile = gettext.find(package_name, localedir) + localefile = gettext.find(package_name, localedir, languages=[loc]) if localefile: break - return gettext.translation(package_name, localedir=localedir, fallback=True) + return gettext.translation( + package_name, localedir=localedir, languages=[loc], fallback=True + ) diff --git a/tests/test_translate.py b/tests/test_translate.py index cd71643f..13fd95ad 100644 --- a/tests/test_translate.py +++ b/tests/test_translate.py @@ -1,28 +1,57 @@ # Setting French as system language -import os -os.environ['LC_ALL'] = 'fr_FR.utf-8' +from __future__ import annotations + +import importlib +import locale import pytest -import sys -from plumbum import cli +import plumbum.cli +import plumbum.cli.i18n + + +def reload_cli(): + importlib.reload(plumbum.cli.i18n) + importlib.reload(plumbum.cli.switches) + importlib.reload(plumbum.cli.application) + importlib.reload(plumbum.cli) + + +@pytest.fixture() +def french(): + try: + locale.setlocale(locale.LC_ALL, "fr_FR") + reload_cli() + yield + finally: + locale.setlocale(locale.LC_ALL, "") + reload_cli() + + +@pytest.mark.usefixtures("french") +def test_nolang_switches(): + class Simple(plumbum.cli.Application): + foo = plumbum.cli.SwitchAttr("--foo") + + def main(self): + pass + + _, rc = Simple.run(["foo", "-h"], exit=False) + assert rc == 0 + _, rc = Simple.run(["foo", "--version"], exit=False) + assert rc == 0 -class Simple(cli.Application): - foo = cli.SwitchAttr("--foo") - def main(self): - pass +@pytest.mark.usefixtures("french") +def test_help_lang(capsys): + class Simple(plumbum.cli.Application): + foo = plumbum.cli.SwitchAttr("--foo") -class TestFRCLI: - def test_nolang_switches(self): - _, rc = Simple.run(["foo", "-h"], exit = False) - assert rc == 0 - _, rc = Simple.run(["foo", "--version"], exit = False) - assert rc == 0 + def main(self): + pass - def test_help_lang(self, capsys): - _, rc = Simple.run(["foo", "-h"], exit = False) - assert rc == 0 - stdout, stderr = capsys.readouterr() - assert "Utilisation" in stdout - assert "Imprime ce message d'aide et sort" in stdout + _, rc = Simple.run(["foo", "-h"], exit=False) + assert rc == 0 + stdout, stderr = capsys.readouterr() + assert "Utilisation" in stdout + assert "Imprime ce message d'aide et sort" in stdout