-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawio-translate.py
executable file
·101 lines (72 loc) · 2.64 KB
/
drawio-translate.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
#!/opt/local/bin/python
"""
Translate text in drawio docs (in uncompressed xml) to another language using Google Translate.
"""
import codecs
import time
import sys
from xml.dom.minidom import *
from googletrans import Translator
from HTMLParser import HTMLParser
THROTTLE=2
SRC_LANG="en"
DEST_LANG="es"
DEBUG=1
class GoogleTranslate(Translator):
def translate(self, text):
time.sleep(THROTTLE)
return super(GoogleTranslate, self).translate(text, DEST_LANG, SRC_LANG)
translator = GoogleTranslate()
class TranslateHTML(HTMLParser):
translatedText = ""
def handle_data(self, data):
translation = translator.translate(data)
# print "==>", unicode(translation.text)
self.translatedText += translation.text
def handle_starttag(self, tag, attrs):
self.translatedText += self.get_starttag_text()
def handle_endtag(self, tag):
self.translatedText += "</" + tag + ">"
def handle_starttag(self, tag, attrs):
self.translatedText += self.get_starttag_text()
def handle_endtag(self, tag):
self.translatedText += "</" + tag + ">"
# ----------------------------------------------------------------------------
# -- Main
# ----------------------------------------------------------------------------
if (len(sys.argv) < 2):
print "Usage:", sys.argv[0], "src_diag.drawio dest_diag.drawio"
quit()
else:
SRC_DIAG=sys.argv[1]
DEST_DIAG=sys.argv[2]
print "Translating", SRC_DIAG, "from", SRC_LANG, "to", DEST_LANG
doc = parse(SRC_DIAG)
nodes = doc.getElementsByTagName("mxCell")
for node in nodes:
if node.hasAttribute("style"):
style = node.getAttribute("style")
text = node.getAttribute("value")
parser = TranslateHTML()
if (style and "html=1" in style and text):
print "1: ", text
# More parsing necessary to parse text from inner font tag
if ("<font" in text):
parser.feed(text)
try:
# TODO - parse text out of font tags before translating & paste back together later
if parser.translatedText:
translation = parser.translatedText
else:
translation = translator.translate(text).text
print "2: ", translation, "\n"
node.setAttribute("value", translation)
except ValueError as e:
print "^ could not translate", e
doc.writexml (
codecs.open(DEST_DIAG, 'w', encoding='utf-8'),
indent=" ",
addindent=" ",
newl=''
)
print "Translated diagram: " , DEST_DIAG