Add local formatting of dates with fallback to system locale

This commit is contained in:
Veronica Berglyd Olsen
2024-03-12 22:01:07 +01:00
parent 370116718a
commit dc2c6d2a0d
+19 -2
View File
@@ -30,6 +30,7 @@ import logging
from time import time from time import time
from pathlib import Path from pathlib import Path
from datetime import datetime
from PyQt5.QtGui import QFontDatabase from PyQt5.QtGui import QFontDatabase
from PyQt5.QtCore import ( from PyQt5.QtCore import (
@@ -84,8 +85,11 @@ class Config:
self._nwLangPath = self._appPath / "assets" / "i18n" self._nwLangPath = self._appPath / "assets" / "i18n"
self._qtLangPath = QLibraryInfo.location(QLibraryInfo.TranslationsPath) self._qtLangPath = QLibraryInfo.location(QLibraryInfo.TranslationsPath)
wantedLocale = self._nwLangPath / f"nw_{QLocale.system().name()}.qm" hasLocale = (self._nwLangPath / f"nw_{QLocale.system().name()}.qm").exists()
self._qLocale = QLocale.system() if wantedLocale.exists() else QLocale("en_GB") 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 = {} self._qtTrans = {}
# PDF Manual # PDF Manual
@@ -414,6 +418,14 @@ class Config:
self._errData = [] self._errData = []
return message 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]]: def listLanguages(self, lngSet: int) -> list[tuple[str, str]]:
"""List localisation files in the i18n folder. The default GUI """List localisation files in the i18n folder. The default GUI
language is British English (en_GB). language is British English (en_GB).
@@ -496,6 +508,11 @@ class Config:
QLocale.setDefault(self._qLocale) QLocale.setDefault(self._qLocale)
self._qtTrans = {} 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 = [ langList = [
(self._qtLangPath, "qtbase"), # Qt 5.x (self._qtLangPath, "qtbase"), # Qt 5.x
(str(self._nwLangPath), "nw"), # novelWriter (str(self._nwLangPath), "nw"), # novelWriter