Skip to content

Commit 56cf52d

Browse files
author
bram
committed
Fixed issue with fuzzy
1 parent c8226e2 commit 56cf52d

File tree

1 file changed

+55
-23
lines changed

1 file changed

+55
-23
lines changed

python_gpt_po/services/translation_service.py

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,10 @@ def _show_results_summary(self, stats: TranslationStats):
428428
"""Show final results summary after processing."""
429429
logging.info("")
430430
logging.info("=" * 70)
431-
logging.info("TRANSLATION RESULTS")
431+
if self.config.flags.fix_fuzzy:
432+
logging.info("FUZZY FIX RESULTS")
433+
else:
434+
logging.info("TRANSLATION RESULTS")
432435
logging.info("=" * 70)
433436

434437
logging.info("PROCESSING SUMMARY:")
@@ -437,8 +440,12 @@ def _show_results_summary(self, stats: TranslationStats):
437440
logging.info(" Total files handled: %d", stats.files.processed + stats.files.skipped)
438441

439442
logging.info("")
440-
logging.info("TRANSLATION RESULTS:")
441-
logging.info(" Entries successfully translated: %d", stats.entries.translated)
443+
if self.config.flags.fix_fuzzy:
444+
logging.info("FUZZY FIX RESULTS:")
445+
logging.info(" Fuzzy entries fixed: %d", stats.entries.translated)
446+
else:
447+
logging.info("TRANSLATION RESULTS:")
448+
logging.info(" Entries successfully translated: %d", stats.entries.translated)
442449
if stats.entries.failed > 0:
443450
logging.info(" Entries failed/skipped: %d", stats.entries.failed)
444451

@@ -563,7 +570,11 @@ def _analyze_po_file(self, po_file):
563570
stats.translated_in_file = stats.total_in_file - stats.untranslated
564571
# Also count fuzzy entries when fix_fuzzy is enabled
565572
if self.config.flags.fix_fuzzy:
573+
# Count all fuzzy entries
566574
stats.fuzzy = len([e for e in po_file if 'fuzzy' in e.flags])
575+
# Also count header if it's fuzzy
576+
if hasattr(po_file, 'metadata_is_fuzzy') and po_file.metadata_is_fuzzy:
577+
stats.fuzzy += 1
567578
else:
568579
stats.fuzzy = 0
569580
return stats
@@ -588,13 +599,19 @@ def _scan_po_files(self, input_folder: str, languages: List[str], gitignore_pars
588599
results.total_translated_in_all_files += stats.translated_in_file
589600

590601
# Include files with fuzzy entries when fix_fuzzy is enabled
591-
needs_processing = stats.untranslated > 0 or (self.config.flags.fix_fuzzy and stats.fuzzy > 0)
602+
if self.config.flags.fix_fuzzy:
603+
# In fix-fuzzy mode, only process files with fuzzy entries
604+
needs_processing = stats.fuzzy > 0
605+
entries_to_process = stats.fuzzy
606+
else:
607+
# In normal mode, process files with untranslated entries
608+
needs_processing = stats.untranslated > 0
609+
entries_to_process = stats.untranslated
592610

593611
if needs_processing:
594-
entries_to_process = stats.untranslated if not self.config.flags.fix_fuzzy else stats.fuzzy
595612
results.files_to_process.append((po_file_path, po_file_result, entries_to_process))
596613
results.total_entries += entries_to_process
597-
if self.config.flags.fix_fuzzy and stats.fuzzy > 0:
614+
if self.config.flags.fix_fuzzy:
598615
logging.debug("File %s: %d fuzzy entries to fix", po_file_path, stats.fuzzy)
599616
else:
600617
logging.debug("File %s: %d/%d entries need translation",
@@ -611,7 +628,15 @@ def _track_file_progress(self, po_file_path, initial_count):
611628
"""Track translation progress for a single file."""
612629
try:
613630
po_file = polib.pofile(po_file_path)
614-
remaining = len([e for e in po_file if is_entry_untranslated(e)])
631+
if self.config.flags.fix_fuzzy:
632+
# In fix-fuzzy mode, count remaining fuzzy entries
633+
remaining = len([e for e in po_file if 'fuzzy' in e.flags])
634+
# Also check if header is still fuzzy
635+
if hasattr(po_file, 'metadata_is_fuzzy') and po_file.metadata_is_fuzzy:
636+
remaining += 1
637+
else:
638+
# In normal mode, count untranslated entries
639+
remaining = len([e for e in po_file if is_entry_untranslated(e)])
615640
return initial_count - remaining, remaining
616641
except Exception:
617642
return initial_count, 0
@@ -944,25 +969,32 @@ def fix_fuzzy_entries(
944969
detail_language: Optional[str] = None,
945970
):
946971
"""Find and fix fuzzy entries in a PO file using AI translation."""
972+
# Check if the metadata/header is fuzzy
973+
header_was_fuzzy = False
974+
if hasattr(po_file, 'metadata_is_fuzzy') and po_file.metadata_is_fuzzy:
975+
header_was_fuzzy = True
976+
po_file.metadata_is_fuzzy = False
977+
logging.info("Removed fuzzy flag from file header in %s", po_file_path)
978+
979+
# Find all fuzzy entries
947980
fuzzy_entries = [entry for entry in po_file if 'fuzzy' in entry.flags]
948-
949-
if not fuzzy_entries:
981+
if not fuzzy_entries and not header_was_fuzzy:
950982
logging.info("No fuzzy entries found in %s", po_file_path)
951983
return
984+
if fuzzy_entries:
985+
logging.info("Found %d fuzzy entries to fix in %s", len(fuzzy_entries), po_file_path)
952986

953-
logging.info("Found %d fuzzy entries to fix in %s", len(fuzzy_entries), po_file_path)
954-
955-
texts_to_translate = [entry.msgid for entry in fuzzy_entries]
956-
translations = self.get_translations(texts_to_translate, target_language, po_file_path, detail_language)
987+
texts_to_translate = [entry.msgid for entry in fuzzy_entries]
988+
translations = self.get_translations(texts_to_translate, target_language, po_file_path, detail_language)
957989

958-
self._update_fuzzy_po_entries(po_file, translations, entries_to_update=fuzzy_entries)
990+
self._update_fuzzy_po_entries(po_file, translations, entries_to_update=fuzzy_entries)
959991

960-
po_file.save(po_file_path)
961-
962-
self.po_file_handler.log_translation_status(
963-
po_file_path,
964-
texts_to_translate,
965-
[entry.msgstr for entry in fuzzy_entries]
966-
)
967-
968-
logging.info("Fuzzy fix completed for %s", po_file_path)
992+
self.po_file_handler.log_translation_status(
993+
po_file_path,
994+
texts_to_translate,
995+
[entry.msgstr for entry in fuzzy_entries]
996+
)
997+
# Save the file if any changes were made (header fuzzy removal or entry translations)
998+
if header_was_fuzzy or fuzzy_entries:
999+
po_file.save(po_file_path)
1000+
logging.info("Fuzzy fix completed for %s", po_file_path)

0 commit comments

Comments
 (0)