Switched from json config to ini configuration file; fixed a issue with language being set to polish only
This commit is contained in:
parent
b17be83791
commit
6681ab4cf3
7 changed files with 71 additions and 27 deletions
|
@ -6,4 +6,14 @@ grade = [
|
|||
"#4CAF50",
|
||||
"#2196F3",
|
||||
"#9C27B0",
|
||||
]
|
||||
]
|
||||
|
||||
defconf = {
|
||||
'isLogged': False,
|
||||
'language': 'pl',
|
||||
}
|
||||
|
||||
usrconf = {
|
||||
'fullName': '',
|
||||
'grade': '',
|
||||
}
|
18
src/i18n.py
18
src/i18n.py
|
@ -1,3 +1,5 @@
|
|||
from utils import getconfigpath
|
||||
import configparser
|
||||
import gettext
|
||||
import locale
|
||||
import json
|
||||
|
@ -5,10 +7,9 @@ import os
|
|||
|
||||
# Determine the user's locale
|
||||
try:
|
||||
with open("config.json", "r") as file:
|
||||
data = json.load(file)
|
||||
langf = data.get("lang")
|
||||
lang = langf
|
||||
config = configparser.ConfigParser()
|
||||
config.read(f"{getconfigpath()}/config.ini")
|
||||
lang = config["DEFAULT"]["language"]
|
||||
except FileNotFoundError:
|
||||
lang = "pl"
|
||||
print("Defaulting to Polish due to missing config file")
|
||||
|
@ -19,18 +20,17 @@ except Exception as e:
|
|||
lang = "pl"
|
||||
print(f"Defaulting to Polish due to error: {e}")
|
||||
finally:
|
||||
print("Language set")
|
||||
|
||||
|
||||
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=True)
|
||||
translation = gettext.translation("base", localedir, languages=[lang], fallback=False)
|
||||
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=True)
|
||||
translation = gettext.translation("base", localedir, languages=[new_lang], fallback=False)
|
||||
translation.install()
|
||||
_ = 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.
|
@ -14,7 +14,7 @@ msgstr ""
|
|||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||
"Language: \n"
|
||||
"MIME-Version: 1.0\n"
|
||||
"Content-Type: text/plain; charset=CHARSET\n"
|
||||
"Content-Type: text/plain; charset=UTF-8\n"
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
|
||||
#: src/main.py:115 src/pages/home.py:9
|
||||
|
|
50
src/main.py
50
src/main.py
|
@ -4,11 +4,11 @@ import base64
|
|||
import gettext
|
||||
import keyring
|
||||
import threading
|
||||
import configparser
|
||||
import flet as ft
|
||||
from i18n import *
|
||||
from utils import *
|
||||
from pages.home import *
|
||||
from pathlib import Path
|
||||
from pages.exams import *
|
||||
from pages.grades import *
|
||||
from pages.homework import *
|
||||
|
@ -16,9 +16,12 @@ from pages.settings import *
|
|||
from pages.timetable import *
|
||||
from pages.behaviour import *
|
||||
from pages.attendance import *
|
||||
from constants import defconf, usrconf
|
||||
from sdk.src.interfaces.prometheus.context import *
|
||||
from sdk.src.interfaces.prometheus.interface import *
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
|
||||
def sync():
|
||||
auth_context_raw = loadauth("Fuji", "Auth Context")
|
||||
auth_context = PrometheusAuthContext.model_validate_json(auth_context_raw)
|
||||
|
@ -182,9 +185,13 @@ def login(page: ft.Page):
|
|||
|
||||
saveauth("Fuji", "Auth Context", jsoncontext)
|
||||
|
||||
config = {"isLoggedIn": True, "lang": "pl"}
|
||||
with open("config.json", "w") as file:
|
||||
json.dump(config, file)
|
||||
config.read(f"{getconfigpath()}/config.ini")
|
||||
config['DEFAULT']['isLogged'] = 'True'
|
||||
config['User']['fullName'] = students[selected_index].full_name
|
||||
config['User']['grade'] = students[selected_index].class_name
|
||||
|
||||
with open(f"{getconfigpath()}/config.ini", "w") as file:
|
||||
config.write(file)
|
||||
|
||||
page.go("/start")
|
||||
|
||||
|
@ -347,14 +354,29 @@ def login(page: ft.Page):
|
|||
# App configuration
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
with open("config.json", "r") as file:
|
||||
data = json.load(file)
|
||||
if data.get("isLoggedIn", False):
|
||||
ft.app(target=main)
|
||||
else:
|
||||
ft.app(target=login)
|
||||
except FileNotFoundError:
|
||||
config = {"isLoggedIn": False, "lang": "pl"}
|
||||
with open("config.json", "w") as file:
|
||||
json.dump(config, file)
|
||||
config.read(f"{getconfigpath()}/config.ini")
|
||||
|
||||
isLogged = config['DEFAULT']['isLogged']
|
||||
|
||||
match isLogged:
|
||||
case 'True':
|
||||
ft.app(target=main)
|
||||
case 'False':
|
||||
ft.app(target=login)
|
||||
|
||||
except (FileNotFoundError, KeyError):
|
||||
os.makedirs(getconfigpath())
|
||||
config['DEFAULT'] = defconf
|
||||
config['User'] = usrconf
|
||||
|
||||
with open(f"{getconfigpath()}/config.ini", "w") as file:
|
||||
config.write(file)
|
||||
ft.app(target=login)
|
||||
|
||||
except FileExistsError:
|
||||
config['DEFAULT'] = defconf
|
||||
config['User'] = usrconf
|
||||
|
||||
with open(f"{getconfigpath()}/config.ini", "w") as file:
|
||||
config.write(file)
|
||||
ft.app(target=login)
|
|
@ -2,8 +2,6 @@ import flet as ft
|
|||
from i18n import *
|
||||
from constants import *
|
||||
|
||||
set_language("pl")
|
||||
|
||||
def HomePage():
|
||||
return ft.Column([
|
||||
ft.Text((_("Home")), size=30, weight="bold"),
|
||||
|
|
14
src/utils.py
14
src/utils.py
|
@ -1,4 +1,6 @@
|
|||
import keyring
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
def saveauth(service, username, data, chunk_size=1000):
|
||||
chunks = [data[i:i+chunk_size] for i in range(0, len(data), chunk_size)]
|
||||
|
@ -13,3 +15,15 @@ def loadauth(service, username):
|
|||
return "".join(keyring.get_password(service, f"{username}_{i}") or "" for i in range(count))
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
def getconfigpath():
|
||||
platform = os.name
|
||||
home = Path.home()
|
||||
|
||||
match platform:
|
||||
case 'posix':
|
||||
return f"{home}/.config/Fuji"
|
||||
case 'Darwin':
|
||||
return f"{home}/.FujiConfig"
|
||||
case 'nt':
|
||||
return f"{home}/AppData/Local/Fuji"
|
||||
|
|
Loading…
Add table
Reference in a new issue