Skip to content
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
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 14 additions & 4 deletions lib/solaar/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
30 changes: 30 additions & 0 deletions tests/solaar/test_i18n_paths.py
Original file line number Diff line number Diff line change
@@ -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")
Loading