Change to use document locale for integers

This commit is contained in:
Veronica Berglyd Olsen
2024-10-28 20:22:02 +01:00
parent d987246062
commit 9bc1561e01
12 changed files with 37 additions and 43 deletions
+4 -4
View File
@@ -252,6 +252,10 @@ class Config:
def hasError(self) -> bool: def hasError(self) -> bool:
return self._hasError return self._hasError
@property
def locale(self) -> QLocale:
return self._dLocale
@property @property
def recentProjects(self) -> RecentProjects: def recentProjects(self) -> RecentProjects:
return self._recentProjects return self._recentProjects
@@ -469,10 +473,6 @@ class Config:
"""Return a localised datetime format.""" """Return a localised datetime format."""
return self._dLocale.toString(value, self._dShortDateTime) return self._dLocale.toString(value, self._dShortDateTime)
def localInt(self, value: int) -> str:
"""Return a localised integer."""
return self._dLocale.toString(value)
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).
+2 -1
View File
@@ -224,7 +224,9 @@ class NWBuildDocument:
# Get Settings # Get Settings
textFont = QFont(CONFIG.textFont) textFont = QFont(CONFIG.textFont)
textFont.fromString(self._build.getStr("format.textFont")) textFont.fromString(self._build.getStr("format.textFont"))
bldObj.setFont(textFont) bldObj.setFont(textFont)
bldObj.setLanguage(self._project.data.language)
bldObj.setPartitionFormat( bldObj.setPartitionFormat(
self._build.getStr("headings.fmtPart"), self._build.getStr("headings.fmtPart"),
@@ -322,7 +324,6 @@ class NWBuildDocument:
bldObj.setReplaceUnicode(self._build.getBool("format.stripUnicode")) bldObj.setReplaceUnicode(self._build.getBool("format.stripUnicode"))
if isinstance(bldObj, (ToOdt, ToDocX)): if isinstance(bldObj, (ToOdt, ToDocX)):
bldObj.setLanguage(self._project.data.language)
bldObj.setHeaderFormat( bldObj.setHeaderFormat(
self._build.getStr("doc.pageHeader"), self._build.getStr("doc.pageHeader"),
self._build.getInt("doc.pageCountOffset"), self._build.getInt("doc.pageCountOffset"),
+4 -11
View File
@@ -36,7 +36,7 @@ from zipfile import ZIP_DEFLATED, ZipFile
from PyQt5.QtCore import QMarginsF, QSizeF from PyQt5.QtCore import QMarginsF, QSizeF
from PyQt5.QtGui import QColor from PyQt5.QtGui import QColor
from novelwriter import CONFIG, __version__ from novelwriter import __version__
from novelwriter.common import firstFloat, xmlSubElem from novelwriter.common import firstFloat, xmlSubElem
from novelwriter.constants import nwHeadFmt, nwStyles from novelwriter.constants import nwHeadFmt, nwStyles
from novelwriter.core.project import NWProject from novelwriter.core.project import NWProject
@@ -182,7 +182,6 @@ class ToDocX(Tokenizer):
# Internal # Internal
self._fontFamily = "Liberation Serif" self._fontFamily = "Liberation Serif"
self._fontSize = 12.0 self._fontSize = 12.0
self._dLanguage = "en_GB"
self._pageSize = QSizeF(210.0, 297.0) self._pageSize = QSizeF(210.0, 297.0)
self._pageMargins = QMarginsF(20.0, 20.0, 20.0, 20.0) self._pageMargins = QMarginsF(20.0, 20.0, 20.0, 20.0)
@@ -200,12 +199,6 @@ class ToDocX(Tokenizer):
# Setters # Setters
## ##
def setLanguage(self, language: str | None) -> None:
"""Set language for the document."""
if language:
self._dLanguage = language.replace("_", "-")
return
def setPageLayout( def setPageLayout(
self, width: float, height: float, top: float, bottom: float, left: float, right: float self, width: float, height: float, top: float, bottom: float, left: float, right: float
) -> None: ) -> None:
@@ -740,7 +733,7 @@ class ToDocX(Tokenizer):
xmlSubElem(xRoot, _mkTag("dcterms", "modified"), timeStamp, attrib=tsAttr) xmlSubElem(xRoot, _mkTag("dcterms", "modified"), timeStamp, attrib=tsAttr)
xmlSubElem(xRoot, _mkTag("dc", "creator"), self._project.data.author) xmlSubElem(xRoot, _mkTag("dc", "creator"), self._project.data.author)
xmlSubElem(xRoot, _mkTag("dc", "title"), self._project.data.name) xmlSubElem(xRoot, _mkTag("dc", "title"), self._project.data.name)
xmlSubElem(xRoot, _mkTag("dc", "language"), self._dLanguage) xmlSubElem(xRoot, _mkTag("dc", "language"), self._dLocale.name())
xmlSubElem(xRoot, _mkTag("cp", "revision"), str(self._project.data.saveCount)) xmlSubElem(xRoot, _mkTag("cp", "revision"), str(self._project.data.saveCount))
xmlSubElem(xRoot, _mkTag("cp", "lastModifiedBy"), self._project.data.author) xmlSubElem(xRoot, _mkTag("cp", "lastModifiedBy"), self._project.data.author)
@@ -777,7 +770,7 @@ class ToDocX(Tokenizer):
}) })
xmlSubElem(xRPr, _wTag("sz"), attrib={W_VAL: size}) xmlSubElem(xRPr, _wTag("sz"), attrib={W_VAL: size})
xmlSubElem(xRPr, _wTag("szCs"), attrib={W_VAL: size}) xmlSubElem(xRPr, _wTag("szCs"), attrib={W_VAL: size})
xmlSubElem(xRPr, _wTag("lang"), attrib={W_VAL: self._dLanguage}) xmlSubElem(xRPr, _wTag("lang"), attrib={W_VAL: self._dLocale.name()})
xmlSubElem(xPPr, _wTag("spacing"), attrib={_wTag("line"): line}) xmlSubElem(xPPr, _wTag("spacing"), attrib={_wTag("line"): line})
# Paragraph Styles # Paragraph Styles
@@ -936,7 +929,7 @@ class ToDocX(Tokenizer):
if self._usedFields and self._counts: if self._usedFields and self._counts:
for xField, field in self._usedFields: for xField, field in self._usedFields:
if (value := self._counts.get(field)) is not None: if (value := self._counts.get(field)) is not None:
xField.text = CONFIG.localInt(value) xField.text = self._formatInt(value)
# Write Paragraphs # Write Paragraphs
for par in pars: for par in pars:
+1 -2
View File
@@ -29,7 +29,6 @@ import logging
from pathlib import Path from pathlib import Path
from time import time from time import time
from novelwriter import CONFIG
from novelwriter.common import formatTimeStamp from novelwriter.common import formatTimeStamp
from novelwriter.constants import nwHtmlUnicode from novelwriter.constants import nwHtmlUnicode
from novelwriter.core.project import NWProject from novelwriter.core.project import NWProject
@@ -259,7 +258,7 @@ class ToHtml(Tokenizer):
for doc, field in self._usedFields: for doc, field in self._usedFields:
if doc >= 0 and doc < pages and (value := self._counts.get(field)) is not None: if doc >= 0 and doc < pages and (value := self._counts.get(field)) is not None:
self._pages[doc] = self._pages[doc].replace( self._pages[doc] = self._pages[doc].replace(
f"{{{{{field}}}}}", CONFIG.localInt(value) f"{{{{{field}}}}}", self._formatInt(value)
) )
# Add footnotes # Add footnotes
+12
View File
@@ -31,6 +31,7 @@ from abc import ABC, abstractmethod
from pathlib import Path from pathlib import Path
from typing import NamedTuple from typing import NamedTuple
from PyQt5.QtCore import QLocale
from PyQt5.QtGui import QColor, QFont from PyQt5.QtGui import QColor, QFont
from novelwriter import CONFIG from novelwriter import CONFIG
@@ -104,6 +105,7 @@ class Tokenizer(ABC):
self._outline: dict[str, str] = {} self._outline: dict[str, str] = {}
# User Settings # User Settings
self._dLocale = CONFIG.locale # The document locale
self._textFont = QFont("Serif", 11) # Output text font self._textFont = QFont("Serif", 11) # Output text font
self._lineHeight = 1.15 # Line height in units of em self._lineHeight = 1.15 # Line height in units of em
self._colorHeads = True # Colourise headings self._colorHeads = True # Colourise headings
@@ -226,6 +228,12 @@ class Tokenizer(ABC):
# Setters # Setters
## ##
def setLanguage(self, language: str | None) -> None:
"""Set language for the document."""
if language:
self._dLocale = QLocale(language)
return
def setTheme(self, theme: TextDocumentTheme) -> None: def setTheme(self, theme: TextDocumentTheme) -> None:
"""Set the document colour theme.""" """Set the document colour theme."""
self._theme = theme self._theme = theme
@@ -1033,6 +1041,10 @@ class Tokenizer(ABC):
# Internal Functions # Internal Functions
## ##
def _formatInt(self, value: int) -> str:
"""Return a localised integer."""
return self._dLocale.toString(value)
def _formatComment(self, style: ComStyle, key: str, text: str) -> tuple[str, T_Formats]: def _formatComment(self, style: ComStyle, key: str, text: str) -> tuple[str, T_Formats]:
"""Apply formatting to comments and notes.""" """Apply formatting to comments and notes."""
tTxt, tFmt = self._extractFormats(text) tTxt, tFmt = self._extractFormats(text)
+1 -2
View File
@@ -27,7 +27,6 @@ import logging
from pathlib import Path from pathlib import Path
from novelwriter import CONFIG
from novelwriter.constants import nwUnicode from novelwriter.constants import nwUnicode
from novelwriter.core.project import NWProject from novelwriter.core.project import NWProject
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt
@@ -159,7 +158,7 @@ class ToMarkdown(Tokenizer):
for doc, field in self._usedFields: for doc, field in self._usedFields:
if doc >= 0 and doc < pages and (value := self._counts.get(field)) is not None: if doc >= 0 and doc < pages and (value := self._counts.get(field)) is not None:
self._pages[doc] = self._pages[doc].replace( self._pages[doc] = self._pages[doc].replace(
f"{{{{{field}}}}}", CONFIG.localInt(value) f"{{{{{field}}}}}", self._formatInt(value)
) )
# Add footnotes # Add footnotes
+4 -8
View File
@@ -223,14 +223,6 @@ class ToOdt(Tokenizer):
# Setters # Setters
## ##
def setLanguage(self, language: str | None) -> None:
"""Set language for the document."""
if language:
lang, _, country = language.partition("_")
self._dLanguage = lang or self._dLanguage
self._dCountry = country or self._dCountry
return
def setPageLayout( def setPageLayout(
self, width: float, height: float, top: float, bottom: float, left: float, right: float self, width: float, height: float, top: float, bottom: float, left: float, right: float
) -> None: ) -> None:
@@ -264,6 +256,10 @@ class ToOdt(Tokenizer):
fontWeight = str(intWeight) fontWeight = str(intWeight)
fontBold = str(min(intWeight + 300, 900)) fontBold = str(min(intWeight + 300, 900))
lang, _, country = self._dLocale.name().partition("_")
self._dLanguage = lang or self._dLanguage
self._dCountry = country or self._dCountry
self._fontFamily = self._textFont.family() self._fontFamily = self._textFont.family()
self._fontSize = self._textFont.pointSize() self._fontSize = self._textFont.pointSize()
self._fontWeight = FONT_WEIGHT_MAP.get(fontWeight, fontWeight) self._fontWeight = FONT_WEIGHT_MAP.get(fontWeight, fontWeight)
+1 -2
View File
@@ -34,7 +34,6 @@ from PyQt5.QtGui import (
) )
from PyQt5.QtPrintSupport import QPrinter from PyQt5.QtPrintSupport import QPrinter
from novelwriter import CONFIG
from novelwriter.constants import nwStyles, nwUnicode from novelwriter.constants import nwStyles, nwUnicode
from novelwriter.core.project import NWProject from novelwriter.core.project import NWProject
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt
@@ -259,7 +258,7 @@ class ToQTextDocument(Tokenizer):
if (value := self._counts.get(field)) is not None: if (value := self._counts.get(field)) is not None:
cursor.setPosition(pos, QtMoveAnchor) cursor.setPosition(pos, QtMoveAnchor)
cursor.setPosition(pos + 1, QtKeepAnchor) cursor.setPosition(pos + 1, QtKeepAnchor)
cursor.insertText(CONFIG.localInt(value)) cursor.insertText(self._formatInt(value))
# Add footnotes # Add footnotes
if self._usedNotes: if self._usedNotes:
@@ -1,10 +1,10 @@
<?xml version='1.0' encoding='utf-8'?> <?xml version='1.0' encoding='utf-8'?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dcterms:created xsi:type="dcterms:W3CDTF">2024-10-28T18:23:43</dcterms:created> <dcterms:created xsi:type="dcterms:W3CDTF">2024-10-28T20:12:57</dcterms:created>
<dcterms:modified xsi:type="dcterms:W3CDTF">2024-10-28T18:23:43</dcterms:modified> <dcterms:modified xsi:type="dcterms:W3CDTF">2024-10-28T20:12:57</dcterms:modified>
<dc:creator>lipsum.com</dc:creator> <dc:creator>lipsum.com</dc:creator>
<dc:title>Lorem Ipsum</dc:title> <dc:title>Lorem Ipsum</dc:title>
<dc:language>en-GB</dc:language> <dc:language>en_GB</dc:language>
<cp:revision>50</cp:revision> <cp:revision>50</cp:revision>
<cp:lastModifiedBy>lipsum.com</cp:lastModifiedBy> <cp:lastModifiedBy>lipsum.com</cp:lastModifiedBy>
</cp:coreProperties> </cp:coreProperties>
@@ -6,7 +6,7 @@
<w:rFonts w:ascii="Source Sans Pro" w:hAnsi="Source Sans Pro" w:cs="Source Sans Pro" /> <w:rFonts w:ascii="Source Sans Pro" w:hAnsi="Source Sans Pro" w:cs="Source Sans Pro" />
<w:sz w:val="24" /> <w:sz w:val="24" />
<w:szCs w:val="24" /> <w:szCs w:val="24" />
<w:lang w:val="en-GB" /> <w:lang w:val="en_GB" />
</w:rPr> </w:rPr>
</w:rPrDefault> </w:rPrDefault>
<w:pPrDefault> <w:pPrDefault>
@@ -1,13 +1,13 @@
<?xml version='1.0' encoding='utf-8'?> <?xml version='1.0' encoding='utf-8'?>
<office:document xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.text"> <office:document xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.text">
<office:meta> <office:meta>
<meta:creation-date>2024-10-22T14:19:48</meta:creation-date> <meta:creation-date>2024-10-28T20:17:34</meta:creation-date>
<meta:generator>novelWriter/2.6a1</meta:generator> <meta:generator>novelWriter/2.6a3</meta:generator>
<meta:initial-creator>Jane Smith</meta:initial-creator> <meta:initial-creator>Jane Smith</meta:initial-creator>
<meta:editing-cycles>1234</meta:editing-cycles> <meta:editing-cycles>1234</meta:editing-cycles>
<meta:editing-duration>P42DT12H34M56S</meta:editing-duration> <meta:editing-duration>P42DT12H34M56S</meta:editing-duration>
<dc:title>Test Project</dc:title> <dc:title>Test Project</dc:title>
<dc:date>2024-10-22T14:19:48</dc:date> <dc:date>2024-10-28T20:17:34</dc:date>
<dc:creator>Jane Smith</dc:creator> <dc:creator>Jane Smith</dc:creator>
</office:meta> </office:meta>
<office:font-face-decls> <office:font-face-decls>
@@ -16,7 +16,7 @@
<office:styles> <office:styles>
<style:default-style style:family="paragraph"> <style:default-style style:family="paragraph">
<style:paragraph-properties style:line-break="strict" style:tab-stop-distance="1.251cm" style:writing-mode="page" /> <style:paragraph-properties style:line-break="strict" style:tab-stop-distance="1.251cm" style:writing-mode="page" />
<style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-weight="normal" fo:font-style="normal" fo:font-size="12pt" fo:language="nb" fo:country="NO" /> <style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-weight="normal" fo:font-style="normal" fo:font-size="12pt" fo:language="en" fo:country="GB" />
</style:default-style> </style:default-style>
<style:style style:name="Standard" style:family="paragraph" style:class="text"> <style:style style:name="Standard" style:family="paragraph" style:class="text">
<style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-weight="normal" fo:font-style="normal" fo:font-size="12pt" /> <style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-weight="normal" fo:font-style="normal" fo:font-size="12pt" />
-5
View File
@@ -780,11 +780,6 @@ def testFmtToOdt_SaveFlat(mockGUI, fncPath, tstPaths):
odt = ToOdt(project, isFlat=True) odt = ToOdt(project, isFlat=True)
odt._isNovel = True odt._isNovel = True
odt._dLanguage = ""
odt.setLanguage(None) # type: ignore
assert odt._dLanguage == ""
odt.setLanguage("nb_NO")
assert odt._dLanguage == "nb"
odt.setHeaderFormat(nwHeadFmt.DOC_AUTO, 1) odt.setHeaderFormat(nwHeadFmt.DOC_AUTO, 1)
assert odt._headerFormat == nwHeadFmt.DOC_AUTO assert odt._headerFormat == nwHeadFmt.DOC_AUTO
odt.setFirstLineIndent(True, 1.4, False) odt.setFirstLineIndent(True, 1.4, False)