import { useEffect } from 'react'; import { useTempoUi } from '@/components/tempo-brand'; import { TranslationRequestError, translateTexts } from '@/translation/service'; import { protectTranslationTokens, restoreTranslationTokens } from '@/translation/utils'; // Never change the structure of React's DOM or the value of a form control. const excluded = 'script,style,noscript,code,pre,textarea,[contenteditable]:not([contenteditable="false"]),[translate="no"],[data-no-translate],.no-translate'; const attributes = ['placeholder', 'title', 'aria-label', 'alt'] as const; type Target = { node: Text | Element; attribute?: string; original: string; translated?: string }; type QueuedText = { value: string; resolve: (value: string) => void; reject: (error: unknown) => void }; const batchMaxItems = 8; const batchMaxChars = 2200; const requestMaxChars = 9000; function read(target: Target): string | null { return target.attribute ? (target.node as Element).getAttribute(target.attribute) : target.node.nodeValue; } function write(target: Target, value: string): void { if (target.attribute) (target.node as Element).setAttribute(target.attribute, value); else target.node.nodeValue = value; } export function PageTranslator() { const { language } = useTempoUi(); useEffect(() => { document.documentElement.lang = language; if (language === 'pt') return; let active = true; const abort = new AbortController(); const targets = new Map>(); const cache = new Map>(); const queue: QueuedText[] = []; let running = 0; let timer: ReturnType | undefined; let unavailable = false; let failureReported = false; const schedule = (delay = 0) => { if (!active || unavailable || timer || running >= 2 || !queue.length) return; timer = setTimeout(() => { timer = undefined; void flush(); }, delay); }; const rejectQueued = (error: unknown) => { if (timer) clearTimeout(timer); timer = undefined; queue.splice(0).forEach((item) => item.reject(error)); }; const enqueue = (value: string): Promise => new Promise((resolve, reject) => { queue.push({ value, resolve, reject }); schedule(); }); const flush = async () => { if (!active || unavailable || running >= 2 || !queue.length) return; const batch: QueuedText[] = []; let batchLength = 0; while (queue.length && batch.length < batchMaxItems) { const next = queue[0]; if (batch.length > 0 && batchLength + next.value.length > batchMaxChars) break; batch.push(queue.shift()!); batchLength += next.value.length; } running++; schedule(650); try { const translated = await translateTexts( batch.map((item) => item.value), 'auto', language, abort.signal, ); batch.forEach((item, index) => item.resolve(translated[index])); } catch (error) { const isUnavailable = !abort.signal.aborted && error instanceof TranslationRequestError && (error.status === 503 || error.status === null); if (isUnavailable) { unavailable = true; rejectQueued(error); } batch.forEach((item) => item.reject(error)); } finally { running--; schedule(650); } }; const splitForRequests = (value: string): string[] => { const chunks: string[] = []; let start = 0; while (start < value.length) { let end = Math.min(start + requestMaxChars, value.length); if (end < value.length) { const boundary = value.lastIndexOf(' ', end); if (boundary > start) end = boundary + 1; } chunks.push(value.slice(start, end)); start = end; } return chunks; }; const translate = (value: string): Promise => { const previous = cache.get(value); if (previous) return previous; const protectedText = protectTranslationTokens(value); const promise = Promise.all( splitForRequests(protectedText.text).map(enqueue), ).then((translatedChunks) => { const translatedText = translatedChunks.join(''); for (const token of protectedText.replacements.keys()) { if (!translatedText.includes(token)) { throw new Error('Translation lost a protected token'); } } return restoreTranslationTokens(translatedText, protectedText.replacements); }); cache.set(value, promise); promise.catch(() => cache.delete(value)); return promise; }; const consider = (node: Text | Element, attribute?: string) => { const element = node instanceof Element ? node : node.parentElement; if (!element || element.closest(excluded)) return; // Without an explicit value, translating an option changes the submitted value. if (element.closest('option:not([value])')) return; const key = attribute ?? ''; const value = attribute ? (node as Element).getAttribute(attribute) : node.nodeValue; if (!value || !/\p{L}/u.test(value)) return; let entries = targets.get(node); if (!entries) { entries = new Map(); targets.set(node, entries); } const old = entries.get(key); if (old && (value === old.original || value === old.translated)) return; const target: Target = { node, attribute, original: value }; entries.set(key, target); translate(value).then((translated) => { if (!active || !node.isConnected || entries?.get(key) !== target || read(target) !== value) return; target.translated = translated; write(target, translated); }).catch((error) => { if (!abort.signal.aborted && !failureReported) { failureReported = true; console.error('Page translation failed; original text was kept.', error); } }); }; const scan = (root: Node) => { if (root instanceof Text) { consider(root); return; } if (!(root instanceof Element)) return; if (root.closest(excluded)) return; for (const name of attributes) if (root.hasAttribute(name)) consider(root, name); const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT); while (walker.nextNode()) { const node = walker.currentNode; if (node instanceof Text) consider(node); else if (node instanceof Element) for (const name of attributes) if (node.hasAttribute(name)) consider(node, name); } }; const observer = new MutationObserver((mutations) => { for (const mutation of mutations) { if (mutation.type === 'characterData') scan(mutation.target); else if (mutation.type === 'attributes' && mutation.target instanceof Element && mutation.attributeName) consider(mutation.target, mutation.attributeName); else mutation.addedNodes.forEach(scan); } }); observer.observe(document.documentElement, { subtree: true, childList: true, characterData: true, attributes: true, attributeFilter: [...attributes] }); scan(document.documentElement); return () => { active = false; abort.abort(); if (timer) clearTimeout(timer); observer.disconnect(); for (const entries of targets.values()) for (const target of entries.values()) { if (target.translated !== undefined && read(target) === target.translated) write(target, target.original); } }; }, [language]); return null; }