|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from babel import Locale |
| 4 | +from textual.app import ComposeResult |
| 5 | +from textual.containers import Horizontal, Vertical |
| 6 | +from textual.screen import Screen |
| 7 | +from textual.widgets import Button, OptionList, Static |
| 8 | + |
| 9 | +from ..components.layout import AppFooter, AppHeader |
| 10 | +from ..strings import _, set_language |
| 11 | +from .auth import AuthScreen |
| 12 | +from .quit_confirm import QuitConfirmScreen |
| 13 | + |
| 14 | + |
| 15 | +def get_available_languages() -> dict[int, str]: |
| 16 | + locales_dir = Path(__file__).parent.parent / "locale" |
| 17 | + langs = ["en"] |
| 18 | + if locales_dir.exists(): |
| 19 | + for item in sorted(locales_dir.iterdir()): |
| 20 | + if item.is_dir() and (item / "LC_MESSAGES" / "messages.mo").exists(): |
| 21 | + langs.append(item.name) |
| 22 | + |
| 23 | + unique_langs = [] |
| 24 | + for lang in langs: |
| 25 | + if lang not in unique_langs: |
| 26 | + unique_langs.append(lang) |
| 27 | + |
| 28 | + return dict(enumerate(unique_langs)) |
| 29 | + |
| 30 | + |
| 31 | +class LanguageScreen(Screen): |
| 32 | + def compose(self) -> ComposeResult: |
| 33 | + self.lang_map = get_available_languages() |
| 34 | + options = [] |
| 35 | + for _idx, lang_code in self.lang_map.items(): |
| 36 | + try: |
| 37 | + name = Locale(lang_code).get_display_name(lang_code) |
| 38 | + display = str(name or lang_code).title() |
| 39 | + except Exception: |
| 40 | + display = lang_code |
| 41 | + |
| 42 | + options.append(display) |
| 43 | + |
| 44 | + yield AppHeader() |
| 45 | + with Vertical(id="lang-container"): |
| 46 | + yield Static(_("Select your language"), id="lang-label") |
| 47 | + yield OptionList( |
| 48 | + *options, |
| 49 | + id="lang-select", |
| 50 | + ) |
| 51 | + with Horizontal(id="lang-actions"): |
| 52 | + yield Button(_("Continue"), id="lang-continue", variant="primary") |
| 53 | + yield Button(_("Quit"), id="lang-quit", variant="error") |
| 54 | + yield AppFooter() |
| 55 | + |
| 56 | + def on_option_list_option_highlighted( |
| 57 | + self, event: OptionList.OptionHighlighted |
| 58 | + ) -> None: |
| 59 | + lang_code = ( |
| 60 | + self.lang_map.get(event.option_index, "en") |
| 61 | + if getattr(self, "lang_map", None) and event.option_index is not None |
| 62 | + else "en" |
| 63 | + ) |
| 64 | + |
| 65 | + set_language(lang_code) |
| 66 | + |
| 67 | + # Update labels dynamically |
| 68 | + self.query_one("#app-header", Static).update(_("Welcome aboard to Python Perú")) |
| 69 | + self.query_one("#lang-label", Static).update(_("Select your language")) |
| 70 | + self.query_one("#lang-continue", Button).label = _("Continue") |
| 71 | + self.query_one("#lang-quit", Button).label = _("Quit") |
| 72 | + self.query_one("#app-footer", Static).update(_("Proudly built with ❤️ in Perú")) |
| 73 | + |
| 74 | + def on_button_pressed(self, event: Button.Pressed) -> None: |
| 75 | + if event.button.id == "lang-quit": |
| 76 | + |
| 77 | + def check_quit(quit_app: bool | None) -> None: |
| 78 | + if quit_app: |
| 79 | + self.app.exit() |
| 80 | + |
| 81 | + self.app.push_screen(QuitConfirmScreen(), check_quit) |
| 82 | + elif event.button.id == "lang-continue": |
| 83 | + # If the user clicks continue without highlighting an option, |
| 84 | + # make sure we set default |
| 85 | + opt_list = self.query_one("#lang-select", OptionList) |
| 86 | + selected_idx = opt_list.highlighted |
| 87 | + lang_code = ( |
| 88 | + self.lang_map.get(selected_idx, "en") |
| 89 | + if getattr(self, "lang_map", None) and selected_idx is not None |
| 90 | + else "en" |
| 91 | + ) |
| 92 | + set_language(lang_code) |
| 93 | + self.app.push_screen(AuthScreen()) |
0 commit comments