|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +from xml.dom import minidom |
| 7 | + |
| 8 | +# STRINGTABLE DIAG TOOL |
| 9 | +# Author: KoffeinFlummi |
| 10 | +# --------------------- |
| 11 | +# Checks for missing translations and all that jazz. |
| 12 | + |
| 13 | +def get_all_languages(projectpath): |
| 14 | + """ Checks what languages exist in the repo. """ |
| 15 | + languages = [] |
| 16 | + |
| 17 | + for module in os.listdir(projectpath): |
| 18 | + if module[0] == ".": |
| 19 | + continue |
| 20 | + |
| 21 | + stringtablepath = os.path.join(projectpath, module, "stringtable.xml") |
| 22 | + try: |
| 23 | + xmldoc = minidom.parse(stringtablepath) |
| 24 | + except: |
| 25 | + continue |
| 26 | + |
| 27 | + keys = xmldoc.getElementsByTagName("Key") |
| 28 | + for key in keys: |
| 29 | + for child in key.childNodes: |
| 30 | + try: |
| 31 | + if not child.tagName in languages: |
| 32 | + languages.append(child.tagName) |
| 33 | + except: |
| 34 | + continue |
| 35 | + |
| 36 | + return languages |
| 37 | + |
| 38 | +def check_module(projectpath, module, languages): |
| 39 | + """ Checks the given module for all the different languages. """ |
| 40 | + localized = [] |
| 41 | + |
| 42 | + stringtablepath = os.path.join(projectpath, module, "stringtable.xml") |
| 43 | + try: |
| 44 | + xmldoc = minidom.parse(stringtablepath) |
| 45 | + except: |
| 46 | + return 0, localized |
| 47 | + |
| 48 | + keynumber = len(xmldoc.getElementsByTagName("Key")) |
| 49 | + |
| 50 | + for language in languages: |
| 51 | + localized.append(len(xmldoc.getElementsByTagName(language))) |
| 52 | + |
| 53 | + return keynumber, localized |
| 54 | + |
| 55 | +def main(): |
| 56 | + scriptpath = os.path.realpath(__file__) |
| 57 | + projectpath = os.path.dirname(os.path.dirname(scriptpath)) |
| 58 | + projectpath = os.path.join(projectpath, "addons") |
| 59 | + |
| 60 | + if "--markdown" not in sys.argv: |
| 61 | + print("#########################") |
| 62 | + print("# Stringtable Diag Tool #") |
| 63 | + print("#########################") |
| 64 | + |
| 65 | + languages = get_all_languages(projectpath) |
| 66 | + |
| 67 | + if "--markdown" not in sys.argv: |
| 68 | + print("\nLanguages present in the repo:") |
| 69 | + print(", ".join(languages)) |
| 70 | + |
| 71 | + keysum = 0 |
| 72 | + localizedsum = list(map(lambda x: 0, languages)) |
| 73 | + missing = list(map(lambda x: [], languages)) |
| 74 | + |
| 75 | + for module in os.listdir(projectpath): |
| 76 | + keynumber, localized = check_module(projectpath, module, languages) |
| 77 | + |
| 78 | + if keynumber == 0: |
| 79 | + continue |
| 80 | + |
| 81 | + if "--markdown" not in sys.argv: |
| 82 | + print("\n# " + module) |
| 83 | + |
| 84 | + keysum += keynumber |
| 85 | + for i in range(len(localized)): |
| 86 | + if "--markdown" not in sys.argv: |
| 87 | + print(" %s %s / %i" % ((languages[i]+":").ljust(10), str(localized[i]).ljust(3), keynumber)) |
| 88 | + localizedsum[i] += localized[i] |
| 89 | + if localized[i] < keynumber: |
| 90 | + missing[i].append(module) |
| 91 | + |
| 92 | + if "--markdown" not in sys.argv: |
| 93 | + print("\n###########") |
| 94 | + print("# RESULTS #") |
| 95 | + print("###########") |
| 96 | + print("\nTotal number of keys: %i\n" % (keysum)) |
| 97 | + |
| 98 | + for i in range(len(languages)): |
| 99 | + if localizedsum[i] == keysum: |
| 100 | + print("%s No missing stringtable entries." % ((languages[i] + ":").ljust(12))) |
| 101 | + else: |
| 102 | + print("%s %s missing stringtable entry/entries." % ((languages[i] + ":").ljust(12), str(keysum - localizedsum[i]).rjust(4)), end="") |
| 103 | + print(" ("+", ".join(missing[i])+")") |
| 104 | + |
| 105 | + print("\n\n### MARKDOWN ###\n") |
| 106 | + |
| 107 | + print("Total number of keys: %i\n" % (keysum)) |
| 108 | + |
| 109 | + print("| Language | Missing Entries | Relevant Modules | % done |") |
| 110 | + print("|----------|----------------:|------------------|--------|") |
| 111 | + |
| 112 | + for i, language in enumerate(languages): |
| 113 | + if localizedsum[i] == keysum: |
| 114 | + print("| {} | 0 | - | 100% |".format(language)) |
| 115 | + else: |
| 116 | + print("| {} | {} | {} | {}% |".format( |
| 117 | + language, |
| 118 | + keysum - localizedsum[i], |
| 119 | + ", ".join(missing[i]), |
| 120 | + round(100 * localizedsum[i] / keysum))) |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + main() |
0 commit comments