Skip to content

Commit 935d2b1

Browse files
authored
add automatic translation progression tracker (#181)
1 parent bbb9efe commit 935d2b1

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

.github/workflows/main.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,20 @@ jobs:
3333
path: 'sqf.log'
3434
- name: Validate SQFLinter Logs
3535
run: python3 tools/sqf_linter_LogChecker.py
36+
37+
stringtables:
38+
runs-on: ubuntu-latest
39+
steps:
40+
- name: Install Python packages
41+
run: |
42+
pip3 install wheel
43+
pip3 install setuptools
44+
pip3 install pygithub
45+
pip3 install pygithub3
46+
- name: Checkout the source code
47+
uses: actions/checkout@master
48+
- name: Update Translation issue
49+
if: github.repository == 'diwako/diwako_dui' && github.ref == 'refs/heads/master' && ! contains(github.event.head_commit.message, '[ci skip]')
50+
env:
51+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52+
run: python3 tools/stringtableDeploy.py

tools/stringtableDeploy.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
3+
###################################
4+
# ZEN automatic deployment script #
5+
# =============================== #
6+
# This is not meant to be run #
7+
# directly! #
8+
###################################
9+
10+
import os
11+
import sys
12+
import shutil
13+
import traceback
14+
import subprocess as sp
15+
from github import Github
16+
17+
TRANSLATIONISSUE = 92
18+
TRANSLATIONBODY = """**[Translation Guide](http://ace3mod.com/wiki/development/how-to-translate-ace3.html)**
19+
{}
20+
"""
21+
22+
REPOUSER = "diwako"
23+
REPONAME = "diwako_dui"
24+
REPOPATH = "{}/{}".format(REPOUSER,REPONAME)
25+
26+
27+
def update_translations(repo):
28+
diag = sp.check_output(["python3", "tools/stringtablediag.py", "--markdown"])
29+
diag = str(diag, "utf-8")
30+
issue = repo.get_issue(TRANSLATIONISSUE)
31+
issue.edit(body=TRANSLATIONBODY.format(diag))
32+
33+
34+
def main():
35+
print("Obtaining token ...")
36+
try:
37+
token = os.environ["GH_TOKEN"]
38+
repo = Github(token).get_repo(REPOPATH)
39+
except:
40+
print("Could not obtain token.")
41+
print(traceback.format_exc())
42+
return 1
43+
else:
44+
print("Token sucessfully obtained.")
45+
46+
print("\nUpdating translation issue ...")
47+
try:
48+
update_translations(repo)
49+
except:
50+
print("Failed to update translation issue.")
51+
print(traceback.format_exc())
52+
return 1
53+
else:
54+
print("Translation issue successfully updated.")
55+
56+
return 0
57+
58+
59+
if __name__ == "__main__":
60+
sys.exit(main())

tools/stringtablediag.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)