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