Prefer GUI locale for date format (#1755)

This commit is contained in:
Veronica Berglyd Olsen
2024-03-12 22:12:55 +01:00
committed by GitHub
8 changed files with 34 additions and 14 deletions
+19 -2
View File
@@ -30,6 +30,7 @@ import logging
from time import time
from pathlib import Path
from datetime import datetime
from PyQt5.QtGui import QFontDatabase
from PyQt5.QtCore import (
@@ -84,8 +85,11 @@ class Config:
self._nwLangPath = self._appPath / "assets" / "i18n"
self._qtLangPath = QLibraryInfo.location(QLibraryInfo.TranslationsPath)
wantedLocale = self._nwLangPath / f"nw_{QLocale.system().name()}.qm"
self._qLocale = QLocale.system() if wantedLocale.exists() else QLocale("en_GB")
hasLocale = (self._nwLangPath / f"nw_{QLocale.system().name()}.qm").exists()
self._qLocale = QLocale.system() if hasLocale else QLocale("en_GB")
self._dLocale = QLocale.system()
self._dShortDate = self._dLocale.dateFormat(QLocale.FormatType.ShortFormat)
self._dShortDateTime = self._dLocale.dateTimeFormat(QLocale.FormatType.ShortFormat)
self._qtTrans = {}
# PDF Manual
@@ -414,6 +418,14 @@ class Config:
self._errData = []
return message
def localDate(self, value: datetime) -> str:
"""Return a localised date format."""
return self._dLocale.toString(value, self._dShortDate)
def localDateTime(self, value: datetime) -> str:
"""Return a localised datetime format."""
return self._dLocale.toString(value, self._dShortDateTime)
def listLanguages(self, lngSet: int) -> list[tuple[str, str]]:
"""List localisation files in the i18n folder. The default GUI
language is British English (en_GB).
@@ -496,6 +508,11 @@ class Config:
QLocale.setDefault(self._qLocale)
self._qtTrans = {}
hasLocale = (self._nwLangPath / f"nw_{self._qLocale.name()}.qm").exists()
self._dLocale = self._qLocale if hasLocale else QLocale.system()
self._dShortDate = self._dLocale.dateFormat(QLocale.FormatType.ShortFormat)
self._dShortDateTime = self._dLocale.dateTimeFormat(QLocale.FormatType.ShortFormat)
langList = [
(self._qtLangPath, "qtbase"), # Qt 5.x
(str(self._nwLangPath), "nw"), # novelWriter
+3 -6
View File
@@ -151,8 +151,7 @@ class GuiPreferences(QDialog):
self.guiLocale.setMinimumWidth(minWidth)
for lang, name in CONFIG.listLanguages(CONFIG.LANG_NW):
self.guiLocale.addItem(name, lang)
if (idx := self.guiLocale.findData(CONFIG.guiLocale)) != -1:
self.guiLocale.setCurrentIndex(idx)
self.guiLocale.setCurrentData(CONFIG.guiLocale, "en_GB")
self.mainForm.addRow(
self.tr("Display language"), self.guiLocale,
@@ -164,8 +163,7 @@ class GuiPreferences(QDialog):
self.guiTheme.setMinimumWidth(minWidth)
for theme, name in SHARED.theme.listThemes():
self.guiTheme.addItem(name, theme)
if (idx := self.guiTheme.findData(CONFIG.guiTheme)) != -1:
self.guiTheme.setCurrentIndex(idx)
self.guiTheme.setCurrentData(CONFIG.guiTheme, "default")
self.mainForm.addRow(
self.tr("Colour theme"), self.guiTheme,
@@ -226,8 +224,7 @@ class GuiPreferences(QDialog):
self.guiSyntax.setMinimumWidth(CONFIG.pxInt(200))
for syntax, name in SHARED.theme.listSyntax():
self.guiSyntax.addItem(name, syntax)
if (idx := self.guiSyntax.findData(CONFIG.guiSyntax)) != -1:
self.guiSyntax.setCurrentIndex(idx)
self.guiSyntax.setCurrentData(CONFIG.guiSyntax, "default_light")
self.mainForm.addRow(
self.tr("Document colour theme"), self.guiSyntax,
+6
View File
@@ -44,6 +44,12 @@ class NComboBox(QComboBox):
event.ignore()
return
def setCurrentData(self, data: str, default: str) -> None:
"""Set the current index from data, with a fallback."""
idx = self.findData(data)
self.setCurrentIndex(self.findData(default) if idx < 0 else idx)
return
# END Class NComboBox
+1 -1
View File
@@ -57,7 +57,7 @@ class VersionInfoWidget(QWidget):
# Labels
self._lblInfo = QLabel("{0} {1} \u2013 {2} {3} \u2013 {4}".format(
self.tr("Version"), formatVersion(__version__),
self.tr("Released on"), datetime.strptime(__date__, "%Y-%m-%d").strftime("%x"),
self.tr("Released on"), CONFIG.localDate(datetime.strptime(__date__, "%Y-%m-%d")),
"<a href='#notes'>{0}</a>".format(self.tr("Release Notes")),
), self)
self._lblInfo.linkActivated.connect(self._processLink)
+1 -1
View File
@@ -457,7 +457,7 @@ class GuiMain(QMainWindow):
"'{0}' ({1} {2}), last active on {3}."
).format(
lockStatus[0], lockStatus[1], lockStatus[2],
datetime.fromtimestamp(int(lockStatus[3])).strftime("%x %X")
CONFIG.localDateTime(datetime.fromtimestamp(int(lockStatus[3])))
)
except Exception:
lockDetails = ""
+1 -1
View File
@@ -829,7 +829,7 @@ class _PreviewWidget(QTextBrowser):
"""Update the build time and the fuzzy age."""
if self._docTime > 0:
strBuildTime = "%s (%s)" % (
datetime.fromtimestamp(self._docTime).strftime("%x %X"),
CONFIG.localDateTime(datetime.fromtimestamp(self._docTime)),
fuzzyTime(int(time()) - self._docTime)
)
else:
+1 -1
View File
@@ -456,7 +456,7 @@ class _ProjectListModel(QAbstractListModel):
opened = self.tr("Last Opened")
records = sorted(CONFIG.recentProjects.listEntries(), key=lambda x: x[3], reverse=True)
for path, title, count, time in records:
when = datetime.fromtimestamp(time).strftime("%x")
when = CONFIG.localDate(datetime.fromtimestamp(time))
data.append((title, path, f"{opened}: {when}, {words}: {formatInt(count)}"))
self._data = data
return
+2 -2
View File
@@ -75,8 +75,8 @@ def testToolWelcome_Open(qtbot: QtBot, monkeypatch, nwGUI, fncPath):
CONFIG.recentProjects.update("/stuff/project_one", "Project One", 12345, 1690000000)
CONFIG.recentProjects.update("/stuff/project_two", "Project Two", 54321, 1700000000)
dateOne = datetime.fromtimestamp(1700000000).strftime("%x")
dateTwo = datetime.fromtimestamp(1690000000).strftime("%x")
dateOne = CONFIG.localDate(datetime.fromtimestamp(1700000000))
dateTwo = CONFIG.localDate(datetime.fromtimestamp(1690000000))
welcome = GuiWelcome(nwGUI)
with qtbot.waitExposed(welcome):