SD-online/Fooocus-Docker
2
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, a = [], walk = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, null, false);12 while ((n = walk.nextNode())) a.push(n);13 return a;14}15 16function canBeTranslated(node, text) {17 if (!text) return false;18 if (!node.parentElement) return false;19 var parentType = node.parentElement.nodeName;20 if (parentType == 'SCRIPT' || parentType == 'STYLE' || parentType == 'TEXTAREA') return false;21 if (re_num.test(text)) return false;22 return true;23}24 25function getTranslation(text) {26 if (!text) return undefined;27 28 if (translated_lines[text] === undefined) {29 original_lines[text] = 1;30 }31 32 var tl = localization[text];33 if (tl !== undefined) {34 translated_lines[tl] = 1;35 }36 37 return tl;38}39 40function processTextNode(node) {41 var text = node.textContent.trim();42 43 if (!canBeTranslated(node, text)) return;44 45 var tl = getTranslation(text);46 if (tl !== undefined) {47 node.textContent = tl;48 if (text && node.parentElement) {49 node.parentElement.setAttribute("data-original-text", text);50 }51 }52}53 54function processNode(node) {55 if (node.nodeType == 3) {56 processTextNode(node);57 return;58 }59 60 if (node.title) {61 let tl = getTranslation(node.title);62 if (tl !== undefined) {63 node.title = tl;64 }65 }66 67 if (node.placeholder) {68 let tl = getTranslation(node.placeholder);69 if (tl !== undefined) {70 node.placeholder = tl;71 }72 }73 74 textNodesUnder(node).forEach(function(node) {75 processTextNode(node);76 });77}78 79function refresh_style_localization() {80 processNode(document.querySelector('.style_selections'));81}82 83function localizeWholePage() {84 processNode(gradioApp());85 86 function elem(comp) {87 var elem_id = comp.props.elem_id ? comp.props.elem_id : "component-" + comp.id;88 return gradioApp().getElementById(elem_id);89 }90 91 for (var comp of window.gradio_config.components) {92 if (comp.props.webui_tooltip) {93 let e = elem(comp);94 95 let tl = e ? getTranslation(e.title) : undefined;96 if (tl !== undefined) {97 e.title = tl;98 }99 }100 if (comp.props.placeholder) {101 let e = elem(comp);102 let textbox = e ? e.querySelector('[placeholder]') : null;103 104 let tl = textbox ? getTranslation(textbox.placeholder) : undefined;105 if (tl !== undefined) {106 textbox.placeholder = tl;107 }108 }109 }110}111 112document.addEventListener("DOMContentLoaded", function() {113 if (!hasLocalization()) {114 return;115 }116 117 onUiUpdate(function(m) {118 m.forEach(function(mutation) {119 mutation.addedNodes.forEach(function(node) {120 processNode(node);121 });122 });123 });124 125 localizeWholePage();126 127 if (localization.rtl) { // if the language is from right to left,128 (new MutationObserver((mutations, observer) => { // wait for the style to load129 mutations.forEach(mutation => {130 mutation.addedNodes.forEach(node => {131 if (node.tagName === 'STYLE') {132 observer.disconnect();133 134 for (const x of node.sheet.rules) { // find all rtl media rules135 if (Array.from(x.media || []).includes('rtl')) {136 x.media.appendMedium('all'); // enable them137 }138 }139 }140 });141 });142 })).observe(gradioApp(), {childList: true});143 }144});145 