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