From bf37ec32ecde2138df5dc9397fd1c73be598e692 Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Mon, 27 Nov 2023 18:06:27 +0100 Subject: [PATCH] Start: Allow dynamic translation of start page (#11513) * Allow dynamic translation of start page - fixes #9996 * [pre-commit.ci] auto fixes from pre-commit.com hooks --- src/Mod/Start/Gui/Workbench.cpp | 20 +- src/Mod/Start/StartPage/StartPage.py | 52 ++--- src/Mod/Start/StartPage/TranslationTexts.py | 233 ++++++++++---------- 3 files changed, 160 insertions(+), 145 deletions(-) diff --git a/src/Mod/Start/Gui/Workbench.cpp b/src/Mod/Start/Gui/Workbench.cpp index 8443a266ce..e5f792af1c 100644 --- a/src/Mod/Start/Gui/Workbench.cpp +++ b/src/Mod/Start/Gui/Workbench.cpp @@ -76,6 +76,7 @@ void StartGui::Workbench::loadStartPage() std::string escapedstr = Base::Tools::escapedUnicodeFromUtf8(utf8Title); std::stringstream str; str << "import WebGui,sys,Start\n" + << "from PySide import QtCore\n" << "from StartPage import StartPage\n\n" << "class WebPage(object):\n" << " def __init__(self):\n" @@ -89,16 +90,19 @@ void StartGui::Workbench::loadStartPage() #endif << " def onChange(self, par, reason):\n" << " try:\n" - << " if reason == 'RecentFiles':\n" -#if defined(FC_OS_WIN32) - << " self.browser.setHtml(StartPage.handle(), App.getResourceDir() + " - "'Mod/Start/StartPage/')\n\n" -#else - << " self.browser.setHtml(StartPage.handle(), 'file://' + " - "App.getResourceDir() + 'Mod/Start/StartPage/')\n\n" -#endif + << " if reason in ('RecentFiles','Language'):\n" + << " QtCore.QTimer.singleShot(100, self.reload)\n" << " except RuntimeError as e:\n" << " pass\n" + << " def reload(self):\n" +#if defined(FC_OS_WIN32) + << " self.browser.setHtml(StartPage.handle(), App.getResourceDir() + " + "'Mod/Start/StartPage/')\n\n" +#else + << " self.browser.setHtml(StartPage.handle(), 'file://' + " + "App.getResourceDir() + 'Mod/Start/StartPage/')\n\n" +#endif + << "class WebView(object):\n" << " def __init__(self):\n" << " self.pargrp = FreeCAD.ParamGet('User " diff --git a/src/Mod/Start/StartPage/StartPage.py b/src/Mod/Start/StartPage/StartPage.py index 04361d73d8..c01f5b689c 100644 --- a/src/Mod/Start/StartPage/StartPage.py +++ b/src/Mod/Start/StartPage/StartPage.py @@ -176,8 +176,8 @@ def getInfo(filename): ctime = getLocalTime(s.st_ctime) mtime = getLocalTime(s.st_mtime) author = "" - company = TranslationTexts.T_UNKNOWN - lic = TranslationTexts.T_UNKNOWN + company = TranslationTexts.get("T_UNKNOWN") + lic = TranslationTexts.get("T_UNKNOWN") image = None descr = "" path = os.path.abspath(filename) @@ -310,25 +310,25 @@ def build_new_file_card(template): templates = { "empty_file": [ - TranslationTexts.T_TEMPLATE_EMPTYFILE_NAME, - TranslationTexts.T_TEMPLATE_EMPTYFILE_DESC, + TranslationTexts.get("T_TEMPLATE_EMPTYFILE_NAME"), + TranslationTexts.get("T_TEMPLATE_EMPTYFILE_DESC"), ], "open_file": [ - TranslationTexts.T_TEMPLATE_OPENFILE_NAME, - TranslationTexts.T_TEMPLATE_OPENFILE_DESC, + TranslationTexts.get("T_TEMPLATE_OPENFILE_NAME"), + TranslationTexts.get("T_TEMPLATE_OPENFILE_DESC"), ], "parametric_part": [ - TranslationTexts.T_TEMPLATE_PARAMETRICPART_NAME, - TranslationTexts.T_TEMPLATE_PARAMETRICPART_DESC, + TranslationTexts.get("T_TEMPLATE_PARAMETRICPART_NAME"), + TranslationTexts.get("T_TEMPLATE_PARAMETRICPART_DESC"), ], - # "csg_part": [TranslationTexts.T_TEMPLATE_CSGPART_NAME, TranslationTexts.T_TEMPLATE_CSGPART_DESC], + # "csg_part": [TranslationTexts.get("T_TEMPLATE_CSGPART_NAME"), TranslationTexts.get("T_TEMPLATE_CSGPART_DESC")], "2d_draft": [ - TranslationTexts.T_TEMPLATE_2DDRAFT_NAME, - TranslationTexts.T_TEMPLATE_2DDRAFT_DESC, + TranslationTexts.get("T_TEMPLATE_2DDRAFT_NAME"), + TranslationTexts.get("T_TEMPLATE_2DDRAFT_DESC"), ], "architecture": [ - TranslationTexts.T_TEMPLATE_ARCHITECTURE_NAME, - TranslationTexts.T_TEMPLATE_ARCHITECTURE_DESC, + TranslationTexts.get("T_TEMPLATE_ARCHITECTURE_NAME"), + TranslationTexts.get("T_TEMPLATE_ARCHITECTURE_DESC"), ], } @@ -366,12 +366,12 @@ def buildCard(filename, method, arg=None): finfo = getInfo(filename) if finfo: image, size, author, ctime, mtime, descr, company, lic, path = finfo - infostring = TranslationTexts.T_CREATIONDATE + ": " + ctime + "\n" - infostring += TranslationTexts.T_LASTMODIFIED + ": " + mtime + "\n" - infostring += TranslationTexts.T_SIZE + ": " + size + "\n" - infostring += TranslationTexts.T_AUTHOR + ": " + author + "\n" - infostring += TranslationTexts.T_LICENSE + ": " + lic + "\n" - infostring += TranslationTexts.T_FILEPATH + ": " + path + "\n" + infostring = TranslationTexts.get("T_CREATIONDATE") + ": " + ctime + "\n" + infostring += TranslationTexts.get("T_LASTMODIFIED") + ": " + mtime + "\n" + infostring += TranslationTexts.get("T_SIZE") + ": " + size + "\n" + infostring += TranslationTexts.get("T_AUTHOR") + ": " + author + "\n" + infostring += TranslationTexts.get("T_LICENSE") + ": " + lic + "\n" + infostring += TranslationTexts.get("T_FILEPATH") + ": " + path + "\n" if finfo[5]: infostring += "\n\n" + descr if size: @@ -509,7 +509,7 @@ def handle(): v = FreeCAD.Version() VERSIONSTRING = ( - TranslationTexts.T_VERSION + TranslationTexts.get("T_VERSION") + " " + v[0] + "." @@ -517,7 +517,7 @@ def handle(): + "." + v[2] + " " - + TranslationTexts.T_BUILD + + TranslationTexts.get("T_BUILD") + " " + v[3] ) @@ -525,9 +525,9 @@ def handle(): # translate texts - texts = [t for t in dir(TranslationTexts) if t.startswith("T_")] + texts = [t for t in TranslationTexts.get("index") if t.startswith("T_")] for text in texts: - HTML = HTML.replace(text, getattr(TranslationTexts, text)) + HTML = HTML.replace(text, TranslationTexts.get(text)) # build a "create new" icon with the FreeCAD background color gradient @@ -548,7 +548,7 @@ def handle(): # build SECTION_NEW_FILE - SECTION_NEW_FILE = "

" + TranslationTexts.T_NEWFILE + "

" + SECTION_NEW_FILE = "

" + TranslationTexts.get("T_NEWFILE") + "

" SECTION_NEW_FILE += "