from os import listdir import re def is_menu_string(s): # Matches strings that look like "translate * strings:" for the menus pattern = r'translate\s+\w+\s+strings:' return re.match(pattern, s) def parse(lines,cont): menu_line = False temp_cont = [] for line in lines: if menu_line or is_menu_string(line): menu_line = True if len(temp_cont) != 0: cont.append(temp_cont) temp_cont = [] # We reached the menu, everything below this point doesn't need change menu_block.append(line) elif line.startswith("translate") and not is_menu_string(line): # We've reached a new dialogue block so we "save" the old one if len(temp_cont) != 0: cont.append(temp_cont) temp_cont = [] temp_cont.append(line) else: temp_cont.append(line) for file in listdir(f"files_old"): dialogue_blocks_old = [] dialogue_blocks_new = [] menu_block = [] out_lines = [] linesNew = [] linesOld = [] if file.endswith(".rpy"): with open(f"files_old/{file}",'r', encoding="utf8") as f: linesOld = f.readlines() with open(f"files_new/{file}",'r', encoding="utf8") as f2: linesNew = f2.readlines() parse(linesNew,dialogue_blocks_new) parse(linesOld,dialogue_blocks_old) print(menu_block) if len(dialogue_blocks_old) != len(dialogue_blocks_new): print(f"ERROR processing {file}.\nThere's a mismatch in the dialogue blocks.\nCopy paste the missing dialogue blocks into your old translation file to properly merge them\n{len(dialogue_blocks_new)}/{len(dialogue_blocks_old)}") else: for num1,block in enumerate(dialogue_blocks_old): for num2,line in enumerate(block): if num2 == 0: out_lines.append(dialogue_blocks_new[num1][0]) else: out_lines.append(line) out_lines = out_lines + menu_block with open(f"files_out/{file}",'w', encoding="utf8") as fo: for line in out_lines: fo.write(line) ############################## """ Translation blocks follow this structure #comment translate lang id: #comment "Dialogue" But we cannot use comments or line numbers to determine what a block is as a translator may add internal translation notes using comments, not to mention that a translation block can have more than one dialogue line. Menus or interpolated strings (Such as screens) on the other hand look like this translate lang strings: # comment old "Untranslated text" new "Translated text" Since these don't use IDs we don't care to "update" them and just pass on the old menus instead """ ##############################