stable-diffusion-webui/javascript/localization.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

211 lines
5.6 KiB
JavaScript
Raw Normal View History

2022-10-18 02:15:32 +08:00
// localization = {} -- the dict with translations is created by the backend
2023-05-18 14:59:10 +08:00
var ignore_ids_for_localization = {
2022-10-18 02:15:32 +08:00
setting_sd_hypernetwork: 'OPTION',
setting_sd_model_checkpoint: 'OPTION',
modelmerger_primary_model_name: 'OPTION',
modelmerger_secondary_model_name: 'OPTION',
modelmerger_tertiary_model_name: 'OPTION',
train_embedding: 'OPTION',
train_hypernetwork: 'OPTION',
2023-01-14 19:56:39 +08:00
txt2img_styles: 'OPTION',
img2img_styles: 'OPTION',
setting_random_artist_categories: 'OPTION',
setting_face_restoration_model: 'OPTION',
setting_realesrgan_enabled_models: 'OPTION',
extras_upscaler_1: 'OPTION',
extras_upscaler_2: 'OPTION',
2022-10-18 02:15:32 +08:00
};
2023-05-18 14:59:10 +08:00
var re_num = /^[.\d]+$/;
var re_emoji = /[\p{Extended_Pictographic}\u{1F3FB}-\u{1F3FF}\u{1F9B0}-\u{1F9B3}]/u;
2023-05-18 14:59:10 +08:00
var original_lines = {};
var translated_lines = {};
function hasLocalization() {
return window.localization && Object.keys(window.localization).length > 0;
}
2022-10-18 02:15:32 +08:00
function textNodesUnder(el) {
var n, a = [], walk = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, null, false);
2023-05-18 14:59:10 +08:00
while ((n = walk.nextNode())) a.push(n);
2022-10-18 02:15:32 +08:00
return a;
}
2022-10-18 02:15:32 +08:00
function canBeTranslated(node, text) {
if (!text) return false;
if (!node.parentElement) return false;
2023-05-01 03:08:52 +08:00
var parentType = node.parentElement.nodeName;
2022-10-18 02:15:32 +08:00
if (parentType == 'SCRIPT' || parentType == 'STYLE' || parentType == 'TEXTAREA') return false;
2022-10-18 02:15:32 +08:00
if (parentType == 'OPTION' || parentType == 'SPAN') {
2023-05-01 03:08:52 +08:00
var pnode = node;
2022-10-18 02:15:32 +08:00
for (var level = 0; level < 4; level++) {
pnode = pnode.parentElement;
if (!pnode) break;
2022-10-18 02:15:32 +08:00
if (ignore_ids_for_localization[pnode.id] == parentType) return false;
}
}
2022-10-18 02:15:32 +08:00
if (re_num.test(text)) return false;
if (re_emoji.test(text)) return false;
return true;
}
2022-10-18 02:15:32 +08:00
function getTranslation(text) {
if (!text) return undefined;
2022-10-18 02:15:32 +08:00
if (translated_lines[text] === undefined) {
original_lines[text] = 1;
}
2023-05-18 14:59:10 +08:00
var tl = localization[text];
2022-10-18 02:15:32 +08:00
if (tl !== undefined) {
translated_lines[tl] = 1;
}
2022-10-18 02:15:32 +08:00
return tl;
}
2022-10-18 02:15:32 +08:00
function processTextNode(node) {
2023-05-01 03:08:52 +08:00
var text = node.textContent.trim();
2022-10-18 02:15:32 +08:00
if (!canBeTranslated(node, text)) return;
2023-05-18 14:59:10 +08:00
var tl = getTranslation(text);
2022-10-18 02:15:32 +08:00
if (tl !== undefined) {
node.textContent = tl;
}
}
2022-10-18 02:15:32 +08:00
function processNode(node) {
if (node.nodeType == 3) {
processTextNode(node);
return;
}
2022-10-18 02:15:32 +08:00
if (node.title) {
2023-05-18 14:59:10 +08:00
let tl = getTranslation(node.title);
2022-10-18 02:15:32 +08:00
if (tl !== undefined) {
node.title = tl;
}
}
2022-10-18 02:15:32 +08:00
if (node.placeholder) {
2023-05-18 14:59:10 +08:00
let tl = getTranslation(node.placeholder);
2022-10-18 02:15:32 +08:00
if (tl !== undefined) {
node.placeholder = tl;
}
}
2022-10-18 02:15:32 +08:00
textNodesUnder(node).forEach(function(node) {
processTextNode(node);
});
}
function localizeWholePage(){
processNode(gradioApp());
function elem(comp) {
if(comp.props.elem_id) {
elem_id = comp.props.elem_id;
} else{
elem_id = "component-" + comp.id;
}
return gradioApp().getElementById(elem_id);
}
for(comp of window.gradio_config.components) {
if(comp.props.webui_tooltip) {
var e = elem(comp);
var tl = e ? getTranslation(e.title) : undefined;
if (tl !== undefined) {
e.title = tl;
}
}
if(comp.props.placeholder) {
var e = elem(comp);
var textbox = e ? e.querySelector('[placeholder]') : null;
var tl = textbox ? getTranslation(textbox.placeholder) : undefined;
if (tl !== undefined) {
textbox.placeholder = tl;
}
}
}
}
2022-10-18 02:15:32 +08:00
function dumpTranslations() {
2023-05-14 00:22:39 +08:00
if (!hasLocalization()) {
// If we don't have any localization,
// we will not have traversed the app to find
// original_lines, so do that now.
localizeWholePage();
2023-05-14 00:22:39 +08:00
}
2023-05-01 03:08:52 +08:00
var dumped = {};
2022-10-25 11:55:17 +08:00
if (localization.rtl) {
2023-05-14 00:22:39 +08:00
dumped.rtl = true;
2022-10-25 11:55:17 +08:00
}
2023-05-14 00:22:39 +08:00
for (const text in original_lines) {
if (dumped[text] !== undefined) continue;
dumped[text] = localization[text] || text;
}
2023-05-14 00:22:39 +08:00
return dumped;
2022-10-18 02:15:32 +08:00
}
2022-10-18 02:15:32 +08:00
function download_localization() {
2023-05-01 03:08:52 +08:00
var text = JSON.stringify(dumpTranslations(), null, 4);
2022-10-18 02:15:32 +08:00
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', "localization.json");
element.style.display = 'none';
document.body.appendChild(element);
2022-10-18 02:15:32 +08:00
element.click();
2022-10-18 02:15:32 +08:00
document.body.removeChild(element);
}
document.addEventListener("DOMContentLoaded", function() {
if (!hasLocalization()) {
return;
}
onUiUpdate(function(m) {
m.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
processNode(node);
});
});
});
localizeWholePage();
2023-05-18 14:59:10 +08:00
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();
2023-05-18 14:59:10 +08:00
for (const x of node.sheet.rules) { // find all rtl media rules
if (Array.from(x.media || []).includes('rtl')) {
2023-05-18 14:59:10 +08:00
x.media.appendMedium('all'); // enable them
}
}
}
});
});
2023-05-18 14:59:10 +08:00
})).observe(gradioApp(), {childList: true});
}
});