Use message translator for widget copy
This commit is contained in:
@@ -1191,32 +1191,17 @@ async function translateWidgetCopyOnce(sourceCopy, sourceLanguage, targetLanguag
|
|||||||
const settings = getTranslationSettings();
|
const settings = getTranslationSettings();
|
||||||
if (!settings.enabled || !settings.openaiApiKey || !targetLanguage || sourceLanguage === targetLanguage) return null;
|
if (!settings.enabled || !settings.openaiApiKey || !targetLanguage || sourceLanguage === targetLanguage) return null;
|
||||||
try {
|
try {
|
||||||
const response = await fetch("https://api.openai.com/v1/responses", {
|
const translatedCopy = {};
|
||||||
method: "POST",
|
for (const field of WIDGET_COPY_FIELDS) {
|
||||||
headers: {
|
const value = String(sourceCopy[field] || "").trim();
|
||||||
"Authorization": `Bearer ${settings.openaiApiKey}`,
|
if (!value) {
|
||||||
"Content-Type": "application/json"
|
translatedCopy[field] = "";
|
||||||
},
|
continue;
|
||||||
body: JSON.stringify({
|
|
||||||
model: settings.model,
|
|
||||||
input: [
|
|
||||||
{
|
|
||||||
role: "system",
|
|
||||||
content: "Translate UI copy for a live chat widget. Return only compact JSON with the same keys. Preserve brand names and meaning. Keep labels short."
|
|
||||||
},
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: JSON.stringify({ sourceLanguage, targetLanguage, copy: sourceCopy })
|
|
||||||
}
|
}
|
||||||
]
|
const result = await translateText(value, targetLanguage, settings.model, settings.openaiApiKey);
|
||||||
})
|
translatedCopy[field] = String(result.translatedText || value).trim();
|
||||||
});
|
|
||||||
if (!response.ok) {
|
|
||||||
const openAiError = await openAiErrorFromResponse(response);
|
|
||||||
throw new Error(openAiError.message);
|
|
||||||
}
|
}
|
||||||
const data = await response.json();
|
const translated = normalizeWidgetCopyEntry(translatedCopy, targetLanguage);
|
||||||
const translated = normalizeWidgetCopyEntry(parseJsonBlock(outputTextFromResponse(data)), targetLanguage);
|
|
||||||
if (widgetCopyLooksUntranslated(sourceCopy, translated)) return null;
|
if (widgetCopyLooksUntranslated(sourceCopy, translated)) return null;
|
||||||
return translated;
|
return translated;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -1230,11 +1215,6 @@ function widgetCopyLooksUntranslated(sourceCopy, translatedCopy) {
|
|||||||
return importantFields.every((field) => String(sourceCopy[field] || "").trim() === String(translatedCopy[field] || "").trim());
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
function syncLegacyWidgetCopy(settings) {
|
function syncLegacyWidgetCopy(settings) {
|
||||||
for (const language of Object.keys(settings.widgetCopy || {})) {
|
for (const language of Object.keys(settings.widgetCopy || {})) {
|
||||||
const copy = settings.widgetCopy[language];
|
const copy = settings.widgetCopy[language];
|
||||||
|
|||||||
Reference in New Issue
Block a user