diff --git a/.gitignore b/.gitignore index cd5e1013..5d442ef0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,8 +7,8 @@ *.egg-info setup.iss novelwriter.desktop -*.qm -*.qph +i18n/*.qm +i18n/*.qph # Documentation /docs/build/ diff --git a/nw/config.py b/nw/config.py index ad7e13ca..7daeb540 100644 --- a/nw/config.py +++ b/nw/config.py @@ -393,6 +393,25 @@ class Config: return + def listLanguages(self): + """List localisation files in the i18n folder. The default GUI + language 'en_GB' is British English. + """ + langList = { + "en_GB": QLocale("en_GB").nativeLanguageName().title() + } + 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"): + continue + qmLang = qmFile[3:-3] + qmName = QLocale(qmLang).nativeLanguageName().title() + if qmLang and qmName: + langList[qmLang] = qmName + + return sorted(langList.items(), key=lambda x: x[0]) + def loadConfig(self): """Load preferences from file and replace default settings. """ diff --git a/nw/gui/preferences.py b/nw/gui/preferences.py index 5952f9c5..a919b120 100644 --- a/nw/gui/preferences.py +++ b/nw/gui/preferences.py @@ -137,10 +137,27 @@ class GuiPreferencesGeneral(QWidget): # Look and Feel # ============= self.mainForm.addGroupLabel(self.tr("Look and Feel")) + minWidth = self.mainConf.pxInt(200) + + ## Select Locale + self.guiLang = QComboBox() + self.guiLang.setMinimumWidth(minWidth) + self.theLangs = self.mainConf.listLanguages() + for lang, langName in self.theLangs: + self.guiLang.addItem(langName, lang) + langIdx = self.guiLang.findData(self.mainConf.guiLang) + if langIdx != -1: + self.guiLang.setCurrentIndex(langIdx) + + self.mainForm.addRow( + self.tr("Main GUI language"), + self.guiLang, + self.tr("Changing this requires restarting novelWriter.") + ) ## Select Theme self.guiTheme = QComboBox() - self.guiTheme.setMinimumWidth(self.mainConf.pxInt(200)) + self.guiTheme.setMinimumWidth(minWidth) self.theThemes = self.theTheme.listThemes() for themeDir, themeName in self.theThemes: self.guiTheme.addItem(themeName, themeDir) @@ -156,7 +173,7 @@ class GuiPreferencesGeneral(QWidget): ## Select Icon Theme self.guiIcons = QComboBox() - self.guiIcons.setMinimumWidth(self.mainConf.pxInt(200)) + self.guiIcons.setMinimumWidth(minWidth) self.theIcons = self.theTheme.theIcons.listThemes() for iconDir, iconName in self.theIcons: self.guiIcons.addItem(iconName, iconDir) @@ -240,6 +257,7 @@ class GuiPreferencesGeneral(QWidget): def saveValues(self): """Save the values set for this tab. """ + guiLang = self.guiLang.currentData() guiTheme = self.guiTheme.currentData() guiIcons = self.guiIcons.currentData() guiDark = self.guiDark.isChecked() @@ -248,12 +266,14 @@ class GuiPreferencesGeneral(QWidget): # Check if restart is needed needsRestart = False + needsRestart |= self.mainConf.guiLang != guiLang needsRestart |= self.mainConf.guiTheme != guiTheme needsRestart |= self.mainConf.guiIcons != guiIcons needsRestart |= self.mainConf.guiDark != guiDark needsRestart |= self.mainConf.guiFont != guiFont needsRestart |= self.mainConf.guiFontSize != guiFontSize + self.mainConf.guiLang = guiLang self.mainConf.guiTheme = guiTheme self.mainConf.guiIcons = guiIcons self.mainConf.guiDark = guiDark diff --git a/tests/conftest.py b/tests/conftest.py index 57956aff..59fd5c5d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -61,6 +61,14 @@ def refDir(): theDir = os.path.join(testDir, "reference") return theDir +@pytest.fixture(scope="session") +def filesDir(): + """The folder where additional test files are stored. + """ + testDir = os.path.dirname(__file__) + theDir = os.path.join(testDir, "files") + return theDir + @pytest.fixture(scope="session") def outDir(tmpDir): """An output folder for test results diff --git a/tests/dummy.py b/tests/dummy.py index 72ac5e21..653fca58 100644 --- a/tests/dummy.py +++ b/tests/dummy.py @@ -91,6 +91,16 @@ class DummyStatusBar(): # END Class DummyStatusBar +class DummyApp: + + def __init__(self): + return + + def installTranslator(self, theLang): + return + +# END Class DummyApp + # =========================================================================== # # Error Functions # Dummy functions that will raise errors instead. diff --git a/tests/files/nw_en_GB.qm b/tests/files/nw_en_GB.qm new file mode 100644 index 00000000..be651eed --- /dev/null +++ b/tests/files/nw_en_GB.qm @@ -0,0 +1 @@ +<¸dÊÍ!¿`¡½Ý \ No newline at end of file diff --git a/tests/test_base/test_base_config.py b/tests/test_base/test_base_config.py index 10ca038d..1197ec19 100644 --- a/tests/test_base/test_base_config.py +++ b/tests/test_base/test_base_config.py @@ -27,8 +27,8 @@ import configparser from shutil import copyfile -from dummy import causeOSError -from tools import cmpFiles +from dummy import causeOSError, DummyApp +from tools import cmpFiles, writeFile from nw.config import Config from nw.constants import nwConst, nwFiles @@ -85,7 +85,7 @@ def testBaseConfig_Constructor(monkeypatch): # END Test testBaseConfig_Constructor @pytest.mark.base -def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir): +def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir, filesDir): """Test config intialisation. """ tstConf = Config() @@ -192,6 +192,21 @@ def testBaseConfig_Init(monkeypatch, tmpDir, fncDir, outDir, refDir): tstConf.doReplaceSQuote = orDoSng assert tstConf.saveConfig() + # Localisation + i18nDir = os.path.join(fncDir, "i18n") + os.mkdir(i18nDir) + os.mkdir(os.path.join(i18nDir, "stuff")) + tstConf.nwLangPath = i18nDir + + copyfile(os.path.join(filesDir, "nw_en_GB.qm"), os.path.join(fncDir, "nw_en_GB.qm")) + writeFile(os.path.join(i18nDir, "nw_en_GB.ts"), "") + writeFile(os.path.join(i18nDir, "nw_abcd.qm"), "") + + tstApp = DummyApp() + tstConf.initLocalisation(tstApp) + theList = tstConf.listLanguages() + assert theList == [("en_GB", "British English")] + copyfile(confFile, testFile) assert cmpFiles(testFile, compFile, [2, 9, 10])