diff --git a/backend/src/plugins/translate/_batch_proc.py b/backend/src/plugins/translate/_batch_proc.py index 75c48e2f..da8066d4 100644 --- a/backend/src/plugins/translate/_batch_proc.py +++ b/backend/src/plugins/translate/_batch_proc.py @@ -127,21 +127,27 @@ class BatchProcessingService: llm_rows, pre_rows = [], [] tls_lower = [str(t).lower() for t in tls] for row in batch_rows: - # ★ NEW: Same-language pre-filter — detected language matches target + # ★ Same-language pre-filter: only short-circuit when ALL targets + # match the detected language. If only SOME match, still process + # other targets via cache or LLM. dl = row.get("_detected_lang") if dl and str(dl).lower() not in ("und", "") and str(dl).lower() in tls_lower: + non_matching = [t for t in tls if str(t).lower() != str(dl).lower()] + if not non_matching: + # All targets are the same as detected language — no translation needed + row["_same_language"] = True + pre_rows.append(row) + continue + # Partial match: mark same-language for later use, but don't skip row["_same_language"] = True - pre_rows.append(row) - continue if row.get("approved_translation"): pre_rows.append(row) continue cl = row.get("_cached_lang_values") - if cl: - if all(lc in cl for lc in tls): - pre_rows.append(row) - continue + if cl and all(lc in cl for lc in tls): + pre_rows.append(row) + continue if preview_edits_cache: sd = row.get("source_data") or {} if sd: @@ -159,34 +165,18 @@ class BatchProcessingService: def _persist_pre(self, pre_rows, bid, run_id, tls): count = 0 for row in pre_rows: - # ★ Same-language row: source language = target → no translation needed - if row.get("_same_language"): - dl = str(row["_detected_lang"]) - source_text = row.get("source_text", "") - rec = TranslationRecord( - id=str(uuid.uuid4()), batch_id=bid, run_id=run_id, - source_sql=source_text, target_sql=source_text, - source_object_type="table_row", source_object_id=row.get("row_index"), - source_object_name=row.get("source_object_name", ""), - source_data=row.get("source_data"), source_hash=row.get("_source_hash"), - status="SUCCESS", - ) - self.db.add(rec) - # One TranslationLanguage for the detected (same) language - self.db.add(TranslationLanguage( - id=str(uuid.uuid4()), record_id=rec.id, language_code=dl, - source_language_detected=dl, translated_value=source_text, - final_value=source_text, status="translated", needs_review=False, - )) - count += 1 - continue - - # Existing: cached / approved rows cl = row.get("_cached_lang_values") detected_lang = row.get("_detected_lang", "und") or "und" + source_text = row.get("source_text", "") + is_same = row.get("_same_language") + + # target_sql: prefer approved_translation, then first cached value, then source + primary_cached = next(iter(cl.values()), "") if cl else "" + target_sql = row.get("approved_translation") or primary_cached or source_text + rec = TranslationRecord( id=str(uuid.uuid4()), batch_id=bid, run_id=run_id, - source_sql=row.get("source_text", ""), target_sql=row.get("approved_translation", ""), + source_sql=source_text, target_sql=target_sql, source_object_type="table_row", source_object_id=row.get("row_index"), source_object_name=row.get("source_object_name", ""), source_data=row.get("source_data"), source_hash=row.get("_source_hash"), @@ -194,7 +184,13 @@ class BatchProcessingService: ) self.db.add(rec) for lc in tls: - fv = cl[lc] if (cl and lc in cl) else row.get("approved_translation", "") + if is_same and str(lc).lower() == str(detected_lang).lower(): + # Same language: use source text as-is (no translation needed) + fv = source_text + elif cl and lc in cl: + fv = cl[lc] + else: + fv = row.get("approved_translation", "") self.db.add(TranslationLanguage( id=str(uuid.uuid4()), record_id=rec.id, language_code=lc, source_language_detected=detected_lang, translated_value=fv,