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..800dc5b1 --- /dev/null +++ b/tests/solaar/test_i18n_paths.py @@ -0,0 +1,30 @@ +import os + +from pathlib import Path + +import pytest + +from solaar import i18n + + +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) + + +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")