1
0
Fork 0
forked from Fuji/Fuji

Compile language files; Enhance translation functions

This commit is contained in:
Maarceeli 2025-03-10 16:58:15 +01:00
parent 580d57f7b0
commit 06db2121c8
4 changed files with 49 additions and 13 deletions

View file

@ -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 _

Binary file not shown.

Binary file not shown.

View file

@ -61,6 +61,9 @@ def setlanguage(lang):
config.write(file)
def restart(page: ft.Page):
python = sys.executable
subprocess.Popen([python] + sys.argv)
page.window.destroy()
try:
python = sys.executable
subprocess.Popen([python] + sys.argv)
page.window.destroy()
except FileNotFoundError:
exit()