Guard lazy widget copy translations
This commit is contained in:
@@ -1218,13 +1218,20 @@ async function translateWidgetCopyOnce(sourceCopy, sourceLanguage, targetLanguag
|
||||
throw new Error(openAiError.message);
|
||||
}
|
||||
const data = await response.json();
|
||||
return normalizeWidgetCopyEntry(parseJsonBlock(outputTextFromResponse(data)), targetLanguage);
|
||||
const translated = normalizeWidgetCopyEntry(parseJsonBlock(outputTextFromResponse(data)), targetLanguage);
|
||||
if (widgetCopyLooksUntranslated(sourceCopy, translated)) return null;
|
||||
return translated;
|
||||
} catch (error) {
|
||||
console.error("widget_copy_translation_failed", targetLanguage, error.message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function widgetCopyLooksUntranslated(sourceCopy, translatedCopy) {
|
||||
const importantFields = ["title", "intro", "offlineIntro", "placeholder", "sendLabel"];
|
||||
return importantFields.every((field) => String(sourceCopy[field] || "").trim() === String(translatedCopy[field] || "").trim());
|
||||
}
|
||||
|
||||
function parseJsonBlock(value) {
|
||||
const text = String(value || "").trim().replace(/^```(?:json)?/i, "").replace(/```$/, "").trim();
|
||||
return JSON.parse(text);
|
||||
|
||||
Reference in New Issue
Block a user