222
This commit is contained in:
parent
2e53c9d9ec
commit
cb57d4590c
4636
octl/room/en_US.json
4636
octl/room/en_US.json
File diff suppressed because it is too large
Load Diff
@ -2,29 +2,44 @@ import json
|
||||
import polib
|
||||
|
||||
|
||||
def extract_and_deduplicate(data):
|
||||
def flatten_and_deduplicate(values, seen):
|
||||
"""
|
||||
递归展开列表并去重
|
||||
"""
|
||||
flattened = []
|
||||
for value in values:
|
||||
if isinstance(value, list):
|
||||
flattened.extend(flatten_and_deduplicate(value, seen))
|
||||
else:
|
||||
value = value.strip()
|
||||
if value and value not in seen:
|
||||
seen.add(value)
|
||||
flattened.append(value)
|
||||
return flattened
|
||||
|
||||
|
||||
def extract_and_deduplicate(source, target):
|
||||
"""
|
||||
遍历 JSON 数据,去重并生成新的 JSON 格式
|
||||
"""
|
||||
with open(source, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
|
||||
new_json = {}
|
||||
index = 0
|
||||
|
||||
for key, values in data.items():
|
||||
if not isinstance(values, list):
|
||||
continue # 只处理列表类型的值
|
||||
|
||||
seen = set()
|
||||
count = 0
|
||||
flattened_values = flatten_and_deduplicate(values, seen)
|
||||
|
||||
for value in values:
|
||||
value = value.strip()
|
||||
if value and value not in seen:
|
||||
new_key = f"{index}-{key}-{count}"
|
||||
new_json[new_key] = value
|
||||
seen.add(value)
|
||||
count += 1
|
||||
for count, value in enumerate(flattened_values):
|
||||
new_key = f"{key}-{count}"
|
||||
new_json[new_key] = value
|
||||
|
||||
if len(values) > 0:
|
||||
index += 1
|
||||
|
||||
return new_json
|
||||
with open(target, "w", encoding="utf-8") as f:
|
||||
json.dump(new_json, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
def json_to_new_json(input_file, output_file):
|
||||
@ -56,18 +71,23 @@ def load_translations(po_file):
|
||||
return translations
|
||||
|
||||
|
||||
def translate_json(input_json, po_file, output_json):
|
||||
def translate_json(input_json, translation_old_json, output_json):
|
||||
"""
|
||||
读取 JSON 文件,匹配翻译并生成新 JSON 文件
|
||||
"""
|
||||
with open(input_json, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
data_source = json.load(f)
|
||||
with open(translation_old_json, "r", encoding="utf-8") as f:
|
||||
data_old = json.load(f)
|
||||
|
||||
translations = load_translations(po_file)
|
||||
|
||||
new_data = {key: translations.get(value, "") for key, value in data.items()}
|
||||
new_data = {key: data_old.get(key, "") for key, value in data_source.items()}
|
||||
|
||||
with open(output_json, "w", encoding="utf-8") as f:
|
||||
json.dump(new_data, f, ensure_ascii=False, indent=4)
|
||||
|
||||
print(f"翻译后的 JSON 文件已保存至: {output_json}")
|
||||
|
||||
|
||||
extract_and_deduplicate("original.json", "ru_RU.json")
|
||||
translate_json("ru_RU.json", "en_US.json", "en_US.json")
|
||||
translate_json("ru_RU.json", "zh_CN.json", "zh_CN.json")
|
||||
|
File diff suppressed because it is too large
Load Diff
4632
octl/room/ru_RU.json
4632
octl/room/ru_RU.json
File diff suppressed because it is too large
Load Diff
4636
octl/room/zh_CN.json
4636
octl/room/zh_CN.json
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user