From 05f538accae35821788e94e1062ce5982e05c00c Mon Sep 17 00:00:00 2001 From: fredaime Date: Sat, 3 Oct 2026 04:07:49 +0200 Subject: [PATCH 1/2] i18n: translated setting labels for development installs plasmaard sent English setting labels to the System Settings module on a French desktop. Two reasons, both specific to running from a source checkout: - the .mo catalogues are build artefacts (git-ignored) produced by tools/po-compile.sh, which nothing ran: add `make compile_translations` and run it from install_user_service; - the catalogue lookup only searched relative to sys.path[0], i.e. the launching script's directory; for a console script in a venv that is .venv/bin, so share/locale of the checkout was never found. Also search relative to the package (lib/solaar/ -> share/). With catalogues present and a non-English locale, tests comparing English strings failed, so tests/conftest.py pins LANGUAGE=C before anything imports solaar. Verified: ListSettings now returns French labels ("Direction de la molette", ...), and the suite passes under fr_FR. Co-Authored-By: Claude Opus 5.5 Signed-off-by: fredaime --- Makefile | 8 ++++++-- lib/solaar/i18n.py | 18 ++++++++++++++---- tests/conftest.py | 6 ++++++ tests/solaar/test_i18n_paths.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 tests/solaar/test_i18n_paths.py diff --git a/Makefile b/Makefile index 734c96f7..ae3ece5d 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ PIP_ARGS ?= . .PHONY: install_ubuntu install_macos .PHONY: install_apt install_brew install_pip -.PHONY: install_udev install_udev_uinput reload_udev uninstall_udev install_user_service uninstall_user_service install_desktop_entry +.PHONY: install_udev install_udev_uinput reload_udev uninstall_udev install_user_service uninstall_user_service install_desktop_entry compile_translations .PHONY: format lint test install_ubuntu: install_apt install_udev_uinput install_pip @@ -69,7 +69,11 @@ install_desktop_entry: install -D -m 644 share/applications/$(DESKTOP_ENTRY) $(USER_APPLICATIONS_DIR)/$(DESKTOP_ENTRY) -update-desktop-database $(USER_APPLICATIONS_DIR) -install_user_service: install_desktop_entry +compile_translations: + @echo "Compiling translations into share/locale" + sh tools/po-compile.sh + +install_user_service: install_desktop_entry compile_translations @test -n "$(PLASMAARD)" || { echo "plasmaard not found; install plasmaar or pass PLASMAARD=/path/to/plasmaard"; exit 1; } @echo "Installing plasmaard user service running $(PLASMAARD)" mkdir -p $(USER_UNIT_DIR) diff --git a/lib/solaar/i18n.py b/lib/solaar/i18n.py index d3e53242..aa7d0584 100644 --- a/lib/solaar/i18n.py +++ b/lib/solaar/i18n.py @@ -14,6 +14,8 @@ ## with this program; if not, write to the Free Software Foundation, Inc., ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +from __future__ import annotations + import gettext import locale import logging @@ -29,11 +31,19 @@ logger = logging.getLogger(__name__) -def _find_locale_path(locale_domain: str) -> str: - prefix_share = os.path.normpath(os.path.join(os.path.realpath(sys.path[0]), "..")) - src_share = os.path.normpath(os.path.join(os.path.realpath(sys.path[0]), "..", "share")) +def _locale_locations() -> tuple[str, ...]: + """Directories whose locale/ subdirectory may hold the compiled catalogues, most specific first.""" + script = os.path.realpath(sys.path[0]) + prefix_share = os.path.normpath(os.path.join(script, "..")) + src_share = os.path.normpath(os.path.join(script, "..", "share")) + # relative to the package, for console scripts of a source checkout (e.g. an editable install in a venv, + # where sys.path[0] is the venv's bin directory): lib/solaar/ -> share/ + package_share = os.path.normpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "..", "share")) + return prefix_share, src_share, package_share - for location in prefix_share, src_share: + +def _find_locale_path(locale_domain: str) -> str: + for location in _locale_locations(): mo_files = glob(os.path.join(location, "locale", "*", "LC_MESSAGES", f"{locale_domain}.mo")) if mo_files: return os.path.join(location, "locale") diff --git a/tests/conftest.py b/tests/conftest.py index 622aadf2..04f65ca0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,15 @@ import importlib +import os from unittest import mock import pytest +# Tests compare English strings, so never translate them: with compiled catalogues in share/locale +# (make compile_translations) and a non-English locale, solaar.i18n would otherwise pick them up. +# Set before any test module imports solaar (pytest loads this conftest first). +os.environ["LANGUAGE"] = "C" + @pytest.fixture(autouse=True) def isolate_solaar_configuration(tmp_path, monkeypatch): diff --git a/tests/solaar/test_i18n_paths.py b/tests/solaar/test_i18n_paths.py new file mode 100644 index 00000000..536f5648 --- /dev/null +++ b/tests/solaar/test_i18n_paths.py @@ -0,0 +1,29 @@ +import os + +from pathlib import Path + +import pytest + +from solaar import i18n + + +def test_package_relative_location_is_the_source_share_dir(): + share = Path(__file__).resolve().parents[2] / "share" + + assert i18n._locale_locations()[-1] == str(share) + + +def test_finds_catalogues_in_a_location(tmp_path, monkeypatch): + mo = tmp_path / "share" / "locale" / "fr" / "LC_MESSAGES" / "solaar.mo" + mo.parent.mkdir(parents=True) + mo.write_bytes(b"") + monkeypatch.setattr(i18n, "_locale_locations", lambda: (str(tmp_path / "nothing"), str(tmp_path / "share"))) + + assert i18n._find_locale_path("solaar") == os.path.join(str(tmp_path / "share"), "locale") + + +def test_no_catalogues_anywhere(tmp_path, monkeypatch): + monkeypatch.setattr(i18n, "_locale_locations", lambda: (str(tmp_path),)) + + with pytest.raises(FileNotFoundError): + i18n._find_locale_path("solaar") From ad85979eb304fd9b6386ece9c788d6825e701436 Mon Sep 17 00:00:00 2001 From: fredaime Date: Sat, 3 Oct 2026 04:09:37 +0200 Subject: [PATCH 2/2] test: package-relative locale path relative to the imported package CI installs plasmaar non-editable, so solaar is imported from site-packages, not the checkout the test file lives in. Co-Authored-By: Claude Opus 5.5 Signed-off-by: fredaime --- tests/solaar/test_i18n_paths.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/solaar/test_i18n_paths.py b/tests/solaar/test_i18n_paths.py index 536f5648..800dc5b1 100644 --- a/tests/solaar/test_i18n_paths.py +++ b/tests/solaar/test_i18n_paths.py @@ -7,8 +7,9 @@ from solaar import i18n -def test_package_relative_location_is_the_source_share_dir(): - share = Path(__file__).resolve().parents[2] / "share" +def test_package_relative_location(): + """lib/solaar/ -> share/ in a source checkout (the package is wherever solaar was imported from).""" + share = Path(i18n.__file__).resolve().parents[2] / "share" assert i18n._locale_locations()[-1] == str(share)