From e9763f4ec68a040f90fd8259285b10e5f7f09b07 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 18 Feb 2021 20:45:18 +0100 Subject: [PATCH] Add language option to the Build tool --- nw/config.py | 25 ++++++++++++++++++------- nw/core/project.py | 28 ++++++++++++++++++---------- nw/gui/build.py | 21 ++++++++++++++++++++- nw/gui/preferences.py | 8 ++------ 4 files changed, 58 insertions(+), 24 deletions(-) diff --git a/nw/config.py b/nw/config.py index 037520c8..5e532e73 100644 --- a/nw/config.py +++ b/nw/config.py @@ -53,6 +53,9 @@ class Config: CNF_S_LST = 3 CNF_I_LST = 4 + LANG_NW = 1 + LANG_PROJ = 2 + def __init__(self): # Set Application Variables @@ -391,21 +394,29 @@ class Config: return - def listLanguages(self): + def listLanguages(self, lngSet): """List localisation files in the i18n folder. The default GUI language 'en_GB' is British English. """ - langList = { - "en_GB": QLocale("en_GB").nativeLanguageName().title() - } + if lngSet == self.LANG_NW: + fPre = "nw_" + fExt = ".qm" + langList = {"en_GB": QLocale("en_GB").nativeLanguageName().title()} + elif lngSet == self.LANG_PROJ: + fPre = "project_" + fExt = ".json" + langList = {"en": "English"} + else: + return [] + for qmFile in os.listdir(self.nwLangPath): if not os.path.isfile(os.path.join(self.nwLangPath, qmFile)): continue - if not qmFile.startswith("nw_") or not qmFile.endswith(".qm"): + if not qmFile.startswith(fPre) or not qmFile.endswith(fExt): continue - qmLang = qmFile[3:-3] + qmLang = qmFile[len(fPre):-len(fExt)] qmName = QLocale(qmLang).nativeLanguageName().title() - if qmLang and qmName: + if qmLang and qmName and qmLang != "en": langList[qmLang] = qmName return sorted(langList.items(), key=lambda x: x[0]) diff --git a/nw/core/project.py b/nw/core/project.py index cc4914b9..c00d6a18 100644 --- a/nw/core/project.py +++ b/nw/core/project.py @@ -197,7 +197,7 @@ class NWProject(): self.projContent = None self.projDict = None self.projSpell = None - self.projLang = None + self.projLang = self.mainConf.guiLang self.projFile = nwFiles.PROJ_FILE self.projName = "" self.bookTitle = "" @@ -593,7 +593,7 @@ class NWProject(): self.theParent.setStatus(self.tr("Opened Project: {0}").format(self.projName)) self._scanProjectFolder() - self.loadProjectLocalisation(self.projLang) + self._loadProjectLocalisation() self.currWCount = self.lastWCount self.projOpened = time() @@ -1019,7 +1019,16 @@ class NWProject(): theLang = checkString(theLang, None, True) if self.projSpell != theLang: self.projSpell = theLang - # self.loadProjectLocalisation(theLang) + self.setProjectChanged(True) + return True + + def setProjectLang(self, theLang): + """Set the project-specific language. + """ + theLang = checkString(theLang, None, True) + if self.projLang != theLang: + self.projLang = theLang + self._loadProjectLocalisation() self.setProjectChanged(True) return True @@ -1215,13 +1224,16 @@ class NWProject(): theValue = str(theWord) return self.langData.get(theValue, theValue) - def loadProjectLocalisation(self, theLang): + ## + # Internal Functions + ## + + def _loadProjectLocalisation(self): """Load the language data for the current project language. """ + theLang = self.projLang if theLang is None: theLang = self.mainConf.spellLanguage - if theLang is None: - theLang = "en" lngShort = theLang.split("_")[0] loadFile = os.path.join(self.mainConf.nwLangPath, "project_en.json") @@ -1245,10 +1257,6 @@ class NWProject(): return True - ## - # Internal Functions - ## - def _readLockFile(self): """Reads the lock file in the project folder. """ diff --git a/nw/gui/build.py b/nw/gui/build.py index 1e818733..df66fbcb 100644 --- a/nw/gui/build.py +++ b/nw/gui/build.py @@ -41,7 +41,7 @@ from PyQt5.QtWidgets import ( qApp, QDialog, QVBoxLayout, QHBoxLayout, QTextBrowser, QPushButton, QLabel, QLineEdit, QGroupBox, QGridLayout, QProgressBar, QMenu, QAction, QFileDialog, QFontDialog, QSpinBox, QScrollArea, QSplitter, QWidget, - QSizePolicy, QDoubleSpinBox + QSizePolicy, QDoubleSpinBox, QComboBox ) from nw.common import fuzzyTime, makeFileNameSafe @@ -163,6 +163,16 @@ class GuiBuildNovel(QDialog): self._reFmtCodes(self.theProject.titleFormat["section"]) ) + self.buildLang = QComboBox() + self.buildLang.setMinimumWidth(xFmt) + theLangs = self.mainConf.listLanguages(self.mainConf.LANG_PROJ) + for langID, langName in theLangs: + self.buildLang.addItem(langName, langID) + + langIdx = self.buildLang.findData(self.theProject.projLang) + if langIdx != -1: + self.buildLang.setCurrentIndex(langIdx) + # Dummy boxes due to QGridView and QLineEdit expand bug self.boxTitle = QHBoxLayout() self.boxTitle.addWidget(self.fmtTitle) @@ -180,6 +190,7 @@ class GuiBuildNovel(QDialog): unnumbLabel = QLabel(self.tr("Unnumbered")) sceneLabel = QLabel(self.tr("Scene")) sectionLabel = QLabel(self.tr("Section")) + langLabel = QLabel(self.tr("Language")) self.titleForm.addWidget(titleLabel, 0, 0, 1, 1, Qt.AlignLeft) self.titleForm.addLayout(self.boxTitle, 0, 1, 1, 1, Qt.AlignRight) @@ -191,6 +202,8 @@ class GuiBuildNovel(QDialog): self.titleForm.addLayout(self.boxScene, 3, 1, 1, 1, Qt.AlignRight) self.titleForm.addWidget(sectionLabel, 4, 0, 1, 1, Qt.AlignLeft) self.titleForm.addLayout(self.boxSection, 4, 1, 1, 1, Qt.AlignRight) + self.titleForm.addWidget(langLabel, 5, 0, 1, 1, Qt.AlignLeft) + self.titleForm.addWidget(self.buildLang, 5, 1, 1, 1, Qt.AlignRight) self.titleForm.setColumnStretch(0, 0) self.titleForm.setColumnStretch(1, 1) @@ -651,6 +664,9 @@ class GuiBuildNovel(QDialog): includeBody = self.includeBody.isChecked() replaceUCode = self.replaceUCode.isChecked() + # The language lookup dict is reloaded if needed + self.theProject.setProjectLang(self.buildLang.currentData()) + # Get font information fontInfo = QFontInfo(QFont(textFont, textSize)) textFixed = fontInfo.fixedPitch() @@ -1115,6 +1131,7 @@ class GuiBuildNovel(QDialog): "section" : self.fmtSection.text().strip(), }) + buildLang = self.buildLang.currentData() winWidth = self.mainConf.rpxInt(self.width()) winHeight = self.mainConf.rpxInt(self.height()) justifyText = self.justifyText.isChecked() @@ -1136,6 +1153,8 @@ class GuiBuildNovel(QDialog): boxWidth = self.mainConf.rpxInt(mainSplit[0]) docWidth = self.mainConf.rpxInt(mainSplit[1]) + self.theProject.setProjectLang(buildLang) + # GUI Settings self.optState.setValue("GuiBuildNovel", "winWidth", winWidth) self.optState.setValue("GuiBuildNovel", "winHeight", winHeight) diff --git a/nw/gui/preferences.py b/nw/gui/preferences.py index 09a0c3a9..683eeea4 100644 --- a/nw/gui/preferences.py +++ b/nw/gui/preferences.py @@ -91,7 +91,6 @@ class GuiPreferences(PagedDialog): logger.debug("Saving new preferences") needsRestart = self.tabGeneral.saveValues() - prevSpell = self.mainConf.spellLanguage self.tabProjects.saveValues() self.tabDocs.saveValues() @@ -99,9 +98,6 @@ class GuiPreferences(PagedDialog): self.tabSyntax.saveValues() self.tabAuto.saveValues() - if prevSpell != self.mainConf.spellLanguage: - self.theProject.loadProjectLocalisation(self.mainConf.spellLanguage) - if needsRestart: self.theParent.makeAlert( self.tr("Some changes will not be applied until novelWriter has been restarted."), @@ -142,8 +138,8 @@ class GuiPreferencesGeneral(QWidget): ## Select Locale self.guiLang = QComboBox() self.guiLang.setMinimumWidth(minWidth) - self.theLangs = self.mainConf.listLanguages() - for lang, langName in self.theLangs: + theLangs = self.mainConf.listLanguages(self.mainConf.LANG_NW) + for lang, langName in theLangs: self.guiLang.addItem(langName, lang) langIdx = self.guiLang.findData(self.mainConf.guiLang) if langIdx != -1: