Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/scripts/generate_fullscreen_ui_translation_strings.py
4201 views
1
import code
2
import sys
3
import os
4
import glob
5
import re
6
7
START_IDENT = "// TRANSLATION-STRING-AREA-BEGIN"
8
END_IDENT = "// TRANSLATION-STRING-AREA-END"
9
SRC_FILES = ["src/core/fullscreen_ui.cpp", "src/util/imgui_fullscreen.cpp", "src/util/imgui_fullscreen.h"]
10
DST_FILE = "src/core/fullscreen_ui.cpp"
11
12
full_source = ""
13
for src_file in SRC_FILES:
14
path = os.path.join(os.path.dirname(__file__), "..", src_file)
15
with open(path, "r") as f:
16
full_source += f.read()
17
full_source += "\n"
18
19
strings = set()
20
for token in ["FSUI_STR", "FSUI_CSTR", "FSUI_FSTR", "FSUI_NSTR", "FSUI_VSTR", "FSUI_ICONSTR", "FSUI_ICONVSTR", "FSUI_ICONCSTR"]:
21
token_len = len(token)
22
last_pos = 0
23
while True:
24
last_pos = full_source.find(token, last_pos)
25
if last_pos < 0:
26
break
27
28
if last_pos >= 8 and full_source[last_pos - 8:last_pos] == "#define ":
29
last_pos += len(token)
30
continue
31
32
if full_source[last_pos + token_len] == '(':
33
start_pos = last_pos + token_len + 1
34
end_pos = full_source.find("\")", start_pos)
35
s = full_source[start_pos:end_pos+1]
36
37
# remove "
38
pos = s.find('"')
39
new_s = ""
40
while pos >= 0:
41
if pos == 0 or s[pos - 1] != '\\':
42
epos = pos
43
while True:
44
epos = s.find('"', epos + 1)
45
assert epos > pos
46
if s[epos - 1] == '\\':
47
continue
48
else:
49
break
50
51
assert epos > pos
52
new_s += s[pos+1:epos]
53
pos = s.find('"', epos + 1)
54
else:
55
pos = s.find('"', pos + 1)
56
assert len(new_s) > 0
57
58
assert (end_pos - start_pos) < 300
59
strings.add(new_s)
60
last_pos += len(token)
61
62
print(f"Found {len(strings)} unique strings.")
63
64
full_source = ""
65
dst_path = os.path.join(os.path.dirname(__file__), "..", DST_FILE)
66
with open(dst_path, "r") as f:
67
full_source = f.read()
68
start = full_source.find(START_IDENT)
69
end = full_source.find(END_IDENT)
70
assert start >= 0 and end > start
71
72
new_area = ""
73
for string in sorted(list(strings)):
74
new_area += f"TRANSLATE_NOOP(\"FullscreenUI\", \"{string}\");\n"
75
76
full_source = full_source[:start+len(START_IDENT)+1] + new_area + full_source[end:]
77
with open(dst_path, "w") as f:
78
f.write(full_source)
79
80