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

fix: setuptools-less installs couldn't translate, add test #373

Merged
merged 3 commits into from
Oct 5, 2024
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
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ jobs:
python-version: ${{ matrix.python-version }}
allow-prereleases: true

- name: Add locale for locale test
if: runner.os == 'Linux' && matrix.python-version != '3.10'
run: sudo locale-gen fr_FR.UTF-8

- name: Setup uv (cached)
uses: hynek/setup-cached-uv@v2

Expand Down
15 changes: 9 additions & 6 deletions plumbum/cli/i18n.py
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -23,33 +26,33 @@ def get_translation_for(

else:
import gettext
import os

# If not installed with setuptools, this might not be available
try:
import pkg_resources
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
)
61 changes: 61 additions & 0 deletions tests/test_translate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Setting French as system language
from __future__ import annotations

import importlib
import locale

import pytest

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.utf-8")
reload_cli()
yield
except locale.Error:
pytest.skip(
"No fr_FR locale found, run 'sudo locale-gen fr_FR.UTF-8' to run this test"
)
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


@pytest.mark.usefixtures("french")
def test_help_lang(capsys):
class Simple(plumbum.cli.Application):
foo = plumbum.cli.SwitchAttr("--foo")

def main(self):
pass

_, 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