Compile language files; Enhance translation functions
This commit is contained in:
parent
580d57f7b0
commit
06db2121c8
4 changed files with 49 additions and 13 deletions
53
src/i18n.py
53
src/i18n.py
|
@ -1,36 +1,69 @@
|
||||||
from utils import getconfigpath
|
from utils import getconfigpath
|
||||||
import configparser
|
import configparser
|
||||||
import gettext
|
import gettext
|
||||||
import locale
|
|
||||||
import json
|
import json
|
||||||
import os
|
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
|
# Determine the user's locale
|
||||||
try:
|
try:
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read(f"{getconfigpath()}/config.ini")
|
config.read(os.path.join(getconfigpath(), "config.ini"))
|
||||||
lang = config["Settings"]["language"]
|
lang = config.get("Settings", "language", fallback="pl")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
lang = "pl"
|
lang = "pl"
|
||||||
print("Defaulting to Polish due to missing config file")
|
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:
|
except Exception as e:
|
||||||
lang = "pl"
|
lang = "pl"
|
||||||
print(f"Defaulting to Polish due to error: {e}")
|
print(f"Defaulting to Polish due to error: {e}")
|
||||||
finally:
|
finally:
|
||||||
print(f"Language set to {lang}")
|
print(f"Language set to {lang}")
|
||||||
|
|
||||||
# Set up translation
|
# Determine the correct locales directory
|
||||||
localedir = os.path.join(os.path.dirname(__file__), "locales")
|
if getattr(sys, 'frozen', False):
|
||||||
translation = gettext.translation("base", localedir, languages=[lang], fallback=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.install()
|
||||||
_ = translation.gettext # Shortcut for gettext
|
_ = translation.gettext # Shortcut for gettext
|
||||||
|
|
||||||
# Function to switch language dynamically
|
# Function to switch language dynamically
|
||||||
def set_language(new_lang):
|
def set_language(new_lang):
|
||||||
global translation, _
|
global translation, _
|
||||||
translation = gettext.translation("base", localedir, languages=[new_lang], fallback=False)
|
translation = load_translation(new_lang)
|
||||||
translation.install()
|
translation.install()
|
||||||
_ = translation.gettext # Update global _
|
_ = translation.gettext # Update global _
|
||||||
|
|
BIN
src/locales/en/LC_MESSAGES/base.mo
Normal file
BIN
src/locales/en/LC_MESSAGES/base.mo
Normal file
Binary file not shown.
BIN
src/locales/pl/LC_MESSAGES/base.mo
Normal file
BIN
src/locales/pl/LC_MESSAGES/base.mo
Normal file
Binary file not shown.
|
@ -61,6 +61,9 @@ def setlanguage(lang):
|
||||||
config.write(file)
|
config.write(file)
|
||||||
|
|
||||||
def restart(page: ft.Page):
|
def restart(page: ft.Page):
|
||||||
python = sys.executable
|
try:
|
||||||
subprocess.Popen([python] + sys.argv)
|
python = sys.executable
|
||||||
page.window.destroy()
|
subprocess.Popen([python] + sys.argv)
|
||||||
|
page.window.destroy()
|
||||||
|
except FileNotFoundError:
|
||||||
|
exit()
|
Loading…
Add table
Reference in a new issue