generate_fullscreen_ui_translation_strings.py (2139B)
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 10 src_file = os.path.join(os.path.dirname(__file__), "..", "src", "core", "fullscreen_ui.cpp") 11 12 with open(src_file, "r") as f: 13 full_source = f.read() 14 15 strings = set() 16 for token in ["FSUI_STR", "FSUI_CSTR", "FSUI_FSTR", "FSUI_NSTR", "FSUI_ICONSTR", "FSUI_VSTR"]: 17 token_len = len(token) 18 last_pos = 0 19 while True: 20 last_pos = full_source.find(token, last_pos) 21 if last_pos < 0: 22 break 23 24 if last_pos >= 8 and full_source[last_pos - 8:last_pos] == "#define ": 25 last_pos += len(token) 26 continue 27 28 if full_source[last_pos + token_len] == '(': 29 start_pos = last_pos + token_len + 1 30 end_pos = full_source.find("\")", start_pos) 31 s = full_source[start_pos:end_pos+1] 32 33 # remove " 34 pos = s.find('"') 35 new_s = "" 36 while pos >= 0: 37 if pos == 0 or s[pos - 1] != '\\': 38 epos = pos 39 while True: 40 epos = s.find('"', epos + 1) 41 assert epos > pos 42 if s[epos - 1] == '\\': 43 continue 44 else: 45 break 46 47 assert epos > pos 48 new_s += s[pos+1:epos] 49 pos = s.find('"', epos + 1) 50 else: 51 pos = s.find('"', pos + 1) 52 assert len(new_s) > 0 53 54 assert (end_pos - start_pos) < 300 55 strings.add(new_s) 56 last_pos += len(token) 57 58 print(f"Found {len(strings)} unique strings.") 59 60 start = full_source.find(START_IDENT) 61 end = full_source.find(END_IDENT) 62 assert start >= 0 and end > start 63 64 new_area = "" 65 for string in sorted(list(strings)): 66 new_area += f"TRANSLATE_NOOP(\"FullscreenUI\", \"{string}\");\n" 67 68 full_source = full_source[:start+len(START_IDENT)+1] + new_area + full_source[end:] 69 with open(src_file, "w") as f: 70 f.write(full_source)