From 10169af77492d5e341c3ae01094f904f40b40b5d Mon Sep 17 00:00:00 2001 From: Alyner3 Date: Thu, 29 May 2025 18:19:09 +0200 Subject: [PATCH] Feat: Add translation tools --- .gitignore | 1 + tools/compile_translations.py | 35 ++++++++++++++++++++++++++++++ tools/translation_venv.py | 41 +++++++++++++++++++++++++++++++++++ tools/update_translations.py | 32 +++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 tools/compile_translations.py create mode 100644 tools/translation_venv.py create mode 100644 tools/update_translations.py diff --git a/.gitignore b/.gitignore index 4a8dab0..685b70e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ *.qm .etag_cache +.venv-translations TODO.md \ No newline at end of file diff --git a/tools/compile_translations.py b/tools/compile_translations.py new file mode 100644 index 0000000..e0659c7 --- /dev/null +++ b/tools/compile_translations.py @@ -0,0 +1,35 @@ +import subprocess +import glob +import os +from pathlib import Path + +TRANSLATIONS_DIR = Path(__file__).resolve().parent.parent +print(TRANSLATIONS_DIR) + +ts_files = glob.glob(str(TRANSLATIONS_DIR / "*.ts")) +success = [] +failed = [] + +print("Compiling .ts files to .qm:") + +for ts_file in ts_files: + qm_file = ts_file.replace(".ts", ".qm") + cmd = ["qt6-tools", "lrelease", ts_file] + result = subprocess.run(cmd, capture_output=True, text=True) + + if result.returncode == 0: + print(f"[Compiled]:\t {os.path.basename(ts_file)} → {os.path.basename(qm_file)}") + success.append(qm_file) + else: + print(f"[Failed]:\t {os.path.basename(ts_file)}") + print(result.stderr) + failed.append(ts_file) + +print("\n--- Summary ---") +print(f"{len(success)} succeeded") +print(f"{len(failed)} failed") + +if failed: + print("\nFailed files:") + for f in failed: + print(" -", f) diff --git a/tools/translation_venv.py b/tools/translation_venv.py new file mode 100644 index 0000000..b76ec13 --- /dev/null +++ b/tools/translation_venv.py @@ -0,0 +1,41 @@ +import os +import subprocess +import sys +import shutil +from pathlib import Path + +TRANSLATIONS_DIR = Path(__file__).resolve().parent.parent +VENV_DIR = TRANSLATIONS_DIR / ".venv-translations" +PYTHON_BIN = VENV_DIR / "Scripts" / "python.exe" if os.name == "nt" else VENV_DIR / "bin" / "python" + +def create_venv(): + print(f"[Setup] Creating virtual environment at {VENV_DIR}...") + subprocess.run([sys.executable, "-m", "venv", str(VENV_DIR)], check=True) + +def install_pyqt6_tools(): + print(f"[Setup] Installing pyqt6-tools in {VENV_DIR}...") + subprocess.run([str(PYTHON_BIN), "-m", "pip", "install", "--upgrade", "pip"], check=True) + subprocess.run([str(PYTHON_BIN), "-m", "pip", "install", "--pre", "pyqt6-tools"], check=True) + +def run_pyqt6_tools_tool(args): + tool = ["qt6-tools"] + args + print(f"[Running] {' '.join(tool)}") + subprocess.run([str(PYTHON_BIN)] + ["-m"] + tool, check=True) + +def main(): + if not VENV_DIR.exists(): + create_venv() + install_pyqt6_tools() + + if not shutil.which("qt6-tools"): + print("[Info] Executing within virtual environment.") + run_pyqt6_tools_tool(sys.argv[1:]) + else: + print("Unexpected: qt6-tools found globally, using system environment.") + subprocess.run(["qt6-tools"] + sys.argv[1:], check=True) + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python translation_venv.py [pylupdate6|lrelease|linguist] ") + sys.exit(1) + main() \ No newline at end of file diff --git a/tools/update_translations.py b/tools/update_translations.py new file mode 100644 index 0000000..2ea824f --- /dev/null +++ b/tools/update_translations.py @@ -0,0 +1,32 @@ +import subprocess +import glob +import os +from pathlib import Path + +BASE_DIR = Path(__file__).resolve().parent.parent.parent +TRANSLATIONS_DIR = BASE_DIR / "translations" + +util_qt_files = glob.glob(str(BASE_DIR / "util_qt" / "**" / "*.py"), recursive=True) +source_files = [str(BASE_DIR / "gui.py")] + util_qt_files + +ts_files = [ + str(TRANSLATIONS_DIR / "wulkplot_de.ts"), + str(TRANSLATIONS_DIR / "wulkplot_fr.ts"), + str(TRANSLATIONS_DIR / "wulkplot_no.ts"), + str(TRANSLATIONS_DIR / "wulkplot_ru.ts"), +] + +cmd = ["pylupdate6"] + source_files +for ts in ts_files: + cmd += ["-ts", ts] + +print("Running command:") +print(" ".join(cmd)) + +result = subprocess.run(cmd, capture_output=True, text=True) + +if result.returncode != 0: + print("Error:") + print(result.stderr) +else: + print("pylupdate6 completed successfully.")