import os import json root_dir = os.path.dirname(os.path.abspath(__file__)) translate_dir = os.path.join(root_dir, 'translate') def load_translation(filename): with open(os.path.join(translate_dir, filename), 'r', encoding='utf-8') as f: return json.load(f) star_texts = load_translation('StarText.json') desc_texts = load_translation('DescText.json') other_texts = load_translation('OtherText.json') block_texts = load_translation('BlockText.json') import hashlib def text_to_id(text): return hashlib.md5(text.encode('utf-8')).hexdigest() def is_separator(line): return line.startswith('@') or line.startswith('>') or line.startswith('.') or line.startswith('END') def need_real_newline(file_path): rel_path = os.path.relpath(file_path, root_dir).replace('\\', '/') return rel_path == 'Main.txt' or \ (rel_path.startswith('Rooms/') and os.path.basename(file_path) != 'Intro.txt') or \ rel_path.startswith('Shops/') SPECIAL_PREFIXES = [ "name:", "desc:", "menuDesc:", "shopDesc:", "lwEquip:", "equipEffect:", "descriptionDW:" ] for root, dirs, files in os.walk(root_dir): for file in files: if file.endswith('.txt'): file_path = os.path.join(root, file) rel_path = os.path.relpath(file_path, root_dir) mode = 'block' if ( rel_path == 'Main.txt' or rel_path.startswith(('Rooms/', 'Rooms\\', 'Shops/', 'Shops\\')) and not (rel_path.startswith('Rooms') and os.path.basename(file) == 'Intro.txt') ) else 'normal' with open(file_path, 'r', encoding='utf-8') as f: lines = f.readlines() new_lines = [] real_newline = need_real_newline(file_path) if mode == 'block': block = [] for line in lines: line = line.rstrip('\n') if is_separator(line): if block: text = '\n'.join(block) id_ = text_to_id(text) restored = block_texts.get(id_, text) if real_newline: restored = restored.replace('/n', '\n') new_lines.extend(restored.split('\n')) block = [] new_lines.append(line) else: block.append(line) if block: text = '\n'.join(block) id_ = text_to_id(text) restored = block_texts.get(id_, text) if real_newline: restored = restored.replace('/n', '\n') new_lines.extend(restored.split('\n')) else: i = 0 while i < len(lines): line = lines[i].rstrip('\n') if is_separator(line): new_lines.append(line) i += 1 continue if line.startswith('*'): id_ = text_to_id(line) restored = star_texts.get(id_, line) restored = restored.replace('/n', '\n' if real_newline else '/n') new_lines.append(restored) elif any(line.startswith(prefix) for prefix in SPECIAL_PREFIXES): if ':' in line: prefix_part, content_part = line.split(':', 1) original_content = content_part.strip() id_ = text_to_id(original_content) restored_content = desc_texts.get(id_, original_content) if real_newline: restored_content = restored_content.replace('/n', '\n') new_lines.append(f"{prefix_part}: {restored_content}") elif line.startswith('party:'): new_lines.append('party:') i += 1 while i < len(lines): subline = lines[i].rstrip('\n') if subline.startswith('|'): id_ = text_to_id(subline) restored = desc_texts.get(id_, subline) restored = restored.replace('/n', '\n' if real_newline else '/n') new_lines.append(restored) i += 1 else: break else: id_ = text_to_id(line) restored = other_texts.get(id_, line) restored = restored.replace('/n', '\n' if real_newline else '/n') new_lines.append(restored) i += 1 with open(file_path, 'w', encoding='utf-8') as f: f.write('\n'.join(new_lines)) # 註釋: # 導入之前,先把Choices中的\n替換成/n,DescTexts中的\n替換成\\n,StarText中的\n替換成\\n、\\t替換成\t。