Skip to content

Commit 72e513e

Browse files
committed
Add: Script to check character coverage against OpenTTD translations
Also adds running this script to the build process; assumes the OpenTTD source exists in `../openttd`.
1 parent a1c5d12 commit 72e513e

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ cd ..
1515
cd openttd-mono
1616
bash build.sh
1717
cd ..
18+
19+
python3 checkOpenTTDStrings.py ../openttd/src/lang

checkOpenTTDStrings.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/python3
2+
3+
# Checks characters used in OpenTTD strings against characters present in these fonts
4+
5+
# Usage:
6+
# python3 checkTranslationChars.py path/to/openttd/src/lang
7+
# python3 path/to/checkTranslationChars.py [run in openttd/src/lang]
8+
9+
import glob, os, sys
10+
from fontTools.ttLib import TTFont
11+
12+
if len(sys.argv) > 1:
13+
langpath = sys.argv[1]
14+
else:
15+
langpath = "."
16+
17+
# maximum number of missing characters to list in full
18+
maxlist = 100
19+
20+
# find all ttfs by suffix in directory
21+
suffix = ".ttf"
22+
scriptpath = os.path.dirname(os.path.realpath(__file__))
23+
files = [
24+
os.path.join(scriptpath, "openttd-sans", "OpenTTD-Sans.ttf"),
25+
os.path.join(scriptpath, "openttd-serif", "OpenTTD-Serif.ttf"),
26+
os.path.join(scriptpath, "openttd-mono", "OpenTTD-Mono.ttf"),
27+
os.path.join(scriptpath, "openttd-small", "OpenTTD-Small.ttf"),
28+
os.path.join(scriptpath, "openttd-small", "OpenTTD-SmallCaps.ttf")
29+
]
30+
fonts = []
31+
for file in files:
32+
# for each font, record name and list of character codes available
33+
font = TTFont(file)
34+
fonts.append({
35+
"name": os.path.basename(file[:-len(suffix)]),
36+
"codes": list(font.getBestCmap().keys())
37+
})
38+
39+
# find all lang files by suffix in directory
40+
suffix = ".txt"
41+
files = glob.glob(os.path.join(langpath, "*" + suffix))
42+
print(os.path.join(langpath, "*" + suffix))
43+
for file in files:
44+
# for each language
45+
lang = os.path.basename(file[:-len(suffix)])
46+
with open(file, "r") as handle:
47+
# read lines and remove empty lines
48+
strings = [x for x in handle.read().splitlines() if x]
49+
# extract translated strings
50+
strings = [x[x.find(":") + 1:] for x in strings if x[0] != "#"]
51+
# list all character codes and characters across strings
52+
codes = sorted(list(set([ord(x) for x in "".join(strings)])))
53+
chars = [chr(x) for x in codes]
54+
# check if fully supported by each font
55+
print(lang)
56+
completecount = 0
57+
for font in fonts:
58+
missing = [x for x in codes if x not in font["codes"]]
59+
if len(missing) == 0:
60+
completecount += 1
61+
else:
62+
if len(missing) <= maxlist:
63+
print("", font["name"], "missing "+str(len(missing))+":", "("+"".join([chr(x) for x in missing])+")")
64+
else:
65+
print("", font["name"], "missing "+str(len(missing))+":", "[too many to list]")
66+
if completecount == len(fonts):
67+
print("", "[complete]")

0 commit comments

Comments
 (0)