From b30f6a80c8201898d13859e28323d64aaaafbe76 Mon Sep 17 00:00:00 2001 From: Wei Hong Date: Tue, 4 Aug 2026 17:40:45 +0800 Subject: [PATCH] Update version number. --- constant.py | 6 ++--- core_lib/i18n.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 2 +- 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 core_lib/i18n.py diff --git a/constant.py b/constant.py index 5462651..c944a3d 100644 --- a/constant.py +++ b/constant.py @@ -1,6 +1,6 @@ -LAUNCHER_VERSION = "0.0.5-alpha" -LAUNCHER_NAME = "TestLauncher (Aka TapLauncher)" -LAUNCHER_DIR_NAME = "TestLauncher" +LAUNCHER_VERSION = "0.0.6-alpha" +LAUNCHER_NAME = "TapLauncher" +LAUNCHER_DIR_NAME = "TapLauncher" MAIN_REPO_URL = "https://repo.weispace.net/wei/Launcher/" CLIENT_ID = "30cbb657-edab-4d06-9fb7-cf5734a40088" diff --git a/core_lib/i18n.py b/core_lib/i18n.py new file mode 100644 index 0000000..51fce2f --- /dev/null +++ b/core_lib/i18n.py @@ -0,0 +1,58 @@ +"""Small, dependency-free translation catalog used by the launcher UI.""" + +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + +from PySide6.QtCore import QObject, Signal + + +SUPPORTED_LANGUAGES = ("en", "zh-TW") + + +class TranslationManager(QObject): + """Load JSON catalogs and notify widgets when the language changes.""" + + language_changed = Signal(str) + + def __init__(self, catalog_dir: Path, language: str = "en") -> None: + super().__init__() + self.catalog_dir = Path(catalog_dir) + self._language = "en" + self._catalog: dict[str, str] = {} + self.set_language(language, emit=False) + + @property + def language(self) -> str: + return self._language + + def set_language(self, language: str, *, emit: bool = True) -> None: + if language not in SUPPORTED_LANGUAGES: + language = "en" + + catalog: dict[str, str] = {} + path = self.catalog_dir / f"{language}.json" + if path.is_file(): + try: + raw = json.loads(path.read_text(encoding="utf-8")) + catalog = { + str(key): str(value) + for key, value in raw.items() + if isinstance(value, str) + } + except (OSError, json.JSONDecodeError): + # An invalid optional catalog must not prevent the launcher starting. + catalog = {} + + changed = language != self._language or catalog != self._catalog + self._language = language + self._catalog = catalog + if emit and changed: + self.language_changed.emit(language) + + def translate(self, key: str, /, **values: Any) -> str: + text = self._catalog.get(key, key) + return text.format(**values) if values else text + diff --git a/pyproject.toml b/pyproject.toml index ff1995e..4200849 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "testlauncher" -version = "0.0.5a0" +version = "0.0.6a0" description = "A lightweight Minecraft launcher." readme = "README.md" requires-python = ">=3.10"