This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlang.py
executable file
·141 lines (119 loc) · 5.06 KB
/
lang.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python3
import argparse
import subprocess
from xml.dom import minidom
import sys
import os
current_dir = os.path.dirname(os.path.realpath(__file__))
# Works only on Fedora (tools are called lupdate-qt5 and lrelease-qt5)
lupdate = "lupdate-qt5"
lrelease = "lrelease-qt5"
def getText(element):
if element.firstChild is None:
return None
text = element.firstChild
if text.nodeType != text.TEXT_NODE:
return None
return text.wholeText
def copy(element, doc):
element_copy = doc.createElement(element.tagName)
for attribute in element.attributes.keys():
element_copy.setAttribute(attribute, element.getAttribute(attribute))
# Copy text
text = getText(element)
if text is None:
return element_copy
text_copy = doc.createTextNode(text)
element_copy.appendChild(text_copy)
return element_copy
def do_push():
# Execute lupdate
subprocess.call([lupdate, "src", "-ts", "%s/src/translations/friends-tmp.ts" % current_dir])
# Open the translation file and do a fake translation
doc = minidom.parse("%s/src/translations/friends-tmp.ts" % current_dir)
ts = doc.documentElement
final_doc = minidom.Document()
imp = minidom.getDOMImplementation('')
dt = imp.createDocumentType('TS', '', '')
final_doc.appendChild(dt)
# Copy TS
final_ts = copy(ts, doc)
final_ts.setAttribute("language", "en")
final_doc.appendChild(final_ts)
# Copy contexts
contexts = ts.getElementsByTagName("context")
for context in contexts:
final_context = copy(context, doc)
final_ts.appendChild(final_context)
# Copy context name
names = context.getElementsByTagName("name")
for name in names:
final_context.appendChild(copy(name, doc))
# Copy messages
messages = context.getElementsByTagName("message")
for message in messages:
final_message = copy(message, doc)
final_context.appendChild(final_message)
# Copy location / source / extra comment
locations = message.getElementsByTagName("location")
for location in locations:
final_message.appendChild(copy(location, doc))
sources = message.getElementsByTagName("source")
for source in sources:
sourceText = getText(source)
final_message.appendChild(copy(source, doc))
extra_comments = message.getElementsByTagName("extracomment")
for extra_comment in extra_comments:
final_message.appendChild(copy(extra_comment, doc))
translation = doc.createElement("translation")
final_message.appendChild(translation)
if message.hasAttribute("numerus"):
# We need to add <numerusform>text</numerusform> twice
for i in range(0, 2):
numerus_form = doc.createElement("numerusform")
text_copy = doc.createTextNode(sourceText)
numerus_form.appendChild(text_copy)
translation.appendChild(numerus_form)
else:
text_copy = doc.createTextNode(sourceText)
translation.appendChild(text_copy)
f = open("%s/src/translations/friends.ts" % current_dir, 'w')
final_doc.writexml(f, encoding="utf-8")
f.close()
subprocess.call(["rm", "%s/src/translations/friends-tmp.ts" % current_dir])
subprocess.call(["tx", "push", "-s"])
def do_pull(perc):
subprocess.call(["rm *.ts"], cwd="%s/src/translations" % current_dir, shell=True)
subprocess.call(["tx", "pull", "-a", "--minimum-perc=%s" % perc])
subprocess.call(["%s -idbased *.ts" % lrelease], cwd="%s/src/translations" % current_dir, shell=True)
subprocess.call(["rm *.ts"], cwd="%s/src/translations" % current_dir, shell=True)
subprocess.call([lupdate, "src", "-ts", "%s/src/translations/friends.ts" % current_dir])
subprocess.call(["%s -idbased friends.ts -qm friends-engineering-english.qm" % lrelease], cwd="%s/src/translations" % current_dir, shell=True)
subprocess.call(["rm *.ts"], cwd="%s/src/translations" % current_dir, shell=True)
parser = argparse.ArgumentParser(description='Friends translation management script')
parser.add_argument('--push', action='store_true', help='Push source translation file')
parser.add_argument('--pull', action='store_true', help='Pull translation file')
parser.add_argument('percentage', metavar='percentage', type=int, nargs='?',
help='Percentage of translation required')
args = parser.parse_args()
push = args.push
pull = args.pull
percentage = args.percentage
if push and pull or (not push and not pull):
parser.print_help()
sys.exit(1)
if percentage is not None and not pull:
parser.print_help()
sys.exit(1)
if percentage is not None:
if percentage < 0 or percentage > 100:
parser.print_help()
sys.exit(1)
else:
percentage = 100
if push:
do_push()
sys.exit(0)
if pull:
do_pull(percentage)
sys.exit(0)