.
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useTempoUi } from '@/components/tempo-brand';
|
||||
import { translateText } 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 };
|
||||
|
||||
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(() => {
|
||||
if (language === 'pt') return;
|
||||
let active = true;
|
||||
const abort = new AbortController();
|
||||
const targets = new Map<Node, Map<string, Target>>();
|
||||
const cache = new Map<string, Promise<string>>();
|
||||
const queue: Array<() => void> = [];
|
||||
let running = 0;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
let unavailable = false;
|
||||
|
||||
const run = () => {
|
||||
if (!active || unavailable || timer || running >= 2 || !queue.length) return;
|
||||
running++;
|
||||
queue.shift()?.();
|
||||
// Stay below the API's per-minute limit on large pages.
|
||||
timer = setTimeout(() => { timer = undefined; run(); }, 650);
|
||||
};
|
||||
const translate = (value: string): Promise<string> => {
|
||||
const previous = cache.get(value);
|
||||
if (previous) return previous;
|
||||
const promise = new Promise<string>((resolve, reject) => {
|
||||
queue.push(() => {
|
||||
const protectedText = protectTranslationTokens(value);
|
||||
translateText({ text: protectedText.text, sourceLanguage: 'auto', targetLanguage: language }, abort.signal)
|
||||
.then(({ translatedText }) => {
|
||||
for (const token of protectedText.replacements.keys()) {
|
||||
if (!translatedText.includes(token)) throw new Error('Translation lost a protected token');
|
||||
}
|
||||
resolve(restoreTranslationTokens(translatedText, protectedText.replacements));
|
||||
})
|
||||
.catch((error) => {
|
||||
if (!abort.signal.aborted && (error?.status === 503 || error?.status === null)) unavailable = true;
|
||||
reject(error);
|
||||
})
|
||||
.finally(() => { running--; run(); });
|
||||
});
|
||||
run();
|
||||
});
|
||||
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 || value.length > 10000 || !/\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(() => { /* Keep the original visible when translation fails. */ });
|
||||
};
|
||||
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.body, { subtree: true, childList: true, characterData: true, attributes: true, attributeFilter: [...attributes] });
|
||||
scan(document.body);
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
import type {
|
||||
TranslateRequest,
|
||||
TranslateResponse,
|
||||
TranslationLanguage,
|
||||
} from "./types";
|
||||
import { readLanguageList } from "./utils";
|
||||
|
||||
const translationApiBaseUrl = "/api";
|
||||
|
||||
const REQUEST_TIMEOUT_MS = 65_000;
|
||||
const MAX_ATTEMPTS = 3;
|
||||
|
||||
export class TranslationRequestError extends Error {
|
||||
status: number | null;
|
||||
retryable: boolean;
|
||||
|
||||
constructor(message: string, options?: { status?: number | null; retryable?: boolean }) {
|
||||
super(message);
|
||||
this.name = "TranslationRequestError";
|
||||
this.status = options?.status ?? null;
|
||||
this.retryable = options?.retryable ?? false;
|
||||
}
|
||||
}
|
||||
|
||||
function isRetryableStatus(status: number): boolean {
|
||||
return [429, 500, 502, 503, 504].includes(status);
|
||||
}
|
||||
|
||||
async function wait(ms: number): Promise<void> {
|
||||
await new Promise((resolve) => window.setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async function fetchJson<T>(
|
||||
input: RequestInfo | URL,
|
||||
init: RequestInit = {},
|
||||
attempt = 1,
|
||||
maxAttempts = MAX_ATTEMPTS,
|
||||
): Promise<T> {
|
||||
const parentSignal = init.signal;
|
||||
const controller = new AbortController();
|
||||
const timeoutId = window.setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
|
||||
|
||||
const forwardAbort = () => controller.abort();
|
||||
parentSignal?.addEventListener("abort", forwardAbort, { once: true });
|
||||
|
||||
try {
|
||||
const response = await fetch(input, {
|
||||
...init,
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
...(init.headers ?? {}),
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const retryable = isRetryableStatus(response.status);
|
||||
if (retryable && attempt < maxAttempts) {
|
||||
await wait(300 * attempt);
|
||||
return fetchJson<T>(input, init, attempt + 1, maxAttempts);
|
||||
}
|
||||
|
||||
let message = `Translation request failed with status ${response.status}`;
|
||||
try {
|
||||
const payload = (await response.json()) as { message?: string };
|
||||
if (payload?.message) {
|
||||
message = payload.message;
|
||||
}
|
||||
} catch {
|
||||
// Ignore JSON parse issues on error responses.
|
||||
}
|
||||
|
||||
throw new TranslationRequestError(message, {
|
||||
status: response.status,
|
||||
retryable,
|
||||
});
|
||||
}
|
||||
|
||||
return (await response.json()) as T;
|
||||
} catch (error) {
|
||||
const aborted = controller.signal.aborted;
|
||||
const isDomAbort = error instanceof DOMException && error.name === "AbortError";
|
||||
const parentAborted = parentSignal?.aborted ?? false;
|
||||
const timedOut = aborted && !parentAborted;
|
||||
const retryable = timedOut || error instanceof TypeError;
|
||||
|
||||
if (retryable && attempt < maxAttempts) {
|
||||
await wait(300 * attempt);
|
||||
return fetchJson<T>(input, init, attempt + 1, maxAttempts);
|
||||
}
|
||||
|
||||
if (error instanceof TranslationRequestError) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (parentAborted || isDomAbort) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (timedOut) {
|
||||
throw new TranslationRequestError("Translation request timed out", {
|
||||
status: null,
|
||||
retryable,
|
||||
});
|
||||
}
|
||||
|
||||
throw new TranslationRequestError("Translation request failed", {
|
||||
status: null,
|
||||
retryable,
|
||||
});
|
||||
} finally {
|
||||
window.clearTimeout(timeoutId);
|
||||
parentSignal?.removeEventListener("abort", forwardAbort);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getLanguages(signal?: AbortSignal): Promise<TranslationLanguage[]> {
|
||||
const payload = await fetchJson<unknown>(`${translationApiBaseUrl}/translate/languages`, {
|
||||
method: "GET",
|
||||
signal,
|
||||
}, 1, 1);
|
||||
|
||||
return readLanguageList(payload);
|
||||
}
|
||||
|
||||
export async function translateText(
|
||||
request: TranslateRequest,
|
||||
signal?: AbortSignal,
|
||||
): Promise<TranslateResponse> {
|
||||
const payload = await fetchJson<{
|
||||
original_text?: string;
|
||||
translated_text?: string;
|
||||
source_lang?: string;
|
||||
target_lang?: string;
|
||||
}>(`${translationApiBaseUrl}/translate`, {
|
||||
method: "POST",
|
||||
signal,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text: request.text,
|
||||
source_lang: request.sourceLanguage,
|
||||
target_lang: request.targetLanguage,
|
||||
is_html: request.isHtml ?? false,
|
||||
}),
|
||||
}, 1, 1);
|
||||
|
||||
if (typeof payload.translated_text !== "string" || payload.translated_text.trim() === "") {
|
||||
throw new TranslationRequestError("Translation response is missing translated_text", {
|
||||
status: 200,
|
||||
retryable: false,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
originalText: payload.original_text ?? request.text,
|
||||
translatedText: payload.translated_text,
|
||||
sourceLanguage: payload.source_lang ?? request.sourceLanguage,
|
||||
targetLanguage: payload.target_lang ?? request.targetLanguage,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
export interface TranslationLanguage {
|
||||
code: string;
|
||||
name: string;
|
||||
targets?: string[];
|
||||
}
|
||||
|
||||
export interface TranslateRequest {
|
||||
text: string;
|
||||
sourceLanguage: string;
|
||||
targetLanguage: string;
|
||||
isHtml?: boolean;
|
||||
}
|
||||
|
||||
export interface TranslateResponse {
|
||||
originalText: string;
|
||||
translatedText: string;
|
||||
sourceLanguage: string;
|
||||
targetLanguage: string;
|
||||
}
|
||||
|
||||
export interface TranslationCacheEntry {
|
||||
value: string;
|
||||
createdAt: number;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
export interface ProtectedTranslationTokens {
|
||||
text: string;
|
||||
replacements: Map<string, string>;
|
||||
}
|
||||
|
||||
export interface TranslationPayloadHealth {
|
||||
shouldTranslate: boolean;
|
||||
normalizedText: string;
|
||||
}
|
||||
|
||||
export interface TranslateOptions {
|
||||
isHtml?: boolean;
|
||||
signal?: AbortSignal;
|
||||
protectedTerms?: string[];
|
||||
}
|
||||
@@ -0,0 +1,615 @@
|
||||
import type {
|
||||
ProtectedTranslationTokens,
|
||||
TranslationPayloadHealth,
|
||||
TranslationCacheEntry,
|
||||
TranslationLanguage,
|
||||
} from "./types";
|
||||
|
||||
const CACHE_PREFIX = "tempo_translation_v1";
|
||||
const CACHE_VERSION = "v1";
|
||||
const CACHE_TTL = 7 * 24 * 60 * 60 * 1000;
|
||||
const CACHE_MAX_ENTRIES = 200;
|
||||
const CACHE_CLEANUP_INTERVAL = 12 * 60 * 60 * 1000;
|
||||
const CLEANUP_MARKER_KEY = `${CACHE_PREFIX}:cleanup`;
|
||||
const memoryCache = new Map<string, TranslationCacheEntry>();
|
||||
|
||||
const LANGUAGE_ALIASES: Record<string, string> = {
|
||||
ar: "ar",
|
||||
"ar-sa": "ar",
|
||||
en: "en",
|
||||
"en-gb": "en",
|
||||
"en-us": "en",
|
||||
es: "es",
|
||||
"es-es": "es",
|
||||
fr: "fr",
|
||||
"fr-fr": "fr",
|
||||
hi: "hi",
|
||||
"hi-in": "hi",
|
||||
pt: "pt",
|
||||
"pt-mz": "pt",
|
||||
"pt-pt": "pt",
|
||||
zh: "zh-Hans",
|
||||
"zh-cn": "zh-Hans",
|
||||
"zh-hans": "zh-Hans",
|
||||
};
|
||||
|
||||
const apiUnsafeNodes = "script, iframe, object, embed, link, meta";
|
||||
const excludedTranslationNodes =
|
||||
"code, pre, script, style, textarea, noscript, .no-translate, [data-no-translate], [translate='no']";
|
||||
const INTERNAL_PLACEHOLDER_PREFIX = "¤TRX";
|
||||
const BATCH_MAX_ITEMS = 8;
|
||||
const BATCH_MAX_CHARS = 2200;
|
||||
const TRANSLATION_PLACEHOLDER_PATTERN = /¤TRX[A-Z]+\d+¤/g;
|
||||
const HTML_BATCH_ROOT_ATTR = "data-trx-batch-root";
|
||||
const HTML_BATCH_NODE_ATTR = "data-trx-node";
|
||||
|
||||
export const fallbackTranslationLanguages: TranslationLanguage[] = [
|
||||
{ code: "pt", name: "Português" },
|
||||
{ code: "en", name: "English" },
|
||||
{ code: "fr", name: "French" },
|
||||
{ code: "es", name: "Spanish" },
|
||||
{ code: "ar", name: "Arabic" },
|
||||
{ code: "hi", name: "Hindi" },
|
||||
{ code: "zh-Hans", name: "Chinese" },
|
||||
];
|
||||
|
||||
export const DEFAULT_PROTECTED_TERMS = [
|
||||
"Revista TEMPO",
|
||||
"Grupo TEMPO",
|
||||
"Tempo TV",
|
||||
"Tempo Connect",
|
||||
"Informação Pública",
|
||||
"InvesteMoz",
|
||||
"TEMPO",
|
||||
] as const;
|
||||
|
||||
function getStorage(): Storage | null {
|
||||
if (typeof window === "undefined") return null;
|
||||
return window.localStorage;
|
||||
}
|
||||
|
||||
function normalizeAlias(code: string): string {
|
||||
return LANGUAGE_ALIASES[code.trim().toLowerCase()] ?? code.trim();
|
||||
}
|
||||
|
||||
export function normalizeLanguageCode(
|
||||
code: string,
|
||||
availableLanguages: TranslationLanguage[],
|
||||
): string | null {
|
||||
if (!code) return null;
|
||||
|
||||
const normalized = normalizeAlias(code);
|
||||
const supportedCodes = new Set(
|
||||
availableLanguages.map((language) => normalizeAlias(language.code)),
|
||||
);
|
||||
|
||||
return supportedCodes.has(normalized) ? normalized : null;
|
||||
}
|
||||
|
||||
export function getBrowserLanguageCode(code: string): string {
|
||||
switch (code) {
|
||||
case "zh-Hans":
|
||||
return "zh-CN";
|
||||
default:
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
export function getLocaleForLanguage(code: string): string {
|
||||
switch (normalizeAlias(code)) {
|
||||
case "pt":
|
||||
return "pt-MZ";
|
||||
case "zh-Hans":
|
||||
return "zh-CN";
|
||||
default:
|
||||
return normalizeAlias(code);
|
||||
}
|
||||
}
|
||||
|
||||
export function isRtlLanguage(code: string): boolean {
|
||||
return normalizeAlias(code) === "ar";
|
||||
}
|
||||
|
||||
export function readLanguageList(payload: unknown): TranslationLanguage[] {
|
||||
const source = Array.isArray(payload)
|
||||
? payload
|
||||
: Array.isArray((payload as { value?: unknown[] })?.value)
|
||||
? (payload as { value: unknown[] }).value
|
||||
: Array.isArray((payload as { data?: unknown[] })?.data)
|
||||
? (payload as { data: unknown[] }).data
|
||||
: [];
|
||||
|
||||
return source
|
||||
.filter((item): item is TranslationLanguage => {
|
||||
return (
|
||||
Boolean(item) &&
|
||||
typeof item === "object" &&
|
||||
typeof item.code === "string" &&
|
||||
typeof item.name === "string"
|
||||
);
|
||||
})
|
||||
.map((language) => ({
|
||||
code: normalizeAlias(language.code),
|
||||
name: language.name,
|
||||
targets: Array.isArray(language.targets)
|
||||
? language.targets.map((target) => normalizeAlias(target))
|
||||
: undefined,
|
||||
}));
|
||||
}
|
||||
|
||||
export function withPortugueseFirst(
|
||||
languages: TranslationLanguage[],
|
||||
): TranslationLanguage[] {
|
||||
const unique = new Map<string, TranslationLanguage>();
|
||||
|
||||
for (const language of languages) {
|
||||
const code = normalizeAlias(language.code);
|
||||
if (!unique.has(code)) {
|
||||
unique.set(code, {
|
||||
...language,
|
||||
code,
|
||||
name: code === "pt" ? "Português" : language.name,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const items = Array.from(unique.values()).sort((a, b) =>
|
||||
a.name.localeCompare(b.name),
|
||||
);
|
||||
|
||||
const portuguese =
|
||||
items.find((language) => language.code === "pt") ?? fallbackTranslationLanguages[0];
|
||||
|
||||
return [portuguese, ...items.filter((language) => language.code !== "pt")];
|
||||
}
|
||||
|
||||
export function hashTranslationText(value: string): string {
|
||||
let hash = 2166136261;
|
||||
|
||||
for (let index = 0; index < value.length; index += 1) {
|
||||
hash ^= value.charCodeAt(index);
|
||||
hash = Math.imul(hash, 16777619);
|
||||
}
|
||||
|
||||
return (hash >>> 0).toString(16);
|
||||
}
|
||||
|
||||
export function createTranslationCacheKey(params: {
|
||||
sourceLanguage: string;
|
||||
targetLanguage: string;
|
||||
isHtml: boolean;
|
||||
text: string;
|
||||
protectedTerms?: string[];
|
||||
}): string {
|
||||
return [
|
||||
CACHE_PREFIX,
|
||||
CACHE_VERSION,
|
||||
params.sourceLanguage,
|
||||
params.targetLanguage,
|
||||
params.isHtml ? "html" : "text",
|
||||
hashTranslationText(params.text),
|
||||
hashTranslationText(
|
||||
JSON.stringify(
|
||||
collectProtectedTerms(params.protectedTerms).map((term) => term.toLowerCase()),
|
||||
),
|
||||
),
|
||||
].join(":");
|
||||
}
|
||||
|
||||
export function getMemoryCacheEntry(key: string): TranslationCacheEntry | null {
|
||||
const entry = memoryCache.get(key);
|
||||
if (!entry) return null;
|
||||
|
||||
if (entry.expiresAt <= Date.now()) {
|
||||
memoryCache.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
export function setMemoryCacheEntry(key: string, value: string): TranslationCacheEntry {
|
||||
const entry: TranslationCacheEntry = {
|
||||
value,
|
||||
createdAt: Date.now(),
|
||||
expiresAt: Date.now() + CACHE_TTL,
|
||||
};
|
||||
|
||||
memoryCache.set(key, entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
export function getPersistentCacheEntry(key: string): TranslationCacheEntry | null {
|
||||
const storage = getStorage();
|
||||
if (!storage) return null;
|
||||
|
||||
const raw = storage.getItem(key);
|
||||
if (!raw) return null;
|
||||
|
||||
try {
|
||||
const entry = JSON.parse(raw) as TranslationCacheEntry;
|
||||
if (entry.expiresAt <= Date.now()) {
|
||||
storage.removeItem(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry;
|
||||
} catch {
|
||||
storage.removeItem(key);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function setPersistentCacheEntry(key: string, entry: TranslationCacheEntry): void {
|
||||
const storage = getStorage();
|
||||
if (!storage) return;
|
||||
|
||||
storage.setItem(key, JSON.stringify(entry));
|
||||
}
|
||||
|
||||
export function cleanupTranslationCache(): void {
|
||||
const storage = getStorage();
|
||||
if (!storage) return;
|
||||
|
||||
const lastCleanup = Number(storage.getItem(CLEANUP_MARKER_KEY) ?? "0");
|
||||
if (lastCleanup && Date.now() - lastCleanup < CACHE_CLEANUP_INTERVAL) {
|
||||
return;
|
||||
}
|
||||
|
||||
const translationKeys: string[] = [];
|
||||
|
||||
for (let index = 0; index < storage.length; index += 1) {
|
||||
const key = storage.key(index);
|
||||
if (key && key.startsWith(`${CACHE_PREFIX}:${CACHE_VERSION}:`)) {
|
||||
translationKeys.push(key);
|
||||
}
|
||||
}
|
||||
|
||||
const validEntries: Array<{ key: string; entry: TranslationCacheEntry }> = [];
|
||||
|
||||
for (const key of translationKeys) {
|
||||
const entry = getPersistentCacheEntry(key);
|
||||
if (entry) {
|
||||
validEntries.push({ key, entry });
|
||||
}
|
||||
}
|
||||
|
||||
validEntries
|
||||
.sort((a, b) => a.entry.createdAt - b.entry.createdAt)
|
||||
.slice(0, Math.max(0, validEntries.length - CACHE_MAX_ENTRIES))
|
||||
.forEach(({ key }) => storage.removeItem(key));
|
||||
|
||||
storage.setItem(CLEANUP_MARKER_KEY, String(Date.now()));
|
||||
}
|
||||
|
||||
function createPlaceholder(prefix: string, index: number): string {
|
||||
return `${INTERNAL_PLACEHOLDER_PREFIX}${prefix}${index}¤`;
|
||||
}
|
||||
|
||||
function escapeRegExp(value: string): string {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
export function collectProtectedTerms(terms?: Array<string | null | undefined>): string[] {
|
||||
const combined = [...DEFAULT_PROTECTED_TERMS, ...(terms ?? [])]
|
||||
.map((term) => String(term ?? "").trim())
|
||||
.filter(Boolean);
|
||||
|
||||
return Array.from(new Set(combined)).sort((a, b) => b.length - a.length);
|
||||
}
|
||||
|
||||
function protectMatches(
|
||||
input: string,
|
||||
pattern: RegExp,
|
||||
replacements: Map<string, string>,
|
||||
prefix: string,
|
||||
): string {
|
||||
return input.replace(pattern, (match) => {
|
||||
const placeholder = createPlaceholder(prefix, replacements.size);
|
||||
replacements.set(placeholder, match);
|
||||
return placeholder;
|
||||
});
|
||||
}
|
||||
|
||||
function protectExcludedHtmlBlocks(
|
||||
input: string,
|
||||
replacements: Map<string, string>,
|
||||
): string {
|
||||
if (typeof window === "undefined") return input;
|
||||
|
||||
const documentFragment = new DOMParser().parseFromString(input, "text/html");
|
||||
const nodes = Array.from(documentFragment.body.querySelectorAll(excludedTranslationNodes));
|
||||
|
||||
nodes.forEach((node) => {
|
||||
const placeholder = createPlaceholder("HTML", replacements.size);
|
||||
replacements.set(placeholder, node.outerHTML);
|
||||
node.replaceWith(documentFragment.createTextNode(placeholder));
|
||||
});
|
||||
|
||||
return documentFragment.body.innerHTML;
|
||||
}
|
||||
|
||||
export function protectTranslationTokens(
|
||||
input: string,
|
||||
options?: { isHtml?: boolean; protectedTerms?: string[] },
|
||||
): ProtectedTranslationTokens {
|
||||
const replacements = new Map<string, string>();
|
||||
let output = String(input ?? "");
|
||||
|
||||
if (options?.isHtml) {
|
||||
output = protectExcludedHtmlBlocks(output, replacements);
|
||||
}
|
||||
|
||||
output = protectMatches(
|
||||
output,
|
||||
/https?:\/\/[^\s"'<>]+/gi,
|
||||
replacements,
|
||||
"URL",
|
||||
);
|
||||
output = protectMatches(
|
||||
output,
|
||||
/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi,
|
||||
replacements,
|
||||
"EMAIL",
|
||||
);
|
||||
output = protectMatches(
|
||||
output,
|
||||
/\{\{[^{}]+\}\}|\{[a-zA-Z0-9_.-]+\}|%(\d+\$)?[sd]|:[a-zA-Z_][a-zA-Z0-9_]*/g,
|
||||
replacements,
|
||||
"TOKEN",
|
||||
);
|
||||
|
||||
for (const term of collectProtectedTerms(options?.protectedTerms)) {
|
||||
const escapedTerm = escapeRegExp(term);
|
||||
const pattern = new RegExp(`(^|[^\\p{L}\\p{N}])(${escapedTerm})(?=$|[^\\p{L}\\p{N}])`, "giu");
|
||||
|
||||
output = output.replace(pattern, (match, prefix, protectedValue) => {
|
||||
const placeholder = createPlaceholder("TERM", replacements.size);
|
||||
replacements.set(placeholder, protectedValue);
|
||||
return `${prefix}${placeholder}`;
|
||||
});
|
||||
}
|
||||
|
||||
return { text: output, replacements };
|
||||
}
|
||||
|
||||
export function restoreTranslationTokens(
|
||||
input: string,
|
||||
replacements: Map<string, string>,
|
||||
): string {
|
||||
let output = String(input ?? "");
|
||||
|
||||
for (const [placeholder, original] of replacements.entries()) {
|
||||
output = output.split(placeholder).join(original);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
export function getTranslationPayloadHealth(
|
||||
input: string,
|
||||
options?: { isHtml?: boolean; protectedTerms?: string[] },
|
||||
): TranslationPayloadHealth {
|
||||
const source = String(input ?? "");
|
||||
if (!source.trim()) {
|
||||
return {
|
||||
shouldTranslate: false,
|
||||
normalizedText: source,
|
||||
};
|
||||
}
|
||||
|
||||
if (options?.isHtml && typeof window !== "undefined") {
|
||||
const documentFragment = new DOMParser().parseFromString(source, "text/html");
|
||||
const walker = document.createTreeWalker(
|
||||
documentFragment.body,
|
||||
NodeFilter.SHOW_TEXT,
|
||||
{
|
||||
acceptNode(node) {
|
||||
return isSkippableTextNode(node as Text)
|
||||
? NodeFilter.FILTER_REJECT
|
||||
: NodeFilter.FILTER_ACCEPT;
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (!walker.nextNode()) {
|
||||
return {
|
||||
shouldTranslate: false,
|
||||
normalizedText: source,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const protectedPayload = protectTranslationTokens(source, options);
|
||||
const remainder = protectedPayload.text
|
||||
.replace(TRANSLATION_PLACEHOLDER_PATTERN, "")
|
||||
.trim();
|
||||
|
||||
if (!remainder) {
|
||||
return {
|
||||
shouldTranslate: false,
|
||||
normalizedText: source,
|
||||
};
|
||||
}
|
||||
|
||||
const hasLetters = /[\p{L}]/u.test(remainder);
|
||||
if (!hasLetters) {
|
||||
return {
|
||||
shouldTranslate: false,
|
||||
normalizedText: source,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
shouldTranslate: true,
|
||||
normalizedText: source,
|
||||
};
|
||||
}
|
||||
|
||||
export function sanitizeTranslatedHtml(html: string): string {
|
||||
if (typeof window === "undefined" || !html) return "";
|
||||
|
||||
const documentFragment = new DOMParser().parseFromString(String(html), "text/html");
|
||||
documentFragment.querySelectorAll(apiUnsafeNodes).forEach((node) => node.remove());
|
||||
|
||||
documentFragment.querySelectorAll("*").forEach((node) => {
|
||||
for (const attribute of Array.from(node.attributes)) {
|
||||
const name = attribute.name.toLowerCase();
|
||||
const value = attribute.value.trim().toLowerCase();
|
||||
|
||||
if (
|
||||
name.startsWith("on") ||
|
||||
value.startsWith("javascript:") ||
|
||||
value.startsWith("data:text/html") ||
|
||||
value.startsWith("vbscript:")
|
||||
) {
|
||||
node.removeAttribute(attribute.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return documentFragment.body.innerHTML;
|
||||
}
|
||||
|
||||
function isSkippableTextNode(node: Text): boolean {
|
||||
const parent = node.parentElement;
|
||||
const value = node.textContent ?? "";
|
||||
|
||||
if (!value.trim()) return true;
|
||||
if (!parent) return true;
|
||||
if (parent.closest(excludedTranslationNodes)) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function createBatchPayload(values: string[]): {
|
||||
payload: string;
|
||||
} {
|
||||
if (typeof window === "undefined") {
|
||||
return {
|
||||
payload: values.join(" "),
|
||||
};
|
||||
}
|
||||
|
||||
const documentFragment = document.implementation.createHTMLDocument("");
|
||||
const root = documentFragment.createElement("div");
|
||||
root.setAttribute(HTML_BATCH_ROOT_ATTR, "1");
|
||||
|
||||
values.forEach((value, index) => {
|
||||
const node = documentFragment.createElement("span");
|
||||
node.setAttribute(HTML_BATCH_NODE_ATTR, String(index));
|
||||
node.textContent = value;
|
||||
root.appendChild(node);
|
||||
});
|
||||
|
||||
documentFragment.body.appendChild(root);
|
||||
|
||||
return { payload: root.outerHTML };
|
||||
}
|
||||
|
||||
function parseBatchResponse(
|
||||
translated: string,
|
||||
originalValues: string[],
|
||||
): string[] {
|
||||
if (typeof window === "undefined") {
|
||||
return originalValues;
|
||||
}
|
||||
|
||||
const documentFragment = new DOMParser().parseFromString(translated, "text/html");
|
||||
const root = documentFragment.body.querySelector(`[${HTML_BATCH_ROOT_ATTR}]`);
|
||||
if (!root) {
|
||||
return originalValues;
|
||||
}
|
||||
|
||||
const nodes = Array.from(root.querySelectorAll(`[${HTML_BATCH_NODE_ATTR}]`));
|
||||
if (nodes.length !== originalValues.length) {
|
||||
return originalValues;
|
||||
}
|
||||
|
||||
return originalValues.map((originalValue, index) => {
|
||||
const node = nodes.find(
|
||||
(candidate) => candidate.getAttribute(HTML_BATCH_NODE_ATTR) === String(index),
|
||||
);
|
||||
|
||||
return node?.textContent?.trim() ? node.textContent : originalValue;
|
||||
});
|
||||
}
|
||||
|
||||
function buildTextBatches(nodes: Text[]): Text[][] {
|
||||
const batches: Text[][] = [];
|
||||
let currentBatch: Text[] = [];
|
||||
let currentLength = 0;
|
||||
|
||||
for (const node of nodes) {
|
||||
const textLength = (node.textContent ?? "").length;
|
||||
const shouldFlush =
|
||||
currentBatch.length >= BATCH_MAX_ITEMS ||
|
||||
currentLength + textLength > BATCH_MAX_CHARS;
|
||||
|
||||
if (shouldFlush && currentBatch.length > 0) {
|
||||
batches.push(currentBatch);
|
||||
currentBatch = [];
|
||||
currentLength = 0;
|
||||
}
|
||||
|
||||
currentBatch.push(node);
|
||||
currentLength += textLength;
|
||||
}
|
||||
|
||||
if (currentBatch.length > 0) {
|
||||
batches.push(currentBatch);
|
||||
}
|
||||
|
||||
return batches;
|
||||
}
|
||||
|
||||
export async function translateHtmlByTextNodes(params: {
|
||||
html: string;
|
||||
protectedTerms?: string[];
|
||||
translateText: (text: string, options?: { protectedTerms?: string[]; isHtml?: boolean }) => Promise<string>;
|
||||
}): Promise<string> {
|
||||
if (typeof window === "undefined" || !params.html.trim()) {
|
||||
return params.html;
|
||||
}
|
||||
|
||||
const documentFragment = new DOMParser().parseFromString(params.html, "text/html");
|
||||
const walker = document.createTreeWalker(
|
||||
documentFragment.body,
|
||||
NodeFilter.SHOW_TEXT,
|
||||
{
|
||||
acceptNode(node) {
|
||||
return isSkippableTextNode(node as Text)
|
||||
? NodeFilter.FILTER_REJECT
|
||||
: NodeFilter.FILTER_ACCEPT;
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const textNodes: Text[] = [];
|
||||
let currentNode = walker.nextNode();
|
||||
while (currentNode) {
|
||||
textNodes.push(currentNode as Text);
|
||||
currentNode = walker.nextNode();
|
||||
}
|
||||
|
||||
if (textNodes.length === 0) {
|
||||
return sanitizeTranslatedHtml(documentFragment.body.innerHTML);
|
||||
}
|
||||
|
||||
for (const batch of buildTextBatches(textNodes)) {
|
||||
const originalValues = batch.map((node) => node.textContent ?? "");
|
||||
const { payload } = createBatchPayload(originalValues);
|
||||
const translatedPayload = await params.translateText(payload, {
|
||||
protectedTerms: params.protectedTerms,
|
||||
isHtml: true,
|
||||
});
|
||||
|
||||
const translatedValues = parseBatchResponse(translatedPayload, originalValues);
|
||||
|
||||
batch.forEach((node, index) => {
|
||||
node.textContent = translatedValues[index];
|
||||
});
|
||||
}
|
||||
|
||||
return sanitizeTranslatedHtml(documentFragment.body.innerHTML);
|
||||
}
|
||||
Reference in New Issue
Block a user