DRHPS/extract-strings.py
2025-03-04 17:07:46 +08:00

57 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import json
# 递归读取文件夹tmpgui中的所有JSON文件
def read_json_files(directory):
translation_dict = {} # 用来存储所有的m_text值
translation_set = set()
# 遍历目录中的所有文件和子目录
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".json"):
file_path = os.path.join(root, file)
# 读取每个json文件
with open(file_path, "r", encoding="utf-8") as f:
try:
data = json.load(f)
m_text_value = data.get("m_text") # 获取m_text属性的值
# 如果m_text存在追加到translation_dict中
if m_text_value:
if m_text_value not in translation_set:
translation_dict[
file.replace(".json", "").replace(
"TextMeshProUGUI-", ""
)
] = m_text_value
translation_set.add(m_text_value)
except json.JSONDecodeError:
print(f"文件 {file_path} 解析错误")
return translation_dict
# 将字典保存到transdict.json
def save_translation_dict(translation_dict, output_file):
with open(output_file, "w", encoding="utf-8") as f:
json.dump(translation_dict, f, ensure_ascii=False, indent=4)
# 主程序
def main():
directory = "tmpgui" # tmpgui目录
output_file = "transdict.json" # 输出文件名
# 获取所有m_text值并生成字典
translation_dict = read_json_files(directory)
# 将字典保存为json文件
save_translation_dict(translation_dict, output_file)
print(f"翻译字典已保存为 {output_file}")
if __name__ == "__main__":
main()