Ethan Yonker | d946aa4 | 2016-03-30 13:06:18 -0500 | [diff] [blame] | 1 | from xml.dom import minidom |
| 2 | import sys |
| 3 | import 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 |
| 21 | def 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 | |
| 27 | HELP = """ |
| 28 | language_helper.py -o file.xml other language to compre to English |
| 29 | [ -f file.xml ] output file (defaults to new.xml) |
| 30 | -h help info |
| 31 | """ |
| 32 | |
| 33 | enfile = "../gui/theme/common/languages/en.xml" |
| 34 | otherfile = "" |
| 35 | outfile = "new.xml" |
| 36 | |
| 37 | try: |
| 38 | opts, args = getopt.getopt(sys.argv[1:], "hfo:koz", ["device="]) |
| 39 | except getopt.GetoptEror: |
| 40 | print HELP |
| 41 | sys.stdout.flush() |
| 42 | sys.exit(2) |
| 43 | |
| 44 | for 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 | |
| 54 | if otherfile == "": |
| 55 | print HELP |
| 56 | exit() |
| 57 | |
| 58 | print "Comparing %s and %s" % (enfile, otherfile) |
| 59 | print "" |
| 60 | |
| 61 | # Open English |
| 62 | endoc = minidom.parse(enfile) |
| 63 | |
| 64 | # Open other language |
| 65 | otherdoc = minidom.parse(otherfile) |
| 66 | otherstrings = otherdoc.getElementsByTagName('string') |
| 67 | |
| 68 | # create minidom-document |
| 69 | doc = minidom.Document() |
| 70 | |
| 71 | # language tag |
| 72 | language = doc.createElement('language') |
| 73 | doc.appendChild(language) |
| 74 | |
| 75 | # display tag (name of the language that shows in the GUI) |
| 76 | otherlang = "" |
| 77 | otherdisplay = otherdoc.getElementsByTagName('display') |
| 78 | for disnode in otherdisplay: |
| 79 | if disnode.nodeType == disnode.ELEMENT_NODE: |
| 80 | language.appendChild(disnode) |
| 81 | otherlang = disnode.firstChild.data |
| 82 | print otherlang |
| 83 | |
| 84 | # resources |
| 85 | resources = doc.createElement('resources') |
| 86 | language.appendChild(resources) |
| 87 | |
| 88 | enres = endoc.getElementsByTagName('resources') |
| 89 | for 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: |
| 131 | print "'%s' present in English and not in %s" % (child.attributes['name'].value, otherlang) |
| 132 | notfound_err = "NOT FOUND " + child.toxml() |
| 133 | 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 |
| 139 | file_handle = open(outfile,"wb") |
| 140 | itspretty = toprettyxml(doc, "utf-8") |
| 141 | file_handle.write(itspretty) |
| 142 | file_handle.close() |