"""Modal dialog for picking one of the application's available languages.""" from __future__ import annotations from textual.app import ComposeResult from textual.containers import Container, Horizontal, Vertical from textual.screen import ModalScreen from textual.widgets import Button, Label from i18n import I18n class LanguageSelectScreen(ModalScreen[str | None]): """Shows one button per available language; returns the chosen code. Returns ``None`` if the dialog is cancelled (Escape or the Cancel button), leaving the current language unchanged. """ # Styling for this screen lives in the app's shared external stylesheet # (app/styles.tcss), loaded via DownloadManagerApp.CSS_PATH. BINDINGS = [("escape", "cancel", "Cancel")] def compose(self) -> ComposeResult: i18n = I18n() with Container(): yield Label( i18n.t("dialog.select_language", default="Select language"), id="language-title", ) with Vertical(id="language-options"): for code, display_name in i18n.get_available_languages().items(): variant = "primary" if code == i18n.get_language() else "default" yield Button(display_name, id=f"lang-{code}", variant=variant) with Horizontal(): yield Button(i18n.t("button.cancel", default="Cancel"), id="language-cancel") def on_button_pressed(self, event: Button.Pressed) -> None: button_id = event.button.id or "" if button_id == "language-cancel": self.dismiss(None) return if button_id.startswith("lang-"): self.dismiss(button_id.removeprefix("lang-")) def action_cancel(self) -> None: self.dismiss(None)