--- a/svghmi/i18n.py Fri Mar 15 09:20:11 2024 +0100
+++ b/svghmi/i18n.py Mon Mar 18 18:43:41 2024 +0100
@@ -15,12 +15,14 @@
+from email.parser import HeaderParser # to have it for python 2, had to install
# https://pypi.org/project/pycountry/18.12.8/
# python2 -m pip install pycountry==18.12.8 --user
from dialogs import MessageBoxOnce
+from POULibrary import UserAddressedException cmd_parser = re.compile(r'(?:"([^"]+)"\s*|([^\s]+)\s*)?')
@@ -67,8 +69,8 @@
- "\n".join([line.text for line in msg]),
- msg.get("label"), msg.get("id")))
+ b"\n".join([line.text.encode() for line in msg]), + msg.get("label").encode(), msg.get("id").encode())) @@ -77,7 +79,7 @@
w.ImportMessages(messages)
- with open(fname, 'w') as POT_file:
+ with open(fname, 'wb') as POT_file: @@ -91,9 +93,8 @@
for translation_name, po_path in GetPoFiles(dirpath):
- with open(po_path, 'r') as PO_file:
- translations.append((translation_name, r.get_messages()))
+ translations.append((translation_name, r.get_messages())) def MatchTranslations(translations, messages, errcallback):
@@ -155,15 +156,17 @@
msgidel = etree.SubElement(msgsroot, "msgid")
msgel = etree.SubElement(msgidel, "msg")
- for line in msg.split("\n"):
+ for line in msg.split(b"\n"): lineel = etree.SubElement(msgel, "line")
- lineel.text = escape(line.encode("utf-8")).decode("utf-8")
+ lineel.text = escape(line).decode()
+# Code below is based on : +# cpython/Tools/i18n/pygettext.py +# cpython/Tools/i18n/msgfmt.py -locpfx = '#:svghmi.svg:'
+locpfx = b'#:svghmi.svg:' # SOME DESCRIPTIVE TITLE.
@@ -185,75 +188,62 @@
-def make_escapes(pass_iso8859):
- escapes = [chr(i) for i in range(256)]
- # Allow iso-8859 characters to pass through so that e.g. 'msgid
- # "Höhe"' would result not result in 'msgid "H\366he"'. Otherwise we
- # escape any character outside the 32..126 range.
- if not(32 <= i <= 126):
- escapes[i] = "\\%03o" % i
- escapes[ord('\\')] = '\\\\'
- escapes[ord('\t')] = '\\t'
- escapes[ord('\r')] = '\\r'
- escapes[ord('\n')] = '\\n'
- escapes[ord('\"')] = '\\"'
+ escapes = [b"\%03o" % i for i in range(128)] + for i in range(32, 127): + escapes[i] = bytes([i]) + escapes[ord('\\')] = b'\\\\' + escapes[ord('\t')] = b'\\t' + escapes[ord('\r')] = b'\\r' + escapes[ord('\n')] = b'\\n' + escapes[ord('\"')] = b'\\"' -make_escapes(pass_iso8859 = True)
- for i in range(len(s)):
- s[i] = escapes[ord(s[i])]
- return EMPTYSTRING.join(s)
+ l = [escapes[c] if c < 128 else bytes([c]) for c in s] + #return bytes([escapes[c] if c < 128 else c for c in s]) # This converts the various Python string types into a format that is
# appropriate for .po files, namely much closer to C style.
- s = '"' + escape(s) + '"'
+ s = b'"' + escape(s) + b'"' - lines[-1] = lines[-1] + '\n'
+ lines[-1] = lines[-1] + b'\n' for i in range(len(lines)):
lines[i] = escape(lines[i])
- s = '""\n"' + lineterm.join(lines) + '"'
+ s = b'""\n"' + lineterm.join(lines) + b'"'
def ImportMessages(self, msgs):
for msg, label, svgid in msgs:
- self.addentry(msg.encode("utf-8"), label, svgid)
+ self.addentry(msg, label, svgid) def addentry(self, msg, label, svgid):
self.__messages.setdefault(msg, set()).add(entry)
- timestamp = time.strftime('%Y-%m-%d %H:%M+%Z')
- print(pot_header % {'time': timestamp}, file=fp)
+ timestamp = time.strftime('%Y-%m-%d %H:%M%z') + header = pot_header % {'time': timestamp} + fp.write(header.encode()) - for k, v in list(self.__messages.items()):
+ for k, v in self.__messages.items(): reverse.setdefault(tuple(keys), []).append((k, v))
- rkeys = list(reverse.keys())
+ rkeys = sorted(reverse.keys()) @@ -262,17 +252,17 @@
- d = {'label': label, 'svgid': svgid}
- s = _(' %(label)s:%(svgid)s') % d
+ d = {b'label': label, b'svgid': svgid} + s = b' %(label)s:%(svgid)s' % d if len(locline) + len(s) <= 78:
- print(locline, file=fp)
if len(locline) > len(locpfx):
- print(locline, file=fp)
- print('msgid', normalize(k), file=fp)
- print('msgstr ""\n', file=fp)
+ fp.write(b'msgid '+normalize(k)) + fp.write(b'msgstr ""\n') @@ -282,27 +272,39 @@
- def add(self, msgid, msgstr, fuzzy):
+ def add(self, ctxt, msgid, msgstr, fuzzy): "Add a non-fuzzy translation to the dictionary."
if not fuzzy and msgstr and msgid:
- self.__messages[msgid.decode('utf-8')] = msgstr.decode('utf-8')
+ self.__messages[msgid] = msgstr + self.__messages[b"%b\x04%b" % (ctxt, id)] = str
+ def read(self, infile):
+ with open(infile, 'rb') as f: + section = msgctxt = None + # Start off assuming Latin-1, so everything decodes without failure, + # until we know the exact encoding # If we get a comment line after a msgstr, this is a new entry
if l[0] == '#' and section == STR:
- self.add(msgid, msgstr, fuzzy)
+ self.add(msgctxt, msgid, msgstr, fuzzy) + section = msgctxt = None if l[:2] == '#,' and 'fuzzy' in l:
@@ -310,56 +312,66 @@
- # Now we are in a msgid section, output previous section
- if l.startswith('msgid') and not l.startswith('msgid_plural'):
+ # Now we are in a msgid or msgctxt section, output previous section + if l.startswith('msgctxt'): - self.add(msgid, msgstr, fuzzy)
+ self.add(msgctxt, msgid, msgstr, fuzzy) + elif l.startswith('msgid') and not l.startswith('msgid_plural'): + self.add(msgctxt, msgid, msgstr, fuzzy) + # See whether there is an encoding declaration + charset = p.parsestr(msgstr.decode(encoding)).get_content_charset()
# This is a message with plural forms
elif l.startswith('msgid_plural'):
- print('msgid_plural not preceded by msgid on %s:%d' %\
- (infile, lno), file=sys.stderr)
+ raise UserAddressedException( + 'msgid_plural not preceded by msgid on %s:%d' % (infile, lno)) - msgid += '\0' # separator of singular and plural
+ msgid += b'\0' # separator of singular and plural # Now we are in a msgstr section
elif l.startswith('msgstr'):
if l.startswith('msgstr['):
- print('plural without msgid_plural on %s:%d' %\
- (infile, lno), file=sys.stderr)
+ raise UserAddressedException( + 'plural without msgid_plural on %s:%d' % (infile, lno)) - msgstr += '\0' # Separator of the various plural forms
+ msgstr += b'\0' # Separator of the various plural forms - print('indexed msgstr required for plural on %s:%d' %\
- (infile, lno), file=sys.stderr)
+ raise UserAddressedException( + 'indexed msgstr required for plural on %s:%d' % (infile, lno))
+ msgctxt += l.encode(encoding) + msgid += l.encode(encoding)
+ msgstr += l.encode(encoding) - print('Syntax error on %s:%d' % (infile, lno), \
- 'before:', file=sys.stderr)
- print(l, file=sys.stderr)
+ raise UserAddressedException( + 'Syntax error on %s:%d' % (infile, lno) + 'before:\n %s'%l) - self.add(msgid, msgstr, fuzzy)
+ self.add(msgctxt, msgid, msgstr, fuzzy) --- a/svghmi/svghmi.py Fri Mar 15 09:20:11 2024 +0100
+++ b/svghmi/svghmi.py Mon Mar 18 18:43:41 2024 +0100
@@ -559,7 +559,7 @@
hmi_tree_root._hash(hasher)
pofiles = GetPoFiles(self.CTNPath())
filestocheck = [svgfile] + \
- (list(zip(*pofiles)[1]) if pofiles else []) + \
+ (list(list(zip(*pofiles))[1]) if pofiles else []) + \ for filetocheck in filestocheck: