Moonlock lockscreen scaffolding: PAM auth (ctypes), fprintd D-Bus listener, i18n (DE/EN), user detection, power actions. 33 tests passing.
98 lines
2.9 KiB
Python
98 lines
2.9 KiB
Python
# ABOUTME: Locale detection and string lookup for the lockscreen UI.
|
|
# ABOUTME: Reads system locale (LANG or /etc/locale.conf) and provides DE or EN strings.
|
|
|
|
import os
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
DEFAULT_LOCALE_CONF = Path("/etc/locale.conf")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Strings:
|
|
"""All user-visible strings for the lockscreen UI."""
|
|
|
|
# UI labels
|
|
password_placeholder: str
|
|
unlock_button: str
|
|
reboot_tooltip: str
|
|
shutdown_tooltip: str
|
|
|
|
# Fingerprint
|
|
fingerprint_prompt: str
|
|
fingerprint_success: str
|
|
fingerprint_failed: str
|
|
|
|
# Error messages
|
|
auth_failed: str
|
|
wrong_password: str
|
|
reboot_failed: str
|
|
shutdown_failed: str
|
|
|
|
# Templates (use .format())
|
|
faillock_attempts_remaining: str
|
|
faillock_locked: str
|
|
|
|
|
|
_STRINGS_DE = Strings(
|
|
password_placeholder="Passwort",
|
|
unlock_button="Entsperren",
|
|
reboot_tooltip="Neustart",
|
|
shutdown_tooltip="Herunterfahren",
|
|
fingerprint_prompt="Fingerabdruck auflegen zum Entsperren",
|
|
fingerprint_success="Fingerabdruck erkannt",
|
|
fingerprint_failed="Fingerabdruck nicht erkannt",
|
|
auth_failed="Authentifizierung fehlgeschlagen",
|
|
wrong_password="Falsches Passwort",
|
|
reboot_failed="Neustart fehlgeschlagen",
|
|
shutdown_failed="Herunterfahren fehlgeschlagen",
|
|
faillock_attempts_remaining="Noch {n} Versuch(e) vor Kontosperrung!",
|
|
faillock_locked="Konto ist möglicherweise gesperrt",
|
|
)
|
|
|
|
_STRINGS_EN = Strings(
|
|
password_placeholder="Password",
|
|
unlock_button="Unlock",
|
|
reboot_tooltip="Reboot",
|
|
shutdown_tooltip="Shut down",
|
|
fingerprint_prompt="Place finger on reader to unlock",
|
|
fingerprint_success="Fingerprint recognized",
|
|
fingerprint_failed="Fingerprint not recognized",
|
|
auth_failed="Authentication failed",
|
|
wrong_password="Wrong password",
|
|
reboot_failed="Reboot failed",
|
|
shutdown_failed="Shutdown failed",
|
|
faillock_attempts_remaining="{n} attempt(s) remaining before lockout!",
|
|
faillock_locked="Account may be locked",
|
|
)
|
|
|
|
_LOCALE_MAP: dict[str, Strings] = {
|
|
"de": _STRINGS_DE,
|
|
"en": _STRINGS_EN,
|
|
}
|
|
|
|
|
|
def detect_locale(locale_conf_path: Path = DEFAULT_LOCALE_CONF) -> str:
|
|
"""Determine the system language from LANG env var or /etc/locale.conf."""
|
|
lang = os.environ.get("LANG")
|
|
|
|
if not lang and locale_conf_path.exists():
|
|
for line in locale_conf_path.read_text().splitlines():
|
|
if line.startswith("LANG="):
|
|
lang = line.split("=", 1)[1].strip()
|
|
break
|
|
|
|
if not lang or lang in ("C", "POSIX"):
|
|
return "en"
|
|
|
|
# Extract language prefix: "de_DE.UTF-8" → "de"
|
|
lang = lang.split("_")[0].split(".")[0]
|
|
return lang
|
|
|
|
|
|
def load_strings(locale: str | None = None) -> Strings:
|
|
"""Return the string table for the given locale, defaulting to English."""
|
|
if locale is None:
|
|
locale = detect_locale()
|
|
return _LOCALE_MAP.get(locale, _STRINGS_EN)
|