From 06db2121c8b6aaf750b2bed5f15abc74cf548fdf Mon Sep 17 00:00:00 2001 From: Maarceeli Date: Mon, 10 Mar 2025 16:58:15 +0100 Subject: [PATCH] Compile language files; Enhance translation functions --- src/i18n.py | 53 +++++++++++++++++++++++------ src/locales/en/LC_MESSAGES/base.mo | Bin 0 -> 337 bytes src/locales/pl/LC_MESSAGES/base.mo | Bin 0 -> 1914 bytes src/utils.py | 9 +++-- 4 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 src/locales/en/LC_MESSAGES/base.mo create mode 100644 src/locales/pl/LC_MESSAGES/base.mo diff --git a/src/i18n.py b/src/i18n.py index 0d7b796..f1f04ec 100644 --- a/src/i18n.py +++ b/src/i18n.py @@ -1,36 +1,69 @@ from utils import getconfigpath import configparser import gettext -import locale import json import os +import sys + +# Detect if running as a compiled Nuitka binary +if getattr(sys, 'frozen', False): + BASE_DIR = sys._MEIPASS # Nuitka extracts files here +else: + BASE_DIR = os.path.abspath(os.path.dirname(__file__)) # Running locally # Determine the user's locale try: config = configparser.ConfigParser() - config.read(f"{getconfigpath()}/config.ini") - lang = config["Settings"]["language"] + config.read(os.path.join(getconfigpath(), "config.ini")) + lang = config.get("Settings", "language", fallback="pl") except FileNotFoundError: lang = "pl" print("Defaulting to Polish due to missing config file") -except AttributeError: - lang = "pl" - print("Defaulting to Polish due to missing language setting in config file") except Exception as e: lang = "pl" print(f"Defaulting to Polish due to error: {e}") finally: print(f"Language set to {lang}") -# Set up translation -localedir = os.path.join(os.path.dirname(__file__), "locales") -translation = gettext.translation("base", localedir, languages=[lang], fallback=False) +# Determine the correct locales directory +if getattr(sys, 'frozen', False): + # Running as a compiled binary + localedir = os.path.join(BASE_DIR, "src", "locales") +else: + # Running locally + localedir = os.path.join(BASE_DIR, "locales") + +# Ensure .mo files exist +def ensure_mo_files(): + for root, _, files in os.walk(localedir): + for file in files: + if file.endswith(".po"): + mo_file = file.replace(".po", ".mo") + mo_path = os.path.join(root, mo_file) + if not os.path.exists(mo_path): + po_path = os.path.join(root, file) + print(f"Compiling {po_path} to {mo_path}") + os.system(f"msgfmt {po_path} -o {mo_path}") + +if not getattr(sys, 'frozen', False): # Only check when running locally + ensure_mo_files() + +# Function to load translations safely +def load_translation(language): + try: + return gettext.translation("base", localedir, languages=[language], fallback=True) + except FileNotFoundError: + print(f"Warning: Translation file not found for language '{language}'") + return gettext.NullTranslations() + +# Load initial language +translation = load_translation(lang) translation.install() _ = translation.gettext # Shortcut for gettext # Function to switch language dynamically def set_language(new_lang): global translation, _ - translation = gettext.translation("base", localedir, languages=[new_lang], fallback=False) + translation = load_translation(new_lang) translation.install() _ = translation.gettext # Update global _ diff --git a/src/locales/en/LC_MESSAGES/base.mo b/src/locales/en/LC_MESSAGES/base.mo new file mode 100644 index 0000000000000000000000000000000000000000..6c5906d1cd061dff54de8b533942893de34efc9e GIT binary patch literal 337 zcmYL@Jx{|h5Qd9j%E-dP;DHUUz!pqFHI3Uw*h!U-O0b#M1fyU_j*H-j@b~yFTo(FD zk8Zg4bkFbc(a#8TfSe*{$RTop42h8wT;AXuI{#UD_pUbq(k-mD?~SvRtk~?4EjU^8 zqD=EFDs<<30NFQY3lF=dhsseBt#T;zrx|V_Q9)Dk#909{hlG)3PGx%joM$`|st-_k zW&2hI=P8-jLXeC}P9|KkR7_ct6ud0&v1*&0YBW?@eNZA;wx|b_i4fD)jGb@x9W;=s z7)B6vqt|XqS&d`6!exw-r(=I@?x4qAa0wlWacP{nBpI&>$haGtR7cXYJJE z&2&aWLJ^1qHxLqSuW;s24j_tzlW2eFT-5AjJ-^19w-~q4%9t7_PkAMfjW8gmU6iEBd7V}q%`4sp7 z)~CVyz-F<&43gbj1>XVpV=fD7@EOb#knBDIp9gOg^KU@f|2;^4ekj&|0w2Qs7Z5-8 zI|kDG2lxp17f5zwbRIYWlI{hN;%S2P{!Nh16(HI5i+KtjMvmSCDX!08OnLbnB)cz* z`PU%%+bY(7Ech!(c6UIs`xC^E{f&Wi{!`5N;qZf)m%xX?BO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