101 lines
4.2 KiB
Python
101 lines
4.2 KiB
Python
import os
|
|
import json, shutil
|
|
|
|
|
|
# 递归读取文件夹tmpgui中的所有JSON文件
|
|
def read_json_files(directory, textkeylist):
|
|
translation_dict = {} # 用来存储所有的m_text值
|
|
translation_set = set()
|
|
it = 0
|
|
# 遍历目录中的所有文件和子目录
|
|
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)
|
|
for key in textkeylist:
|
|
|
|
def recursive_search(obj, key):
|
|
nonlocal it
|
|
if isinstance(obj, dict):
|
|
for k, v in obj.items():
|
|
if k == key:
|
|
if isinstance(v, str):
|
|
if v not in translation_set:
|
|
translation_set.add(v)
|
|
translation_dict[
|
|
f"{it}-{file.replace(".json", '')}-{key}"
|
|
] = v
|
|
it += 1
|
|
elif isinstance(v, list):
|
|
for item in v:
|
|
if (
|
|
isinstance(item, str)
|
|
and item not in translation_set
|
|
):
|
|
translation_set.add(item)
|
|
translation_dict[
|
|
f"{it}-{file.replace(".json", '')}-{key}"
|
|
] = item
|
|
it += 1
|
|
else:
|
|
recursive_search(item, key)
|
|
else:
|
|
recursive_search(v, key)
|
|
elif isinstance(obj, list):
|
|
for item in obj:
|
|
recursive_search(item, key)
|
|
|
|
recursive_search(data, key)
|
|
|
|
except json.JSONDecodeError:
|
|
print(f"文件 {file_path} 解析错误")
|
|
return translation_dict
|
|
|
|
|
|
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():
|
|
extra = [
|
|
# NewMainMenu_Hypothetical_ContextMenu.cs
|
|
"<color=red>[ No Bonus Quests ]</color>",
|
|
"<color=green>[ Bonus Quests ]</color>",
|
|
]
|
|
save_translation_dict(
|
|
read_json_files(
|
|
"text",
|
|
[
|
|
"m_text",
|
|
"BIOSText",
|
|
"SettingsElementName",
|
|
"SettingsDescription",
|
|
"ExtraName",
|
|
"CreditNames",
|
|
"CreditDescription",
|
|
# 0304补充
|
|
"GoalHint",
|
|
"HypothesisName",
|
|
"HypothesisTagline",
|
|
"HypothesisDescription",
|
|
"TimeWhenWritten",
|
|
],
|
|
),
|
|
"strings/mtext.json",
|
|
)
|
|
save_translation_dict(
|
|
read_json_files("text", ["Text", "Array"]), "strings/dialogue.json"
|
|
)
|
|
shutil.copy("strings/dialogue.json", "translation-tools/weblate/dialogue/en.json")
|
|
shutil.copy("strings/mtext.json", "translation-tools/weblate/mtext/en.json")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|