Path: blob/master/scripts/generate_fullscreen_ui_translation_strings.py
7437 views
import code1import sys2import os3import glob4import re56START_IDENT = "// TRANSLATION-STRING-AREA-BEGIN"7END_IDENT = "// TRANSLATION-STRING-AREA-END"8SRC_FILES = ["src/core/fullscreenui.cpp",9"src/core/fullscreenui.h",10"src/core/fullscreenui_achievements.cpp",11"src/core/fullscreenui_game_list.cpp",12"src/core/fullscreenui_private.h",13"src/core/fullscreenui_settings.cpp",14"src/core/fullscreenui_widgets.cpp",15"src/core/fullscreenui_widgets.h"]16DST_FILE = "src/core/fullscreenui_strings.h"1718full_source = ""19for src_file in SRC_FILES:20path = os.path.join(os.path.dirname(__file__), "..", src_file)21with open(path, "r") as f:22full_source += f.read()23full_source += "\n"2425strings = set()26for token in ["FSUI_STR", "FSUI_CSTR", "FSUI_FSTR", "FSUI_NSTR", "FSUI_VSTR", "FSUI_ICONSTR", "FSUI_ICONVSTR", "FSUI_ICONCSTR"]:27token_len = len(token)28last_pos = 029while True:30last_pos = full_source.find(token, last_pos)31if last_pos < 0:32break3334if last_pos >= 8 and full_source[last_pos - 8:last_pos] == "#define ":35last_pos += len(token)36continue3738if full_source[last_pos + token_len] == '(':39start_pos = last_pos + token_len + 140end_pos = full_source.find("\")", start_pos)41s = full_source[start_pos:end_pos+1]4243# remove "44pos = s.find('"')45new_s = ""46while pos >= 0:47if pos == 0 or s[pos - 1] != '\\':48epos = pos49while True:50epos = s.find('"', epos + 1)51assert epos > pos52if s[epos - 1] == '\\':53continue54else:55break5657assert epos > pos58new_s += s[pos+1:epos]59pos = s.find('"', epos + 1)60else:61pos = s.find('"', pos + 1)62assert len(new_s) > 06364assert (end_pos - start_pos) < 30065strings.add(new_s)66last_pos += len(token)6768print(f"Found {len(strings)} unique strings.")6970full_source = ""71dst_path = os.path.join(os.path.dirname(__file__), "..", DST_FILE)72with open(dst_path, "r") as f:73full_source = f.read()74start = full_source.find(START_IDENT)75end = full_source.find(END_IDENT)76assert start >= 0 and end > start7778new_area = ""79for string in sorted(list(strings)):80new_area += f"TRANSLATE_NOOP(\"FullscreenUI\", \"{string}\");\n"8182full_source = full_source[:start+len(START_IDENT)+1] + new_area + full_source[end:]83with open(dst_path, "w") as f:84f.write(full_source)858687