Skip to content

Commit dfc7824

Browse files
committed
v1.8.7 - 移除调试日志,优化翻译导入功能
- 支持紧凑JSON格式和模板格式的翻译文件 - 自动检测并解析不同格式的翻译文件 - 修复导入翻译时translation为空导致渲染无文字的问题 - 优化代码,移除冗余的调试日志
1 parent 80dc00b commit dfc7824

1 file changed

Lines changed: 5 additions & 38 deletions

File tree

desktop_qt_ui/services/workflow_service.py

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -845,50 +845,35 @@ def safe_update_large_json_from_text(
845845

846846
# 2. 解析翻译内容
847847
logger.debug("Parsing translations from text content.")
848-
logger.info(f"[DEBUG] TXT文件路径: {text_file_path}")
849-
logger.info(f"[DEBUG] JSON文件路径: {json_file_path}")
850-
logger.info(f"[DEBUG] 模板文件路径: {template_path}")
851848
translations = {}
852849

853850
# 首先尝试直接解析为JSON(支持紧凑格式)
854851
try:
855852
parsed_json = json.loads(text_content)
856853
if isinstance(parsed_json, dict):
857854
translations = parsed_json
858-
logger.info(f"[DEBUG] 直接解析为JSON成功,找到 {len(translations)} 条翻译")
859-
logger.info(f"[DEBUG] 前3条翻译: {list(translations.items())[:3]}")
855+
logger.info(f"直接解析为JSON成功,找到 {len(translations)} 条翻译")
860856
else:
861-
logger.info(f"[DEBUG] JSON解析成功但不是字典格式,尝试模板解析")
862857
raise ValueError("Not a dict")
863-
except (json.JSONDecodeError, ValueError) as e:
864-
logger.info(f"[DEBUG] 直接JSON解析失败: {e},尝试使用模板解析")
858+
except (json.JSONDecodeError, ValueError):
865859
# 如果JSON解析失败,使用原来的模板解析逻辑
866860
# 移除前缀和后缀
867-
logger.info(f"[DEBUG] 原始文本内容长度: {len(text_content)}")
868-
logger.info(f"[DEBUG] Prefix: {repr(prefix[:50] if prefix else '')}")
869-
logger.info(f"[DEBUG] Suffix: {repr(suffix[:50] if suffix else '')}")
870861
if prefix and text_content.startswith(prefix):
871862
text_content = text_content[len(prefix):]
872863
if suffix and text_content.endswith(suffix):
873864
text_content = text_content[:-len(suffix)]
874865

875866
# 分割条目
876-
logger.info(f"[DEBUG] Separator: {repr(separator[:50] if separator else '')}")
877867
if separator:
878868
# 尝试使用separator分割
879869
items = text_content.split(separator)
880870
# 如果只分割出1个item,可能是紧凑格式(没有换行),尝试用逗号分割
881871
if len(items) == 1 and ',' in text_content:
882-
logger.info(f"[DEBUG] Separator分割失败,尝试用逗号分割紧凑格式")
883872
# 使用正则表达式分割:匹配 "key": "value", 的模式
884-
# 注意:这个正则会保留引号
885873
items = re.split(r'",\s*"', text_content)
886-
logger.info(f"[DEBUG] 逗号分割后得到 {len(items)} 个items")
887874
else:
888875
items = [text_content] if text_content.strip() else []
889-
logger.info(f"[DEBUG] Found {len(items)} items in text file.")
890-
if len(items) > 0:
891-
logger.info(f"[DEBUG] First item: {repr(items[0][:100] if items[0] else '')}")
876+
logger.debug(f"Found {len(items)} items in text file.")
892877

893878
# 解析每个条目
894879
parts = re.split(f'({re.escape("<original>")}|{re.escape("<translated>")})', item_template)
@@ -906,47 +891,29 @@ def safe_update_large_json_from_text(
906891

907892
# 添加结尾匹配,确保匹配到字符串末尾
908893
parser_regex_str += "$"
909-
910-
logger.info(f"[DEBUG] Item template: {repr(item_template)}")
911-
logger.info(f"[DEBUG] Parser regex: {repr(parser_regex_str)}")
912894
parser_regex = re.compile(parser_regex_str, re.DOTALL)
913895

914-
matched_count = 0
915-
for idx, item in enumerate(items):
896+
for item in items:
916897
item_stripped = item.strip()
917898
if not item_stripped:
918-
logger.info(f"[DEBUG] Item {idx}: 跳过空条目")
919899
continue
920-
921-
if idx < 3: # 打印前3个item的完整内容
922-
logger.info(f"[DEBUG] Item {idx} 原始内容: {repr(item)}")
923900

924901
match = parser_regex.search(item)
925902
if match:
926903
try:
927904
result = {}
928905
for j, group_name in enumerate(group_order):
929906
captured_string = match.group(j + 1)
930-
# 直接使用捕获的字符串
931907
result[group_name] = captured_string
932908
translations[result['original']] = result['translated']
933-
matched_count += 1
934-
if matched_count <= 3: # 只打印前3个
935-
logger.info(f"[DEBUG] Item {idx} 匹配成功: original={repr(result['original'])}, translated={repr(result['translated'])}")
936-
logger.info(f"[DEBUG] Item {idx} 匹配的groups: {match.groups()}")
937-
except (IndexError, KeyError) as e:
938-
logger.info(f"[DEBUG] Item {idx} 解析失败: {item[:100]}... Error: {e}")
909+
except (IndexError, KeyError):
939910
continue # 跳过解析失败的条目
940-
else:
941-
logger.info(f"[DEBUG] Item {idx} 正则匹配失败: {repr(item[:100])}")
942911

943912
if not translations:
944913
logger.warning(f"Could not parse any translations from '{os.path.basename(text_file_path)}'.")
945-
logger.info(f"[DEBUG] 解析失败总结: 找到 {len(items)} 个条目,但没有成功解析任何翻译")
946914
return "错误:未能从TXT文件中解析出任何翻译内容"
947915

948916
logger.info(f"解析出 {len(translations)} 条翻译")
949-
logger.info(f"[DEBUG] 前3条翻译: {list(translations.items())[:3]}")
950917

951918
# 2.5. 创建标准化映射(用于模糊匹配)
952919
def normalize_text(text):

0 commit comments

Comments
 (0)