Path: blob/master/scripts/generate_fullscreen_ui_translation_strings.py
4201 views
import code1import sys2import os3import glob4import re56START_IDENT = "// TRANSLATION-STRING-AREA-BEGIN"7END_IDENT = "// TRANSLATION-STRING-AREA-END"8SRC_FILES = ["src/core/fullscreen_ui.cpp", "src/util/imgui_fullscreen.cpp", "src/util/imgui_fullscreen.h"]9DST_FILE = "src/core/fullscreen_ui.cpp"1011full_source = ""12for src_file in SRC_FILES:13path = os.path.join(os.path.dirname(__file__), "..", src_file)14with open(path, "r") as f:15full_source += f.read()16full_source += "\n"1718strings = set()19for token in ["FSUI_STR", "FSUI_CSTR", "FSUI_FSTR", "FSUI_NSTR", "FSUI_VSTR", "FSUI_ICONSTR", "FSUI_ICONVSTR", "FSUI_ICONCSTR"]:20token_len = len(token)21last_pos = 022while True:23last_pos = full_source.find(token, last_pos)24if last_pos < 0:25break2627if last_pos >= 8 and full_source[last_pos - 8:last_pos] == "#define ":28last_pos += len(token)29continue3031if full_source[last_pos + token_len] == '(':32start_pos = last_pos + token_len + 133end_pos = full_source.find("\")", start_pos)34s = full_source[start_pos:end_pos+1]3536# remove "37pos = s.find('"')38new_s = ""39while pos >= 0:40if pos == 0 or s[pos - 1] != '\\':41epos = pos42while True:43epos = s.find('"', epos + 1)44assert epos > pos45if s[epos - 1] == '\\':46continue47else:48break4950assert epos > pos51new_s += s[pos+1:epos]52pos = s.find('"', epos + 1)53else:54pos = s.find('"', pos + 1)55assert len(new_s) > 05657assert (end_pos - start_pos) < 30058strings.add(new_s)59last_pos += len(token)6061print(f"Found {len(strings)} unique strings.")6263full_source = ""64dst_path = os.path.join(os.path.dirname(__file__), "..", DST_FILE)65with open(dst_path, "r") as f:66full_source = f.read()67start = full_source.find(START_IDENT)68end = full_source.find(END_IDENT)69assert start >= 0 and end > start7071new_area = ""72for string in sorted(list(strings)):73new_area += f"TRANSLATE_NOOP(\"FullscreenUI\", \"{string}\");\n"7475full_source = full_source[:start+len(START_IDENT)+1] + new_area + full_source[end:]76with open(dst_path, "w") as f:77f.write(full_source)787980