lenML/ChatTTS-Forge
301
1var re_num = /^[.\d]+$/;2 3var original_lines = {};4var translated_lines = {};5 6function hasLocalization() {7 return window.localization && Object.keys(window.localization).length > 0;8}9 10function textNodesUnder(el) {11 var n,12 a = [],13 walk = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, null, false);14 while ((n = walk.nextNode())) a.push(n);15 return a;16}17 18function canBeTranslated(node, text) {19 if (!text) return false;20 if (!node.parentElement) return false;21 var parentType = node.parentElement.nodeName;22 if (23 parentType == "SCRIPT" ||24 parentType == "STYLE" ||25 parentType == "TEXTAREA"26 )27 return false;28 if (re_num.test(text)) return false;29 return true;30}31 32function getTranslation(text) {33 if (!text) return undefined;34 35 if (translated_lines[text] === undefined) {36 original_lines[text] = 1;37 }38 39 var tl = localization[text];40 if (tl !== undefined) {41 translated_lines[tl] = 1;42 }43 44 return tl;45}46 47function processTextNode(node) {48 var text = node.textContent.trim();49 50 if (!canBeTranslated(node, text)) return;51 52 var tl = getTranslation(text);53 if (tl !== undefined) {54 node.textContent = tl;55 if (text && node.parentElement) {56 node.parentElement.setAttribute("data-original-text", text);57 }58 }59}60 61/**62 *63 * @param {HTMLElement} node64 * @returns65 */66function processMDNode(node) {67 const text = node.children[0].textContent.trim();68 let tl = getTranslation(text);69 70 if (!tl) return;71 if (Array.isArray(tl)) {72 tl = tl.join("\n");73 }74 const md = marked.marked(tl);75 node.innerHTML = md;76 77 node.setAttribute("data-original-text", text);78}79 80function is_md_child(node) {81 while (node.parentElement !== document.body) {82 if (node?.classList?.contains("md")) {83 return true;84 }85 node = node.parentElement;86 if (!node) break;87 }88 return false;89}90 91function processNode(node) {92 if (node.nodeType == 3) {93 processTextNode(node);94 return;95 }96 if (node.classList.contains("md")) {97 processMDNode(node);98 return;99 }100 if (is_md_child(node)) return;101 102 if (node.title) {103 let tl = getTranslation(node.title);104 if (tl !== undefined) {105 node.title = tl;106 }107 }108 109 if (node.placeholder) {110 let tl = getTranslation(node.placeholder);111 if (tl !== undefined) {112 node.placeholder = tl;113 }114 }115 116 textNodesUnder(node).forEach(function (node) {117 if (is_md_child(node)) return;118 processTextNode(node);119 });120}121 122function refresh_style_localization() {123 processNode(document.querySelector(".style_selections"));124}125 126function refresh_aspect_ratios_label(value) {127 label = document.querySelector("#aspect_ratios_accordion div span");128 translation = getTranslation("Aspect Ratios");129 if (typeof translation == "undefined") {130 translation = "Aspect Ratios";131 }132 label.textContent = translation + " " + htmlDecode(value);133}134 135function localizeWholePage() {136 processNode(gradioApp());137 138 function elem(comp) {139 var elem_id = comp.props.elem_id140 ? comp.props.elem_id141 : "component-" + comp.id;142 return gradioApp().getElementById(elem_id);143 }144 145 for (var comp of window.gradio_config.components) {146 if (comp.props.webui_tooltip) {147 let e = elem(comp);148 149 let tl = e ? getTranslation(e.title) : undefined;150 if (tl !== undefined) {151 e.title = tl;152 }153 }154 if (comp.props.placeholder) {155 let e = elem(comp);156 let textbox = e ? e.querySelector("[placeholder]") : null;157 158 let tl = textbox ? getTranslation(textbox.placeholder) : undefined;159 if (tl !== undefined) {160 textbox.placeholder = tl;161 }162 }163 }164}165 166/**167 *168 * @param {HTMLElement} node169 */170function isNeedTranslate(node) {171 if (!node) return false;172 if (!(node instanceof HTMLElement)) return true;173 while (node.parentElement !== document.body) {174 if (node.classList.contains("no-translate")) {175 return false;176 }177 node = node.parentElement;178 if (!node) break;179 }180 return true;181}182 183document.addEventListener("DOMContentLoaded", function () {184 if (!hasLocalization()) {185 return;186 }187 188 onUiUpdate(function (m) {189 m.forEach(function (mutation) {190 Array.from(mutation.addedNodes)191 .filter(isNeedTranslate)192 .forEach(function (node) {193 processNode(node);194 });195 });196 });197 198 localizeWholePage();199 200 if (localization.rtl) {201 // if the language is from right to left,202 new MutationObserver((mutations, observer) => {203 // wait for the style to load204 mutations.forEach((mutation) => {205 mutation.addedNodes.forEach((node) => {206 if (node.tagName === "STYLE") {207 observer.disconnect();208 209 for (const x of node.sheet.rules) {210 // find all rtl media rules211 if (Array.from(x.media || []).includes("rtl")) {212 x.media.appendMedium("all"); // enable them213 }214 }215 }216 });217 });218 }).observe(gradioApp(), { childList: true });219 }220});221 