4th_fix time error

This commit is contained in:
beabigegg
2025-09-03 09:05:51 +08:00
parent e6e5332705
commit cce3fd4925
26 changed files with 2551 additions and 82 deletions

View File

@@ -74,8 +74,11 @@ class DocxParser(DocumentParser):
def generate_translated_document(self, translations: Dict[str, List[str]],
target_language: str, output_dir: Path) -> str:
"""生成翻譯後的 DOCX 文件 - 使用增強的翻譯插入邏輯"""
"""生成翻譯後的 DOCX 文件 - 使用增強的翻譯插入邏輯(從快取讀取)"""
try:
from sqlalchemy import text as sql_text
from app import db
# 生成輸出檔名
output_filename = generate_filename(
self.file_path.name,
@@ -88,16 +91,29 @@ class DocxParser(DocumentParser):
# 提取段落資訊
segments = self.extract_segments_with_context()
# 建立翻譯映射
# 建立翻譯映射 - 從快取讀取而非使用傳入的translations參數
translation_map = {}
translated_texts = translations.get(target_language, [])
# 對應文字段落與翻譯
text_index = 0
logger.info(f"Building translation map for {len(segments)} segments in language {target_language}")
for seg in segments:
if text_index < len(translated_texts):
translation_map[(target_language, seg.text)] = translated_texts[text_index]
text_index += 1
# 從翻譯快取中查詢每個段落的翻譯
result = db.session.execute(sql_text("""
SELECT translated_text
FROM dt_translation_cache
WHERE source_text = :text AND target_language = :lang
ORDER BY created_at DESC
LIMIT 1
"""), {'text': seg.text, 'lang': target_language})
row = result.fetchone()
if row and row[0]:
translation_map[(target_language, seg.text)] = row[0]
logger.debug(f"Found translation for: {seg.text[:50]}...")
else:
logger.warning(f"No translation found for: {seg.text[:50]}...")
logger.info(f"Translation map built with {len(translation_map)} mappings")
# 使用增強的翻譯插入邏輯
ok_count, skip_count = self.processor.insert_docx_translations(