From 433bf08b9b4b9774d540774cafe315653d4cbe45 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Wed, 17 Aug 2022 18:43:40 +0200 Subject: [PATCH] added translate_external for message that need translation from outside of the installer. --- Pack/MKWFaraphel/messages.json | 6 ++++++ assets/language/en.json | 1 - assets/language/fr.json | 1 - source/gui/install.py | 16 +++++++++------- source/gui/mod_settings.py | 10 ++++++---- source/translation.py | 17 +++++++++++++++++ 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/Pack/MKWFaraphel/messages.json b/Pack/MKWFaraphel/messages.json index 01384c8..0afd35a 100644 --- a/Pack/MKWFaraphel/messages.json +++ b/Pack/MKWFaraphel/messages.json @@ -1,4 +1,10 @@ { + "specific_settings_description": { + "text": { + "fr": "* Ceci activera le mode \"triche\"", + "en": "* This will enable the \"cheat\" mode" + } + }, "installation_completed": { "text": { "fr": "Merci d'avoir téléchargé {{ mod_config.name }} !\nSi vous avez un problème en démarrant le jeu avec Dolphin, essayer dans Configurer > Avancé > Modifier la taille de la mémoire émulée et mettre MEM2 à 128MB\n\nAmusez-vous !", diff --git a/assets/language/en.json b/assets/language/en.json index 94c6a00..ab4348c 100644 --- a/assets/language/en.json +++ b/assets/language/en.json @@ -29,7 +29,6 @@ "USE": "Use", "THREADS": "threads", "MESSAGE_FROM_MOD_AUTHOR": "Message from the author", - "NO_MESSAGE_FROM_AUTHOR": "< The author didn't left any message >", "GLOBAL_MOD_SETTINGS": "Global mod settings", "SPECIFIC_MOD_SETTINGS": "Specific mod settings", "CONFIGURE_MYSTUFF_PATCH": "Configure the MyStuff patchs", diff --git a/assets/language/fr.json b/assets/language/fr.json index 05e2cb3..a727a7d 100644 --- a/assets/language/fr.json +++ b/assets/language/fr.json @@ -30,7 +30,6 @@ "USE": "Utiliser", "THREADS": "threads", "MESSAGE_FROM_MOD_AUTHOR": "Message de l'auteur", - "NO_MESSAGE_FROM_AUTHOR": "< L'auteur n'a pas laissé de message >", "GLOBAL_MOD_SETTINGS": "Paramètre global de mod", "SPECIFIC_MOD_SETTINGS": "Paramètre spécifique de mod", "CONFIGURE_MYSTUFF_PATCH": "Configurer les patchs MyStuff", diff --git a/source/gui/install.py b/source/gui/install.py index 7527576..69d827b 100644 --- a/source/gui/install.py +++ b/source/gui/install.py @@ -14,7 +14,7 @@ from source.mkw.Game import Game from source.mkw.ModConfig import ModConfig from source.option import Option from source.progress import Progress -from source.translation import translate as _ +from source.translation import translate as _, translate_external from source import plugins from source import * import os @@ -432,15 +432,17 @@ class ButtonInstall(ttk.Button): ) ) - message_texts = mod_config.messages.get("installation_completed", {}).get("text", {}) - message = message_texts.get(self.root.options["language"]) - if message is None: message = message_texts.get("*") - if message is None: message = _('NO_MESSAGE_FROM_AUTHOR') - message = mod_config.multiple_safe_eval(message)() + message: str = translate_external( + mod_config, + self.root.options["language"], + mod_config.messages.get("installation_completed", {}).get("text", {}) + ) messagebox.showinfo( _("INSTALLATION_COMPLETED"), - f"{_('INSTALLATION_FINISHED_WITH_SUCCESS')}\n{_('MESSAGE_FROM_MOD_AUTHOR')} :\n\n{message}" + f"{_('INSTALLATION_FINISHED_WITH_SUCCESS')}" + ( + f"\n{_('MESSAGE_FROM_MOD_AUTHOR')} :\n\n{message}" if message != "" else "" + ) ) finally: diff --git a/source/gui/mod_settings.py b/source/gui/mod_settings.py index d14f878..df50528 100644 --- a/source/gui/mod_settings.py +++ b/source/gui/mod_settings.py @@ -2,7 +2,7 @@ import tkinter from tkinter import ttk from typing import TYPE_CHECKING -from source.translation import translate as _ +from source.translation import translate as _, translate_external if TYPE_CHECKING: from source.mkw.ModConfig import ModConfig @@ -63,9 +63,11 @@ class FrameSettings(ttk.Frame): return lambda event: enabled_variable.set(True) for index, (settings_name, settings_data) in enumerate(settings.items()): - text = settings_data.text.get(self.root.options["language"]) - if text is None: text = settings_data.text.get("*") - if text is None: text = settings_name + text = translate_external( + self.master.master.mod_config, + self.root.options["language"], + settings_data.text, + ) enabled_variable = tkinter.BooleanVar(value=False) checkbox = ttk.Checkbutton(self, text=text, variable=enabled_variable) diff --git a/source/translation.py b/source/translation.py index d08439e..d5a897b 100644 --- a/source/translation.py +++ b/source/translation.py @@ -1,5 +1,9 @@ import json from pathlib import Path +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from source.mkw.ModConfig import ModConfig self = __import__(__name__) @@ -28,5 +32,18 @@ def translate(*text) -> str: ]) +def translate_external(mod_config: "ModConfig", language: str, 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) + if message is None: message = message_texts.get("*") + if message is None: message = default + return mod_config.multiple_safe_eval(message)()