MCPcopy Create free account
hub / github.com/comaps/comaps / PoParser

Class PoParser

tools/python/po_parser.py:23–124  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21#msgid
22#msgstr
23class PoParser:
24 def __init__(self):
25 args = self.parse_args()
26 self.folder_path = args.folder
27 self.all_po_files = self.find_all_po_files()
28
29 if (args.strings_txt):
30 self.dest_file = StringsTxt(args.strings_txt)
31 elif (args.categories_txt):
32 self.dest_file = CategoriesTxt(args.categories_txt)
33 else:
34 raise RuntimeError("You must specify either -s or -c")
35
36
37 def find_all_po_files(self):
38 return [
39 f for f in listdir(self.folder_path)
40 if isfile(join(self.folder_path, f)) and f.endswith(".po")
41 ]
42
43
44 def parse_files(self):
45 for po_file in self.all_po_files:
46 self._parse_one_file(
47 join(self.folder_path, po_file),
48 self.lang_from_filename(po_file)
49 )
50
51
52 def lang_from_filename(self, filename):
53 # file names are in this format: strings_ru_RU.po
54 lang = filename[len("strings_"):-len(".po")]
55 if lang in TRANSFORMATION_TABLE:
56 return TRANSFORMATION_TABLE[lang]
57 return lang[:2]
58
59
60 def _parse_one_file(self, filepath, lang):
61 self.translations = defaultdict(str)
62 current_key = None
63 string_started = False
64 with open(filepath) as infile:
65 for line in infile:
66 if line.startswith("msgid"):
67 current_key = self.clean_line(line,"msgid")
68 elif line.startswith("msgstr"):
69 if not current_key:
70 continue
71 translation = self.clean_line(line, "msgstr")
72 if not translation:
73 print("No translation for key {} in file {}".format(current_key, filepath))
74 continue
75 self.dest_file.add_translation(
76 translation,
77 key="[{}]".format(current_key),
78 lang=lang
79 )
80 string_started = True

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected