blob: 67bd20120cbc30be0f7f05157aef370c71b70955 [file] [log] [blame]
Ethan Yonkerd946aa42016-03-30 13:06:18 -05001from xml.dom import minidom
2import sys
3import getopt
4
5# language helper
6#
7# by Ethan Yonker (Dees_Troy)
8#
9# This script reads the English and supplied other language files and
10# compares the 2 and reorders and rewrites the other language to a new
11# XML file such that all the strings are placed in the same order as
12# the English file. It will place commented out string lines for items
13# that are not present in the new file and will not include any strings
14# in the new file that are no longer present in the English source.
15# There is also a version tag that may be compared if present between
16# the English and other language in case a translation string changes.
17
18
19
20# this helps us avoid ascii unicode errors when writing the final XML
21def toprettyxml(xdoc, encoding):
22 #"""Return a pretty-printed XML document in a given encoding."""
23 unistr = xdoc.toprettyxml().replace(u'<?xml version="1.0" ?>',
24 u'<?xml version="1.0" encoding="%s"?>' % encoding)
25 return unistr.encode(encoding, 'xmlcharrefreplace')
26
27HELP = """
Matt Mowerc0f84e72017-02-18 14:59:59 -060028 language_helper.py -o file.xml other language to compare to English
Ethan Yonkerd946aa42016-03-30 13:06:18 -050029 [ -f file.xml ] output file (defaults to new.xml)
30 -h help info
31"""
32
Matt Mowerc0f84e72017-02-18 14:59:59 -060033enfile = "en.xml"
Ethan Yonkerd946aa42016-03-30 13:06:18 -050034otherfile = ""
35outfile = "new.xml"
36
37try:
38 opts, args = getopt.getopt(sys.argv[1:], "hfo:koz", ["device="])
39except getopt.GetoptEror:
40 print HELP
41 sys.stdout.flush()
42 sys.exit(2)
43
44for opt, arg in opts:
45 if opt == "-h":
46 print HELP
47 sys.stdout.flush()
48 sys.exit()
49 elif opt == "-o":
50 otherfile = arg
51 elif opt == "-f":
52 outfile = arg
53
54if otherfile == "":
55 print HELP
56 exit()
57
58print "Comparing %s and %s" % (enfile, otherfile)
59print ""
60
61# Open English
62endoc = minidom.parse(enfile)
63
64# Open other language
65otherdoc = minidom.parse(otherfile)
66otherstrings = otherdoc.getElementsByTagName('string')
67
68# create minidom-document
69doc = minidom.Document()
70
71# language tag
72language = doc.createElement('language')
73doc.appendChild(language)
74
75# display tag (name of the language that shows in the GUI)
76otherlang = ""
77otherdisplay = otherdoc.getElementsByTagName('display')
78for disnode in otherdisplay:
79 if disnode.nodeType == disnode.ELEMENT_NODE:
80 language.appendChild(disnode)
bigbiff bigbiffceb03202019-04-14 20:35:43 -040081 otherlang = disnode.firstChild.data.encode("utf-8")
Ethan Yonkerd946aa42016-03-30 13:06:18 -050082 print otherlang
83
84# resources
85resources = doc.createElement('resources')
86language.appendChild(resources)
87
88enres = endoc.getElementsByTagName('resources')
89for resnode in enres:
90 resc = resnode.childNodes
91 for child in resc:
92 if child.nodeType == child.ELEMENT_NODE:
93 if child.tagName != "string":
94 otherres = otherdoc.getElementsByTagName('resources')
95 found = False
96 for othernode in otherres:
97 otherresc = othernode.childNodes
98 for otherchild in otherresc:
99 if otherchild.nodeType == otherchild.ELEMENT_NODE:
100 if otherchild.tagName == child.tagName:
101 if otherchild.attributes['name'].value == child.attributes['name'].value:
102 found = True
103 resources.appendChild(otherchild)
104 break
105 if found == True:
106 break
107 if found == False:
108 print "Failed to find %s in %s, using what we got from English" % (child.toxml(), otherlang)
109 resources.appendChild(child)
110 else:
111 found = False
112 for others in otherstrings:
113 if child.attributes['name'].value == others.attributes['name'].value:
114 found = True
115 enver = "1"
116 if child.hasAttribute('version'):
117 enver = child.attributes['version'].value
118 otherver = "1"
119 if others.hasAttribute('version'):
120 otherver = others.attributes['version'].value
121 if enver != otherver:
122 ver_err = "English has version " + enver + " but " + otherlang + " has version " + otherver + " for '" + child.attributes['name'].value + "'"
123 print ver_err
124 version_comment = doc.createComment(ver_err)
125 resources.appendChild(version_comment)
126 resources.appendChild(others)
127 else:
128 resources.appendChild(others)
129 break
130 if found == False:
bigbiff bigbiffceb03202019-04-14 20:35:43 -0400131 print "'%s' present in English and not in %s" % (child.attributes['name'].value.encode("utf-8"), otherlang)
Ian Macdonalda112cb52021-03-01 17:20:12 +0100132 notfound_err = "NOT FOUND " + child.toxml().replace('--', '\-\-')
Ethan Yonkerd946aa42016-03-30 13:06:18 -0500133 notfound_comment = doc.createComment(notfound_err)
134 resources.appendChild(notfound_comment)
135 elif child.nodeType == child.COMMENT_NODE:
136 resources.appendChild(child)
137
138# Done, output the xml to a file
139file_handle = open(outfile,"wb")
140itspretty = toprettyxml(doc, "utf-8")
141file_handle.write(itspretty)
142file_handle.close()