summaryrefslogtreecommitdiffstats
path: root/scripts/language_helper.py
blob: 2e3df7132df9396c457e33f89002f3d80568e89e (plain) (blame)
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
142
from xml.dom import minidom
import sys
import getopt

# language helper
#
# by Ethan Yonker (Dees_Troy)
#
# This script reads the English and supplied other language files and
# compares the 2 and reorders and rewrites the other language to a new
# XML file such that all the strings are placed in the same order as
# the English file. It will place commented out string lines for items
# that are not present in the new file and will not include any strings
# in the new file that are no longer present in the English source.
# There is also a version tag that may be compared if present between
# the English and other language in case a translation string changes.



# this helps us avoid ascii unicode errors when writing the final XML
def toprettyxml(xdoc, encoding):
    #"""Return a pretty-printed XML document in a given encoding."""
    unistr = xdoc.toprettyxml().replace(u'<?xml version="1.0" ?>',
                          u'<?xml version="1.0" encoding="%s"?>' % encoding)
    return unistr.encode(encoding, 'xmlcharrefreplace')

HELP = """
  language_helper.py   -o file.xml    other language to compre to English
                     [ -f file.xml ]  output file (defaults to new.xml)
                       -h             help info
"""

enfile = "../gui/theme/common/languages/en.xml"
otherfile = ""
outfile = "new.xml"

try:
	opts, args = getopt.getopt(sys.argv[1:], "hfo:koz", ["device="])
except getopt.GetoptEror:
	print HELP
	sys.stdout.flush()
	sys.exit(2)

for opt, arg in opts:
	if opt == "-h":
		print HELP
		sys.stdout.flush()
		sys.exit()
	elif opt == "-o":
		otherfile = arg
	elif opt == "-f":
		outfile = arg

if otherfile == "":
	print HELP
	exit()

print "Comparing %s and %s" % (enfile, otherfile)
print ""

# Open English
endoc = minidom.parse(enfile)

# Open other language
otherdoc = minidom.parse(otherfile)
otherstrings = otherdoc.getElementsByTagName('string')

# create minidom-document
doc = minidom.Document()

# language tag
language = doc.createElement('language')
doc.appendChild(language)

# display tag (name of the language that shows in the GUI)
otherlang = ""
otherdisplay = otherdoc.getElementsByTagName('display')
for disnode in otherdisplay:
	if disnode.nodeType == disnode.ELEMENT_NODE:
		language.appendChild(disnode)
		otherlang = disnode.firstChild.data
		print otherlang

# resources
resources = doc.createElement('resources')
language.appendChild(resources)

enres = endoc.getElementsByTagName('resources')
for resnode in enres:
	resc = resnode.childNodes
	for child in resc:
		if child.nodeType == child.ELEMENT_NODE:
			if child.tagName != "string":
				otherres = otherdoc.getElementsByTagName('resources')
				found = False
				for othernode in otherres:
					otherresc = othernode.childNodes
					for otherchild in otherresc:
						if otherchild.nodeType == otherchild.ELEMENT_NODE:
							if otherchild.tagName == child.tagName:
								if otherchild.attributes['name'].value == child.attributes['name'].value:
									found = True
									resources.appendChild(otherchild)
									break
					if found == True:
						break
				if found == False:
					print "Failed to find %s in %s, using what we got from English" % (child.toxml(), otherlang)
					resources.appendChild(child)
			else:
				found = False
				for others in otherstrings:
					if child.attributes['name'].value == others.attributes['name'].value:
						found = True
						enver = "1"
						if child.hasAttribute('version'):
							enver = child.attributes['version'].value
						otherver = "1"
						if others.hasAttribute('version'):
							otherver = others.attributes['version'].value
						if enver != otherver:
							ver_err = "English has version " + enver + " but " + otherlang + " has version " + otherver + " for '" + child.attributes['name'].value + "'"
							print ver_err
							version_comment = doc.createComment(ver_err)
							resources.appendChild(version_comment)
							resources.appendChild(others)
						else:
							resources.appendChild(others)
						break
				if found == False:
					print "'%s' present in English and not in %s" % (child.attributes['name'].value, otherlang)
					notfound_err = "NOT FOUND " + child.toxml()
					notfound_comment = doc.createComment(notfound_err)
					resources.appendChild(notfound_comment)
		elif child.nodeType == child.COMMENT_NODE:
			resources.appendChild(child)

# Done, output the xml to a file
file_handle = open(outfile,"wb")
itspretty = toprettyxml(doc, "utf-8")
file_handle.write(itspretty)
file_handle.close()