diff --git a/public/global.d.ts b/public/global.d.ts index f6935383d..dc4f28f51 100644 --- a/public/global.d.ts +++ b/public/global.d.ts @@ -40,4 +40,12 @@ declare global { searchInputCssClass?: string; } } + + /** + * Translates a text to a target language using a translation provider. + * @param text Text to translate + * @param lang Target language + * @param provider Translation provider + */ + async function translate(text: string, lang: string, provider: string = null): Promise; } diff --git a/public/scripts/extensions/expressions/index.js b/public/scripts/extensions/expressions/index.js index 6e68deb66..9faa16215 100644 --- a/public/scripts/extensions/expressions/index.js +++ b/public/scripts/extensions/expressions/index.js @@ -929,8 +929,8 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin return extension_settings.expressions.fallback_expression; } - if (extension_settings.expressions.translate && typeof window['translate'] === 'function') { - text = await window['translate'](text, 'en'); + if (extension_settings.expressions.translate && typeof globalThis.translate === 'function') { + text = await globalThis.translate(text, 'en'); } text = sampleClassifyText(text); diff --git a/public/scripts/extensions/translate/index.js b/public/scripts/extensions/translate/index.js index 1f01f1913..ee89f86f0 100644 --- a/public/scripts/extensions/translate/index.js +++ b/public/scripts/extensions/translate/index.js @@ -605,7 +605,7 @@ const handleOutgoingMessage = createEventHandler(translateOutgoingMessage, () => const handleImpersonateReady = createEventHandler(translateImpersonate, () => shouldTranslate(incomingTypes)); const handleMessageEdit = createEventHandler(translateMessageEdit, () => true); -window['translate'] = translate; +globalThis.translate = translate; jQuery(async () => { const html = await renderExtensionTemplateAsync('translate', 'index'); diff --git a/public/scripts/extensions/vectors/index.js b/public/scripts/extensions/vectors/index.js index 5506bec00..60eea49f0 100644 --- a/public/scripts/extensions/vectors/index.js +++ b/public/scripts/extensions/vectors/index.js @@ -561,9 +561,9 @@ async function retrieveFileChunks(queryText, collectionId) { */ async function vectorizeFile(fileText, fileName, collectionId, chunkSize, overlapPercent) { try { - if (settings.translate_files && typeof window['translate'] === 'function') { + if (settings.translate_files && typeof globalThis.translate === 'function') { console.log(`Vectors: Translating file ${fileName} to English...`); - const translatedText = await window['translate'](fileText, 'en'); + const translatedText = await globalThis.translate(fileText, 'en'); fileText = translatedText; }