File indexing completed on 2025-04-27 04:12:42
0001 """ 0002 SPDX-FileCopyrightText: 2018 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com> 0003 0004 This file is part of the Comics Project Management Tools(CPMT). 0005 0006 SPDX-License-Identifier: GPL-3.0-or-later 0007 """ 0008 0009 """ 0010 A thing that parses through POT files. 0011 """ 0012 0013 import sys 0014 import os 0015 import re 0016 0017 class po_file_parser(): 0018 translationDict = {} 0019 translationList = [] 0020 key_xml = False 0021 0022 def __init__(self, translationLocation, key_xml = False): 0023 self.key_xml = key_xml 0024 if os.path.exists(translationLocation): 0025 for entry in os.scandir(translationLocation): 0026 if entry.name.endswith('.po') and entry.is_file(): 0027 self.parse_pot(os.path.join(translationLocation, entry.name)) 0028 0029 def parse_pot(self, location): 0030 if (os.path.exists(location)): 0031 file = open(location, "r", encoding="utf8") 0032 lang = "en" 0033 for line in file: 0034 if line.startswith("\"Language: "): 0035 lang = line[len("\"Language: "):] 0036 lang = lang.replace('\\n\"\n', "") 0037 file.close() 0038 file = open(location, "r", encoding="utf8") 0039 multiLine = "" 0040 key = None 0041 entry = {} 0042 0043 def addEntryToTranslationDict(key, entry, lang): 0044 if len(entry.keys())>0: 0045 if key is None: 0046 key = "" 0047 if self.key_xml: 0048 text = entry.get("text", "") 0049 text = re.sub("\<.*?\>", " ", text) 0050 key += str(re.sub("\s+", " ", text)).strip() 0051 else: 0052 key += entry.get("text", None) 0053 if key is not None: 0054 if len(key)>0: 0055 dummyDict = {} 0056 dummyDict = self.translationDict.get(key, dummyDict) 0057 dummyDict[lang] = entry 0058 self.translationDict[key] = dummyDict 0059 0060 for line in file: 0061 if line.isspace() or len(line)<1: 0062 addEntryToTranslationDict(key, entry, lang) 0063 entry = {} 0064 key = None 0065 multiLine = "" 0066 if line.startswith("msgid "): 0067 string = line[len("msgid \""):] 0068 string = string[:-len("\"\n")] 0069 string = string.replace("\\\"", "\"") 0070 string = string.replace("\\\'", "\'") 0071 string = string.replace("\\#", "#") 0072 entry["text"] = string 0073 multiLine = "text" 0074 if line.startswith("msgstr "): 0075 string = line[len("msgstr \""):] 0076 string = string[:-len("\"\n")] 0077 string = string.replace("\\\"", "\"") 0078 string = string.replace("\\\'", "\'") 0079 string = string.replace("\\#", "#") 0080 entry["trans"] = string 0081 multiLine = "trans" 0082 if line.startswith("# "): 0083 #Translator comment 0084 if "translComment" in entry.keys(): 0085 entry["translComment"] += line.replace("# ", "") 0086 else: 0087 entry["translComment"] = line.replace("# ", "") 0088 if line.startswith("#. "): 0089 entry["extract"] = line.replace("#. ", "") 0090 if line.startswith("msgctxt "): 0091 key = line[len("msgctxt \""):] 0092 key = key[:-len("\"\n")] 0093 key += " " 0094 if line.startswith("\"") and len(multiLine)>0: 0095 string = line[len("\""):] 0096 string = string[:-len("\"\n")] 0097 string = string.replace("\\\"", "\"") 0098 string = string.replace("\\\'", "\'") 0099 string = string.replace("\\#", "#") 0100 entry[multiLine] += string 0101 # ensure that the final entry gets added. 0102 addEntryToTranslationDict(key, entry, lang) 0103 if lang not in self.translationList: 0104 self.translationList.append(lang) 0105 file.close() 0106 0107 def get_translation_list(self): 0108 return self.translationList 0109 0110 def get_entry_for_key(self, key, lang): 0111 entry = {} 0112 entry["trans"] = " " 0113 if self.key_xml: 0114 key = re.sub("\<.*?\>", " ", key) 0115 key = re.sub("\s+", " ", key) 0116 key = key.strip() 0117 if key in self.translationDict.keys(): 0118 translations = {} 0119 translations = self.translationDict[key] 0120 if lang not in translations.keys(): 0121 print("language missing") 0122 return entry 0123 return translations[lang] 0124 else: 0125 print(str(key).encode("utf8")) 0126 print("translation missing from the translated strings") 0127 return entry