Move the i18n initialisation to the Config class

This commit is contained in:
Veronica K. B. Olsen
2021-02-15 00:02:45 +01:00
parent 98e3343808
commit c0acc1d087
7 changed files with 55 additions and 34 deletions
+1 -31
View File
@@ -24,13 +24,10 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import sys
import getopt
import logging
import re
from PyQt5.QtCore import QLibraryInfo, QLocale, QTranslator
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QErrorMessage
@@ -112,21 +109,6 @@ logger = logging.getLogger(__name__)
# Load the main config as a global object
CONFIG = Config()
nw_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "languages")
qt_path = QLibraryInfo.location(QLibraryInfo.TranslationsPath)
translators = {}
def load_translation(app, path, prefix, lang, script = None, country = None):
filename = "_".join(filter(bool, [prefix,
lang and lang.lower(),
script and script.capitalize(),
country and country.upper()]))
if filename not in translators:
translator = QTranslator()
if translator.load(filename, path):
print(filename, path)
app.installTranslator(translator)
translators[filename] = translator
def main(sysArgs=None):
"""Parses command line, sets up logging, and launches main GUI.
@@ -302,20 +284,8 @@ def main(sysArgs=None):
# Connect the exception handler before making the main GUI
sys.excepthook = exceptionHandler
# Load translations
lang, script, country = re.match(
r"^([a-z]{2,3})(?:_([a-z]{4}))?(?:_([a-z]{2,3}))?$",
QLocale.system().name(), re.IGNORECASE).groups()
for path, prefix in ((qt_path, "qt"),
(qt_path, "qtbase"),
(nw_path, "nw")):
load_translation(nwApp, path, prefix, lang)
load_translation(nwApp, path, prefix, lang, script=script)
load_translation(nwApp, path, prefix, lang, country=country)
load_translation(nwApp, path, prefix, lang, script, country)
# Launch main GUI
CONFIG.initTranslations(nwApp)
nwGUI = GuiMain()
if not nwGUI.hasProject:
nwGUI.showProjectLoadDialog()
+52 -2
View File
@@ -30,11 +30,15 @@ import shutil
import json
import sys
import os
import re
from time import time
from PyQt5.Qt import PYQT_VERSION_STR
from PyQt5.QtCore import QT_VERSION_STR, QStandardPaths, QSysInfo
from PyQt5.QtCore import (
QT_VERSION_STR, QStandardPaths, QSysInfo, QLocale, QLibraryInfo,
QTranslator
)
from nw.constants import nwConst, nwFiles, nwUnicode
from nw.common import splitVersionNumber, formatTimeStamp
@@ -75,6 +79,9 @@ class Config:
self.iconPath = None # The full path to the nw/assets/icons folder
self.helpPath = None # The full path to the novelwriter .qhc help file
# Internationalisation
self.qtTrans = {}
# Runtime Settings and Variables
self.confChanged = False # True whenever the config has chenged, false after save
self.hasHelp = False # True if the Qt help files are present in the assets folder
@@ -84,11 +91,11 @@ class Config:
self.guiSyntax = "default_light"
self.guiIcons = "typicons_colour_light"
self.guiDark = False # Load icons for dark backgrounds, if available
self.guiLang = "en" # Hardcoded for now since the GUI is only in English
self.guiFont = "" # Defaults to system default font
self.guiFontSize = 11
self.guiScale = 1.0 # Set automatically by Theme class
self.lastNotes = "0x0" # The latest release notes that have been shown
self.guiLang = QLocale.system().name()
## Sizes
self.winGeometry = [1200, 650]
@@ -354,6 +361,26 @@ class Config:
return True
def initTranslations(self, nwApp):
"""Initialise the internationalisation.
"""
lnName, lnScript, lnCountry = re.match(
r"^([a-z]{2,3})(?:_([a-z]{4}))?(?:_([a-z]{2,3}))?$", self.guiLang, re.IGNORECASE
).groups()
qtLang = QLibraryInfo.location(QLibraryInfo.TranslationsPath)
nwLang = os.path.join(self.appRoot, "i18n")
loadTrans = [
(qtLang, "qt"), (qtLang, "qtbase"), (nwLang, "nw")
]
for lnPath, lnPref in loadTrans:
self._loadTranslation(nwApp, lnPath, lnPref, lnName)
self._loadTranslation(nwApp, lnPath, lnPref, lnName, lnScript=lnScript)
self._loadTranslation(nwApp, lnPath, lnPref, lnName, lnCountry=lnCountry)
self._loadTranslation(nwApp, lnPath, lnPref, lnName, lnScript, lnCountry)
return
def loadConfig(self):
"""Load preferences from file and replace default settings.
"""
@@ -397,6 +424,9 @@ class Config:
self.lastNotes = self._parseLine(
cnfParse, cnfSec, "lastnotes", self.CNF_STR, self.lastNotes
)
self.guiLang = self._parseLine(
cnfParse, cnfSec, "guilang", self.CNF_STR, self.guiLang
)
## Sizes
cnfSec = "Sizes"
@@ -626,6 +656,7 @@ class Config:
cnfParse.set(cnfSec, "guifont", str(self.guiFont))
cnfParse.set(cnfSec, "guifontsize", str(self.guiFontSize))
cnfParse.set(cnfSec, "lastnotes", str(self.lastNotes))
cnfParse.set(cnfSec, "guilang", str(self.guiLang))
## Sizes
cnfSec = "Sizes"
@@ -951,6 +982,25 @@ class Config:
# Internal Functions
##
def _loadTranslation(self, nwApp, lnPath, lnPref, lnName, lnScript=None, lnCountry=None):
"""Load a translator file and create the translation object.
"""
lngFile = "_".join(filter(bool, [
lnPref,
lnName and lnName.lower(),
lnScript and lnScript.capitalize(),
lnCountry and lnCountry.upper()
]))
if lngFile not in self.qtTrans:
qTranslator = QTranslator()
if qTranslator.load(lngFile, lnPath):
logger.debug("Loaded i18n: %s" % os.path.join(lnPath, lngFile))
nwApp.installTranslator(qTranslator)
self.qtTrans[lngFile] = qTranslator
return
def _packList(self, inData):
"""Pack a list of items into a comma-separated string.
"""
+1
View File
@@ -76,6 +76,7 @@ class GuiMain(QMainWindow):
logger.info("Python Version: %s (0x%x)" % (
self.mainConf.verPyString, self.mainConf.verPyHexVal)
)
logger.info("GUI Language: %s" % self.mainConf.guiLang)
# Core Classes
# ============
Binary file not shown.
File diff suppressed because it is too large Load Diff
-447
View File
@@ -1,447 +0,0 @@
<!DOCTYPE QPH>
<QPH sourcelanguage="en" language="pt">
<phrase>
<source>Point of View</source>
<target>Ponto de Vista</target>
</phrase>
<phrase>
<source>Characters</source>
<target>Personagens</target>
</phrase>
<phrase>
<source>Plot</source>
<target>Enredo</target>
</phrase>
<phrase>
<source>Timeline</source>
<target>Linha do Tempo</target>
</phrase>
<phrase>
<source>Locations</source>
<target>Lugares</target>
</phrase>
<phrase>
<source>Objects</source>
<target>Objetos</target>
</phrase>
<phrase>
<source>Entities</source>
<target>Entidades</target>
</phrase>
<phrase>
<source>None</source>
<target>Nenhum</target>
</phrase>
<phrase>
<source>Novel</source>
<target>Livro</target>
</phrase>
<phrase>
<source>Entity</source>
<target>Entidade</target>
</phrase>
<phrase>
<source>Outtakes</source>
<target>Removidos</target>
</phrase>
<phrase>
<source>Trash</source>
<target>Lixeira</target>
</phrase>
<phrase>
<source>Title Page</source>
<target>Página de Título</target>
</phrase>
<phrase>
<source>Book</source>
<target>Livro</target>
</phrase>
<phrase>
<source>Plain Page</source>
<target>Página</target>
</phrase>
<phrase>
<source>Partition</source>
<target>Partição</target>
</phrase>
<phrase>
<source>Unnumbered</source>
<target>Sem Numeração</target>
</phrase>
<phrase>
<source>Scene</source>
<target>Cena</target>
</phrase>
<phrase>
<source>Note</source>
<target>Nota</target>
</phrase>
<phrase>
<source>Title</source>
<target>Título</target>
</phrase>
<phrase>
<source>Level</source>
<target>Nível</target>
</phrase>
<phrase>
<source>Document</source>
<target>Documento</target>
</phrase>
<phrase>
<source>Line</source>
<target>Linha</target>
</phrase>
<phrase>
<source>Chars</source>
<target>Caracteres</target>
</phrase>
<phrase>
<source>Words</source>
<target>Palavras</target>
</phrase>
<phrase>
<source>Synopsis</source>
<target>Sinopse</target>
</phrase>
<phrase>
<source>About</source>
<target>Sobre</target>
</phrase>
<phrase>
<source>Release</source>
<target>Versões</target>
</phrase>
<phrase>
<source>About novelWriter</source>
<target>Sobre o novelWriter</target>
</phrase>
<phrase>
<source>Credits</source>
<target>Créditos</target>
</phrase>
<phrase>
<source>Author</source>
<target>Autor</target>
</phrase>
<phrase>
<source>Credit</source>
<target>Créditos</target>
</phrase>
<phrase>
<source>License</source>
<target>Licença</target>
</phrase>
<phrase>
<source>Theme</source>
<target>Tema</target>
</phrase>
<phrase>
<source>Icons</source>
<target>Ícones</target>
</phrase>
<phrase>
<source>Syntax</source>
<target>Sintaxe</target>
</phrase>
<phrase>
<source>Website</source>
<target>Website</target>
</phrase>
<phrase>
<source>Chapter</source>
<target>Capítulo</target>
</phrase>
<phrase>
<source>Section</source>
<target>Seção</target>
</phrase>
<phrase>
<source>Font family</source>
<target>Família da fonte</target>
</phrase>
<phrase>
<source>Font size</source>
<target>Tamanho da fonte</target>
</phrase>
<phrase>
<source>Justify text</source>
<target>Texto justificado</target>
</phrase>
<phrase>
<source>Print</source>
<target>Imprimir</target>
</phrase>
<phrase>
<source>Build Project</source>
<target>Construir o Projeto</target>
</phrase>
<phrase>
<source>Save As</source>
<target>Salvar Como</target>
</phrase>
<phrase>
<source>Close</source>
<target>Fechar</target>
</phrase>
<phrase>
<source>Plain Text</source>
<target>Texto Simples</target>
</phrase>
<phrase>
<source>Plain HTML</source>
<target>HTML Simples</target>
</phrase>
<phrase>
<source>Save Document As</source>
<target>Salvar Documento Como</target>
</phrase>
<phrase>
<source>Unknown</source>
<target>Desconhecido</target>
</phrase>
<phrase>
<source>Look and Feel</source>
<target>Aparência</target>
</phrase>
<phrase>
<source>Project Backup</source>
<target>Cópia de Segurança</target>
</phrase>
<phrase>
<source>Path</source>
<target>Caminho</target>
</phrase>
<phrase>
<source>Status</source>
<target>Estado</target>
</phrase>
<phrase>
<source>Replace</source>
<target>Substituir</target>
</phrase>
<phrase>
<source>Search</source>
<target>Pesquisa</target>
</phrase>
<phrase>
<source>Handle</source>
<target>Referência</target>
</phrase>
<phrase>
<source>References</source>
<target>Referências</target>
</phrase>
<phrase>
<source>Label</source>
<target>Rótulo</target>
</phrase>
<phrase>
<source>Class</source>
<target>Classe</target>
</phrase>
<phrase>
<source>Layout</source>
<target>Leiaute</target>
</phrase>
<phrase>
<source> Characters</source>
<target>Caracteres</target>
</phrase>
<phrase>
<source>Editor</source>
<target>Editor</target>
</phrase>
<phrase>
<source>Project</source>
<target>Projeto</target>
</phrase>
<phrase>
<source>Provider</source>
<target>Provedor</target>
</phrase>
<phrase>
<source>unknown</source>
<target>desconhecido</target>
</phrase>
<phrase>
<source>Paragraphs</source>
<target>Parágrafos</target>
</phrase>
<phrase>
<source>Default</source>
<target>Padrão</target>
</phrase>
<phrase>
<source>Working title</source>
<target>Nome do projeto</target>
</phrase>
<phrase>
<source>Project path</source>
<target>Caminho do projeto</target>
</phrase>
<phrase>
<source>Project Stats</source>
<target>Estatíticas do Projeto</target>
</phrase>
<phrase>
<source>Folders</source>
<target>Diretórios</target>
</phrase>
<phrase>
<source>Documents</source>
<target>Documentos</target>
</phrase>
<phrase>
<source>Word count</source>
<target>Contagem de palavras</target>
</phrase>
<phrase>
<source>Keyword</source>
<target>Palavra-chave</target>
</phrase>
<phrase>
<source>New</source>
<target>Novo</target>
</phrase>
<phrase>
<source>Delete</source>
<target>Remover</target>
</phrase>
<phrase>
<source>Save</source>
<target>Salvar</target>
</phrase>
<phrase>
<source>Name</source>
<target>Nome</target>
</phrase>
<phrase>
<source>New Item</source>
<target>Novo Item</target>
</phrase>
<phrase>
<source>Last Opened</source>
<target>Aberto Pela Última Vez</target>
</phrase>
<phrase>
<source>Settings</source>
<target>Configurações</target>
</phrase>
<phrase>
<source>Details</source>
<target>Detalhes</target>
</phrase>
<phrase>
<source>Importance</source>
<target>Importância</target>
</phrase>
<phrase>
<source>Auto-Replace</source>
<target>Substituir automaticamente</target>
</phrase>
<phrase>
<source>Flag</source>
<target>Opção</target>
</phrase>
<phrase>
<source>Flags</source>
<target>Opções</target>
</phrase>
<phrase>
<source>(New Entry)</source>
<target></target>
</phrase>
<phrase>
<source>New File</source>
<target>Novo Arquivo</target>
</phrase>
<phrase>
<source>New Folder</source>
<target>Novo Diretório</target>
</phrase>
<phrase>
<source>Histogram</source>
<target>Histograma</target>
</phrase>
<phrase>
<source>Finished</source>
<target>Finalizado</target>
</phrase>
<phrase>
<source>Done</source>
<target>Pronto</target>
</phrase>
<phrase>
<source>Finish</source>
<target>Terminar</target>
</phrase>
<phrase>
<source>Auto-Replace</source>
<target>Substituição Automática</target>
</phrase>
<phrase>
<source>No Suggestions</source>
<target>Sem Sugestões</target>
</phrase>
<phrase>
<source>Browse</source>
<target>Procurar</target>
</phrase>
<phrase>
<source>Tag</source>
<target>Etiqueta</target>
</phrase>
<phrase>
<source>Draft</source>
<target>Rascunho</target>
</phrase>
<phrase>
<source>Minor</source>
<target>Menor</target>
</phrase>
<phrase>
<source>Major</source>
<target>Maior</target>
</phrase>
<phrase>
<source>Backup</source>
<target>Cópia de Segurança</target>
</phrase>
<phrase>
<source>Undo</source>
<target>Desfazer</target>
</phrase>
<phrase>
<source>Release</source>
<target>Lançamento</target>
</phrase>
<phrase>
<source>Pages</source>
<target>Páginas</target>
</phrase>
<phrase>
<source>Page</source>
<target>Página</target>
</phrase>
<phrase>
<source>Progress</source>
<target>Progresso</target>
</phrase>
<phrase>
<source>Chapters</source>
<target>Capítulos</target>
</phrase>
<phrase>
<source>Scenes</source>
<target>Cenas</target>
</phrase>
<phrase>
<source>Revisions</source>
<target>Revisões</target>
</phrase>
<phrase>
<source>seconds</source>
<target>segundos</target>
</phrase>
</QPH>