Work in progress language selection support

Still need to add a popup telling you that NUSGet needs to be restarted after changing the language, and having it load the selected language in the selection menu so that it's checked on launch.
This commit is contained in:
2025-05-26 21:50:04 -04:00
parent e8d6a19d03
commit db6dc65791
8 changed files with 303 additions and 71 deletions

27
modules/config.py Normal file
View File

@@ -0,0 +1,27 @@
# "modules/config.py", licensed under the MIT license
# Copyright 2024-2025 NinjaCheetah & Contributors
import os
import json
import pathlib
def get_config_file() -> pathlib.Path:
config_dir = pathlib.Path(os.path.join(
os.environ.get('APPDATA') or
os.environ.get('XDG_CONFIG_HOME') or
os.path.join(os.environ['HOME'], '.config'),
"NUSGet"
))
config_dir.mkdir(exist_ok=True)
return config_dir.joinpath("config.json")
def save_config(config_data: dict) -> None:
config_file = get_config_file()
print(f"writing data: {config_data}")
open(config_file, "w").write(json.dumps(config_data, indent=4))
def update_setting(config_data: dict, setting: str, value: any) -> None:
config_data[setting] = value
save_config(config_data)

View File

@@ -1,15 +1,12 @@
# "modules/core.py", licensed under the MIT license
# Copyright 2024-2025 NinjaCheetah & Contributors
import os
import json
import pathlib
import requests
from dataclasses import dataclass
from typing import List
from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import QStyledItemDelegate, QSizePolicy
from PySide6.QtWidgets import QStyledItemDelegate
# This is required to make the dropdown look correct with the custom styling. A little fuzzy on the why, but it has to
@@ -63,6 +60,13 @@ def connect_is_enabled_to_checkbox(items, chkbox):
item.setEnabled(False)
def fixup_qmenu_background(menu):
# These fixes are required to not have a square background poking out from behind the rounded corners of a QMenu.
menu.setWindowFlags(menu.windowFlags() | Qt.FramelessWindowHint)
menu.setWindowFlags(menu.windowFlags() | Qt.NoDropShadowWindowHint)
menu.setAttribute(Qt.WA_TranslucentBackground)
def check_nusget_updates(app, current_version: str, progress_callback=None) -> str | None:
# Simple function to make a request to the GitHub API and then check if the latest available version is newer.
gh_api_request = requests.get(url="https://api.github.com/repos/NinjaCheetah/NUSGet/releases/latest", stream=True)
@@ -81,25 +85,3 @@ def check_nusget_updates(app, current_version: str, progress_callback=None) -> s
return new_version
progress_callback.emit(app.translate("MainWindow", "\n\nYou're running the latest release of NUSGet."))
return None
def get_config_file() -> pathlib.Path:
config_dir = pathlib.Path(os.path.join(
os.environ.get('APPDATA') or
os.environ.get('XDG_CONFIG_HOME') or
os.path.join(os.environ['HOME'], '.config'),
"NUSGet"
))
config_dir.mkdir(exist_ok=True)
return config_dir.joinpath("config.json")
def save_config(config_data: dict) -> None:
config_file = get_config_file()
print(f"writing data: {config_data}")
open(config_file, "w").write(json.dumps(config_data))
def update_setting(config_data: dict, setting: str, value: any) -> None:
config_data[setting] = value
save_config(config_data)

64
modules/language.py Normal file
View File

@@ -0,0 +1,64 @@
# "modules/language.py", licensed under the MIT license
# Copyright 2024-2025 NinjaCheetah & Contributors
from modules.config import update_setting
from PySide6.QtCore import QLocale, QTranslator
def set_language(config_data: dict, lang: str) -> None:
# Match the selected language. These names will NOT be translated since they represent each language in that
# language, but the "System (Default)" option will, so that will match the default case.
match lang:
case "English":
print("setting language to English")
update_setting(config_data, "language", "en")
case "Español":
print("setting language to Spanish")
update_setting(config_data, "language", "es")
case "Deutsch":
print("setting language to German")
update_setting(config_data, "language", "de")
case "Français":
print("setting language to French")
update_setting(config_data, "language", "fr")
case "Italiano":
print("setting language to Italian")
update_setting(config_data, "language", "it")
case "Norsk":
print("setting language to Norwegian")
update_setting(config_data, "language", "no")
case "Română":
print("setting language to Romanian")
update_setting(config_data, "language", "ro")
case "한국어":
print("setting language to Korean")
update_setting(config_data, "language", "ko")
case _:
print("setting language to system (default)")
update_setting(config_data, "language", "")
def get_language(translator: QTranslator, config_data: dict, path: str) -> QTranslator:
try:
lang = config_data["language"]
except KeyError:
lang = ""
# A specific language was set in the app's settings.
if lang != "":
if translator.load(QLocale(lang), 'nusget', '_', path):
return translator
else:
# If we get here, then the saved language is invalid, so clear it and run again to use the system language.
update_setting(config_data, "language", "")
return get_language(translator, config_data, path)
else:
# Unix-likes and Windows handle this differently, apparently. Unix-likes will try `nusget_xx_XX.qm` and then
# fall back on just `nusget_xx.qm` if the region-specific translation for the language can't be found. On
# Windows, no such fallback exists, and so this code manually implements that fallback, since for languages like
# Spanish NUSGet doesn't use region-specific translations.
locale = QLocale.system()
if not translator.load(QLocale.system(), 'nusget', '_', path):
base_locale = QLocale(locale.language())
translator.load(base_locale, 'nusget', '_', path)
return translator