Krita Source Code Documentation
Loading...
Searching...
No Matches
CPMT_po_parser.py
Go to the documentation of this file.
1"""
2SPDX-FileCopyrightText: 2018 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3
4This file is part of the Comics Project Management Tools(CPMT).
5
6SPDX-License-Identifier: GPL-3.0-or-later
7"""
8
9"""
10A thing that parses through POT files.
11"""
12
13import os
14import re
15
17 translationDict = {}
18 translationList = []
19 key_xml = False
20
21 def __init__(self, translationLocation, key_xml = False):
22 self.key_xmlkey_xml = key_xml
23 if os.path.exists(translationLocation):
24 for entry in os.scandir(translationLocation):
25 if entry.name.endswith('.po') and entry.is_file():
26 self.parse_pot(os.path.join(translationLocation, entry.name))
27
28 def parse_pot(self, location):
29 if (os.path.exists(location)):
30 file = open(location, "r", encoding="utf8")
31 lang = "en"
32 for line in file:
33 if line.startswith("\"Language: "):
34 lang = line[len("\"Language: "):]
35 lang = lang.replace('\\n\"\n', "")
36 file.close()
37 file = open(location, "r", encoding="utf8")
38 multiLine = ""
39 key = None
40 entry = {}
41
42 def addEntryToTranslationDict(key, entry, lang):
43 if len(entry.keys())>0:
44 if key is None:
45 key = ""
46 if self.key_xmlkey_xml:
47 text = entry.get("text", "")
48 text = re.sub(r"<.*?>", " ", text)
49 key += str(re.sub(r"\s+", " ", text)).strip()
50 else:
51 key += entry.get("text", None)
52 if key is not None:
53 if len(key)>0:
54 dummyDict = {}
55 dummyDict = self.translationDict.get(key, dummyDict)
56 dummyDict[lang] = entry
57 self.translationDict[key] = dummyDict
58
59 for line in file:
60 if line.isspace() or len(line)<1:
61 addEntryToTranslationDict(key, entry, lang)
62 entry = {}
63 key = None
64 multiLine = ""
65 if line.startswith("msgid "):
66 string = line[len("msgid \""):]
67 string = string[:-len("\"\n")]
68 string = string.replace("\\\"", "\"")
69 string = string.replace("\\\'", "\'")
70 string = string.replace("\\#", "#")
71 entry["text"] = string
72 multiLine = "text"
73 if line.startswith("msgstr "):
74 string = line[len("msgstr \""):]
75 string = string[:-len("\"\n")]
76 string = string.replace("\\\"", "\"")
77 string = string.replace("\\\'", "\'")
78 string = string.replace("\\#", "#")
79 entry["trans"] = string
80 multiLine = "trans"
81 if line.startswith("# "):
82 #Translator comment
83 if "translComment" in entry.keys():
84 entry["translComment"] += line.replace("# ", "")
85 else:
86 entry["translComment"] = line.replace("# ", "")
87 if line.startswith("#. "):
88 entry["extract"] = line.replace("#. ", "")
89 if line.startswith("msgctxt "):
90 key = line[len("msgctxt \""):]
91 key = key[:-len("\"\n")]
92 key += " "
93 if line.startswith("\"") and len(multiLine)>0:
94 string = line[len("\""):]
95 string = string[:-len("\"\n")]
96 string = string.replace("\\\"", "\"")
97 string = string.replace("\\\'", "\'")
98 string = string.replace("\\#", "#")
99 entry[multiLine] += string
100 # ensure that the final entry gets added.
101 addEntryToTranslationDict(key, entry, lang)
102 if lang not in self.translationList:
103 self.translationList.append(lang)
104 file.close()
105
107 return self.translationList
108
109 def get_entry_for_key(self, key, lang):
110 entry = {}
111 entry["trans"] = " "
112 if self.key_xmlkey_xml:
113 key = re.sub(r"<.*?>", " ", key)
114 key = re.sub(r"\s+", " ", key)
115 key = key.strip()
116 if key in self.translationDict.keys():
117 translations = {}
118 translations = self.translationDict[key]
119 if lang not in translations.keys():
120 print("language missing")
121 return entry
122 return translations[lang]
123 else:
124 print(str(key).encode("utf8"))
125 print("translation missing from the translated strings")
126 return entry
VertexDescriptor get(PredecessorMap const &m, VertexDescriptor v)