diff --git a/po_update_from_tsv.py b/po_update_from_tsv.py index 82f642f..0b435a2 100644 --- a/po_update_from_tsv.py +++ b/po_update_from_tsv.py @@ -108,19 +108,32 @@ def update_po_file(file_path, translations_to_update): for msgctxt, new_msgstr in translations_to_update.items(): if msgctxt in po_data: entry = po_data[msgctxt] + current_msgstr = entry['msgstr'] + + # 먼저 현재 값과 새 값이 다른지 확인 + if current_msgstr == new_msgstr: + # 변경사항 없음 - 건너뜀 (로그 출력 안함) + continue + original_block = entry['original_block'] - - old_msgstr_part = f'msgstr "{entry["msgstr"]}"' - escaped_new_msgstr = new_msgstr.replace('"', '\\"') - new_msgstr_part = f'msgstr "{escaped_new_msgstr}"' - - if old_msgstr_part in original_block: - updated_block = original_block.replace(old_msgstr_part, new_msgstr_part) + + # 백슬래시와 쌍따옴표를 올바르게 이스케이프 + escaped_new_msgstr = new_msgstr.replace('\\', '\\\\').replace('"', '\\"') + + # msgstr 라인 전체를 교체하는 정규식 + updated_block = re.sub( + r'^(msgstr\s+)\".*\"$', + r'\1"' + escaped_new_msgstr + '"', + original_block, + flags=re.MULTILINE + ) + + if updated_block != original_block: content = content.replace(original_block, updated_block) logging.info(f" > 업데이트: msgctxt='{msgctxt}'") update_count += 1 else: - logging.warning(f" > 패턴 불일치 (업데이트 실패): msgctxt='{msgctxt}'의 msgstr을 원본에서 찾지 못했습니다.") + logging.warning(f" > 정규식 교체 실패: msgctxt='{msgctxt}'") else: logging.warning(f" > msgctxt 찾기 실패: '{msgctxt}'를 PO 파일에서 찾을 수 없습니다.")