translate_external no longer require the language to translate to

This commit is contained in:
Faraphel 2022-09-06 09:52:38 +02:00
parent 9059824f5a
commit e039ba2bb3
4 changed files with 29 additions and 26 deletions

View file

@ -0,0 +1,10 @@
def get_finished_installation_message(mod_config: "ModConfig") -> str:
message: str = translate_external(
mod_config, mod_config.messages.get("installation_completed", {}).get("text", {})
)
return f"{_('TEXT_INSTALLATION_FINISHED_SUCCESSFULLY')}" + (
f"\n{_('TEXT_MESSAGE_FROM_AUTHOR')} :\n\n{message}" if message != "" else ""
)

View file

@ -9,6 +9,8 @@ from tkinter import messagebox
import webbrowser
from typing import Generator
from source.interface import is_valid_source_path, is_valid_destination_path, is_user_root, are_permissions_enabled, \
get_finished_installation_message
from source.interface.gui import better_gui_error
from source.interface.gui import mod_settings, mystuff
from source.mkw.Game import Game
@ -357,13 +359,13 @@ class ButtonInstall(ttk.Button):
# check if the user entered a source path. If the string is ".", then the user didn't input any path
source_path = Path(self.root.source_path.get())
if not source_path.exists() or str(source_path) == ".":
if not is_valid_source_path(source_path):
messagebox.showerror(_("ERROR"), _("ERROR_INVALID_SOURCE_GAME") % source_path)
return
# check if the user entered a destination path. If the string is ".", then the user didn't input any path
destination_path = Path(self.root.destination_path.get())
if not destination_path.exists() or str(destination_path) == ".":
if not is_valid_destination_path(destination_path):
messagebox.showerror(_("ERROR"), _("ERROR_INVALID_GAME_DESTINATION") % source_path)
return
@ -385,12 +387,11 @@ class ButtonInstall(ttk.Button):
_("WARNING_LOW_SPACE_CONTINUE") % (destination_path.resolve().drive, available_space_destination/Go)
): return
if system == "lin64": # if linux
if os.getuid() != 0: # if the user is not root
if is_user_root():
if not messagebox.askokcancel(_("WARNING"), _("WARNING_NOT_ROOT")):
return
if not os.access("./", os.W_OK | os.X_OK):
if are_permissions_enabled():
# check if writing (for the /.cache/) and execution (for /tools/) are allowed
if not messagebox.askokcancel(_("WARNING"), _("WARNING_INSTALLER_PERMISSION")):
return
@ -407,17 +408,9 @@ class ButtonInstall(ttk.Button):
)
)
message: str = translate_external(
self.root.mod_config,
self.root.options.language.get(),
self.root.mod_config.messages.get("installation_completed", {}).get("text", {})
)
messagebox.showinfo(
_("TEXT_INSTALLATION_COMPLETED"),
f"{_('TEXT_INSTALLATION_FINISHED_SUCCESSFULLY')}" + (
f"\n{_('TEXT_MESSAGE_FROM_AUTHOR')} :\n\n{message}" if message != "" else ""
)
get_finished_installation_message(self.root.mod_config)
)
finally:

View file

@ -54,7 +54,6 @@ class Window(tkinter.Toplevel):
# add at the end a message from the mod creator where he can put some additional note about the settings.
if text := translate_external(
self.mod_config,
self.options.language.get(),
self.mod_config.messages.get("settings_description", {}).get("text", {})
):
self.label_description = ttk.Label(self, text=text, foreground="gray")
@ -91,8 +90,8 @@ class FrameSettings(ttk.Frame):
language = self.root.options.language.get()
for index, (settings_name, settings_data) in enumerate(settings.items()):
text = translate_external(self.root.mod_config, language, settings_data.text)
description = translate_external(self.root.mod_config, language, settings_data.description)
text = translate_external(self.root.mod_config, settings_data.text)
description = translate_external(self.root.mod_config, settings_data.description)
enabled_variable = tkinter.BooleanVar(value=False)
checkbox = ttk.Checkbutton(self, text=f"{text} {settings_data.suffix}", variable=enabled_variable)

View file

@ -7,6 +7,7 @@ if TYPE_CHECKING:
self = __import__(__name__)
self._language = None
self._language_data = {}
@ -16,6 +17,7 @@ def load_language(language: str):
:param language: language code to load
:return:
"""
self._language = language
self._language_data = json.loads(Path(f"./assets/language/{language}.json").read_text(encoding="utf8"))
@ -28,18 +30,17 @@ def translate(text) -> str:
return self._language_data.get("translation", {}).get(text, text)
def translate_external(mod_config: "ModConfig", language: str, message_texts: dict[str, str], default: str = "") -> str:
def translate_external(mod_config: "ModConfig", message_texts: dict[str, str], default: str = "") -> str:
"""
Translate any message that is not from the game.
:param mod_config: the ModConfig object
:param language: the language to translate to
:param message_texts: a dictionary with the translation
:param default: the default message if no translation are found
:return: the translated message
"""
message = message_texts.get(language)
message = message_texts.get(self._language)
if message is None: message = message_texts.get("*")
if message is None: message = default
return mod_config.multiple_safe_eval(message, args=["language"])(language=language)
return mod_config.multiple_safe_eval(message, args=["language"])(language=self._language)