-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2po.py
executable file
·42 lines (35 loc) · 1.24 KB
/
csv2po.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
import csv
import sys
import ast
import json
reader = csv.DictReader(sys.stdin)
locales = { l: {} for l in reader.fieldnames if l.startswith("locale/") }
for row in reader:
for key, value in row.items():
if not key.startswith("locale/"):
continue
locales[key][row["locale/en_US.po"]] = value
for file, strings in locales.items():
result = ""
with open(file) as f:
for line in f:
if line.startswith("msgid "):
result += line
msgid = ast.literal_eval(line[6:])
elif line.startswith("msgstr "):
if msgid != "" and msgid in strings:
msgstr = strings[msgid].replace("\\", "\\\\")
msgstr = strings[msgid].replace("\"", "\\\"")
result += f"msgstr \"{msgstr}\"\n"
else:
result += line
if msgid != "":
print(f"missing {file} translation for {msgid!r}")
elif line.startswith("\""):
if msgid == "" or msgid not in strings:
result += line
else:
result += line
with open(file, "w") as f:
f.write(result)