Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified to be able to recursively parse Front Matter #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions auto-translater.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,25 @@ def translate_text(text, lang, type):

# Front Matter 处理规则
def translate_front_matter(front_matter, lang):
translated_front_matter = {}
for key, value in front_matter.items():
if key in front_matter_translation_rules:
processed_value = front_matter_translation_rules[key](value, lang)
def recursive_translate(value, lang):
if isinstance(value, dict):
translated_dict = {}
for k, v in value.items():
if k in front_matter_translation_rules:
processed_value = front_matter_translation_rules[k](v, lang)
else:
processed_value = recursive_translate(v, lang)
translated_dict[k] = processed_value
return translated_dict
elif isinstance(value, list): #处理数组
translated_list = []
for item in value:
translated_list.append(recursive_translate(item, lang))
return translated_list
else:
# 如果在规则列表内,则不做任何翻译或替换操作
processed_value = value
translated_front_matter[key] = processed_value
# print(key, ":", processed_value)
return translated_front_matter
return value

return recursive_translate(front_matter, lang)

# 定义文章拆分函数
def split_text(text, max_length):
Expand Down