141 lines
5.3 KiB
Python
141 lines
5.3 KiB
Python
import os
|
||
import json
|
||
import hashlib
|
||
|
||
# 获取当前文件所在的目录
|
||
root_dir = os.path.dirname(os.path.abspath(__file__))
|
||
# 定义翻译文件的目录
|
||
translate_dir = os.path.join(root_dir, 'translate')
|
||
|
||
# 生成唯一ID
|
||
def generate_id(text):
|
||
return hashlib.md5(text.encode('utf-8')).hexdigest()
|
||
|
||
# 保存JSON文件
|
||
def save_json_with_ids(filename, text_list):
|
||
# 创建翻译文件目录
|
||
os.makedirs(translate_dir, exist_ok=True)
|
||
# 定义输出文件路径
|
||
output_path = os.path.join(translate_dir, filename)
|
||
# 定义输出内容
|
||
output = {}
|
||
for text in text_list:
|
||
output[generate_id(text)] = text
|
||
# 将内容写入文件
|
||
with open(output_path, 'w', encoding='utf-8') as f:
|
||
json.dump(output, f, ensure_ascii=False, indent=2)
|
||
# 打印保存信息
|
||
print(f"✅ 已儲存:{filename}(共 {len(text_list)} 筆)")
|
||
|
||
# 判断是否为分隔符
|
||
def is_separator(line):
|
||
return line.startswith('@') or line.startswith('>') or line.startswith('.') or line.startswith('END')
|
||
|
||
# 定义不同类型的文本列表
|
||
star_texts = []
|
||
desc_texts = []
|
||
other_texts = []
|
||
block_texts = []
|
||
choices_texts = []
|
||
|
||
# 处理文件
|
||
def process_file(path, mode):
|
||
# 打印处理文件信息
|
||
print(f"📄 正在處理檔案:{path}")
|
||
# 读取文件内容
|
||
with open(path, 'r', encoding='utf-8') as f:
|
||
lines = f.readlines()
|
||
|
||
# 根据模式处理文件
|
||
if mode == 'block':
|
||
block = []
|
||
for line in lines:
|
||
line = line.rstrip('\n')
|
||
# 判断是否为分隔符
|
||
if is_separator(line):
|
||
# 如果有内容,则添加到block_texts中
|
||
if block:
|
||
block_texts.append('\n'.join(block))
|
||
block = []
|
||
# 如果是>prompt:开头,则添加到choices_texts中
|
||
if line.startswith('>prompt:'):
|
||
text = line[len('>prompt:'):].replace(':', '/n')
|
||
if text not in choices_texts:
|
||
choices_texts.append(text)
|
||
else:
|
||
# 否则添加到block中
|
||
block.append(line)
|
||
# 如果有内容,则添加到block_texts中
|
||
if block:
|
||
block_texts.append('\n'.join(block))
|
||
else:
|
||
i = 0
|
||
while i < len(lines):
|
||
line = lines[i].rstrip('\n')
|
||
# 判断是否为分隔符
|
||
if is_separator(line):
|
||
# 如果是>prompt:开头,则添加到choices_texts中
|
||
if line.startswith('>prompt:'):
|
||
text = line[len('>prompt:'):].replace(':', '/n')
|
||
if text not in choices_texts:
|
||
choices_texts.append(text)
|
||
i += 1
|
||
continue
|
||
# 如果是*开头,则添加到star_texts中
|
||
if line.startswith('*'):
|
||
if line not in star_texts:
|
||
star_texts.append(line)
|
||
# 如果是name:、desc:、lwEquip:、shopDesc:、equipEffect:、descriptionDW:、menuDesc:开头,则添加到desc_texts中
|
||
elif (line.startswith('name:') or line.startswith('desc:') or line.startswith('lwEquip:')
|
||
or line.startswith('shopDesc:') or line.startswith('equipEffect:')
|
||
or line.startswith('descriptionDW:') or line.startswith('menuDesc:')):
|
||
content = line.split(':', 1)[1].strip()
|
||
if content not in desc_texts:
|
||
desc_texts.append(content)
|
||
# 如果是party:开头,则添加到desc_texts中
|
||
elif line.startswith('party:'):
|
||
i += 1
|
||
while i < len(lines):
|
||
subline = lines[i].rstrip('\n')
|
||
if subline.startswith('|'):
|
||
if subline not in desc_texts:
|
||
desc_texts.append(subline)
|
||
i += 1
|
||
else:
|
||
break
|
||
continue
|
||
# 否则添加到other_texts中
|
||
else:
|
||
if line not in other_texts:
|
||
other_texts.append(line)
|
||
i += 1
|
||
|
||
# 开始扫描文件夹
|
||
print("🔍 開始掃描資料夾...\n")
|
||
# 遍历文件夹
|
||
for foldername, subfolders, filenames in os.walk(root_dir):
|
||
# 如果是翻译文件夹,则跳过
|
||
if 'translate' in foldername:
|
||
continue
|
||
# 打印扫描文件夹信息
|
||
print(f"📁 掃描資料夾:{foldername}")
|
||
# 遍历文件
|
||
for filename in filenames:
|
||
# 如果是txt文件,则处理
|
||
if filename.endswith('.txt'):
|
||
file_path = os.path.join(foldername, filename)
|
||
relative_path = os.path.relpath(file_path, root_dir)
|
||
# 根据文件路径判断模式
|
||
mode = 'block' if relative_path.startswith(('Main', 'Rooms', 'Shops')) else 'normal'
|
||
process_file(file_path, mode)
|
||
|
||
# 保存翻译JSON文件
|
||
print("\n💾 儲存翻譯 JSON 檔案中...")
|
||
save_json_with_ids('StarText.json', star_texts)
|
||
save_json_with_ids('DescText.json', desc_texts)
|
||
save_json_with_ids('OtherText.json', other_texts)
|
||
save_json_with_ids('BlockText.json', block_texts)
|
||
save_json_with_ids('Choices.json', choices_texts)
|
||
print("\n✨ 作業完成。")
|
||
# 註釋:
|
||
# 導入之後,先把Choices中的/n替換成\n,DescTexts中的\\n替換成\n,StarText中的\\n替換成\n、\t替換成\\t。 |