From 999a03e4a770217395b5eb8f8165ed0d8ebe5656 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sat, 13 May 2023 15:10:35 +0300 Subject: Wait for DOMContentLoaded until checking whether localization should be disabled Refs https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/9955#issuecomment-1546587143 --- javascript/localization.js | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'javascript/localization.js') diff --git a/javascript/localization.js b/javascript/localization.js index 0123b877..d0bdfc16 100644 --- a/javascript/localization.js +++ b/javascript/localization.js @@ -137,7 +137,11 @@ function download_localization() { document.body.removeChild(element); } -if(hasLocalization()) { +document.addEventListener("DOMContentLoaded", function () { + if (!hasLocalization()) { + return; + } + onUiUpdate(function (m) { m.forEach(function (mutation) { mutation.addedNodes.forEach(function (node) { @@ -146,26 +150,23 @@ if(hasLocalization()) { }); }) + processNode(gradioApp()) - document.addEventListener("DOMContentLoaded", function () { - processNode(gradioApp()) + 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(); - 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 - } + 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 }); - } - }) -} + } + }) + }); + })).observe(gradioApp(), { childList: true }); + } +}) -- cgit v1.2.1 From cd6990c243e926672ff84e7db1ca34ae60015486 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sat, 13 May 2023 19:22:39 +0300 Subject: Make dump translations work again --- javascript/localization.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'javascript/localization.js') diff --git a/javascript/localization.js b/javascript/localization.js index d0bdfc16..86e5ca67 100644 --- a/javascript/localization.js +++ b/javascript/localization.js @@ -109,18 +109,23 @@ function processNode(node){ } function dumpTranslations(){ + if(!hasLocalization()) { + // If we don't have any localization, + // we will not have traversed the app to find + // original_lines, so do that now. + processNode(gradioApp()); + } var dumped = {} if (localization.rtl) { - dumped.rtl = true + dumped.rtl = true; } - Object.keys(original_lines).forEach(function(text){ - if(dumped[text] !== undefined) return - - dumped[text] = localization[text] || text - }) + for (const text in original_lines) { + if(dumped[text] !== undefined) continue; + dumped[text] = localization[text] || text; + } - return dumped + return dumped; } function download_localization() { -- cgit v1.2.1 From 9c54b78d9dde5601e916f308d9a9d6953ec39430 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 17 May 2023 15:46:58 +0300 Subject: Run `eslint --fix` (and normalize tabs to spaces) --- javascript/localization.js | 354 ++++++++++++++++++++++----------------------- 1 file changed, 177 insertions(+), 177 deletions(-) (limited to 'javascript/localization.js') diff --git a/javascript/localization.js b/javascript/localization.js index 86e5ca67..3d043a9a 100644 --- a/javascript/localization.js +++ b/javascript/localization.js @@ -1,177 +1,177 @@ - -// localization = {} -- the dict with translations is created by the backend - -ignore_ids_for_localization={ - setting_sd_hypernetwork: 'OPTION', - setting_sd_model_checkpoint: 'OPTION', - setting_realesrgan_enabled_models: 'OPTION', - modelmerger_primary_model_name: 'OPTION', - modelmerger_secondary_model_name: 'OPTION', - modelmerger_tertiary_model_name: 'OPTION', - train_embedding: 'OPTION', - train_hypernetwork: 'OPTION', - txt2img_styles: 'OPTION', - img2img_styles: 'OPTION', - setting_random_artist_categories: 'SPAN', - setting_face_restoration_model: 'SPAN', - setting_realesrgan_enabled_models: 'SPAN', - extras_upscaler_1: 'SPAN', - extras_upscaler_2: 'SPAN', -} - -re_num = /^[\.\d]+$/ -re_emoji = /[\p{Extended_Pictographic}\u{1F3FB}-\u{1F3FF}\u{1F9B0}-\u{1F9B3}]/u - -original_lines = {} -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 (parentType=='OPTION' || parentType=='SPAN'){ - var pnode = node - for(var level=0; level<4; level++){ - pnode = pnode.parentElement - if(! pnode) break; - - if(ignore_ids_for_localization[pnode.id] == parentType) return false; - } - } - - if(re_num.test(text)) return false; - if(re_emoji.test(text)) return false; - return true -} - -function getTranslation(text){ - if(! text) return undefined - - if(translated_lines[text] === undefined){ - original_lines[text] = 1 - } - - 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 - - tl = getTranslation(text) - if(tl !== undefined){ - node.textContent = tl - } -} - -function processNode(node){ - if(node.nodeType == 3){ - processTextNode(node) - return - } - - if(node.title){ - tl = getTranslation(node.title) - if(tl !== undefined){ - node.title = tl - } - } - - if(node.placeholder){ - tl = getTranslation(node.placeholder) - if(tl !== undefined){ - node.placeholder = tl - } - } - - textNodesUnder(node).forEach(function(node){ - processTextNode(node) - }) -} - -function dumpTranslations(){ - if(!hasLocalization()) { - // If we don't have any localization, - // we will not have traversed the app to find - // original_lines, so do that now. - processNode(gradioApp()); - } - var dumped = {} - if (localization.rtl) { - dumped.rtl = true; - } - - for (const text in original_lines) { - if(dumped[text] !== undefined) continue; - dumped[text] = localization[text] || text; - } - - return dumped; -} - -function download_localization() { - var text = JSON.stringify(dumpTranslations(), null, 4) - - 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); - - element.click(); - - 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) - }) - }); - }) - - processNode(gradioApp()) - - 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 }); - } -}) + +// localization = {} -- the dict with translations is created by the backend + +ignore_ids_for_localization = { + setting_sd_hypernetwork: 'OPTION', + setting_sd_model_checkpoint: 'OPTION', + setting_realesrgan_enabled_models: 'OPTION', + modelmerger_primary_model_name: 'OPTION', + modelmerger_secondary_model_name: 'OPTION', + modelmerger_tertiary_model_name: 'OPTION', + train_embedding: 'OPTION', + train_hypernetwork: 'OPTION', + txt2img_styles: 'OPTION', + img2img_styles: 'OPTION', + setting_random_artist_categories: 'SPAN', + setting_face_restoration_model: 'SPAN', + setting_realesrgan_enabled_models: 'SPAN', + extras_upscaler_1: 'SPAN', + extras_upscaler_2: 'SPAN', +}; + +re_num = /^[\.\d]+$/; +re_emoji = /[\p{Extended_Pictographic}\u{1F3FB}-\u{1F3FF}\u{1F9B0}-\u{1F9B3}]/u; + +original_lines = {}; +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 (parentType == 'OPTION' || parentType == 'SPAN') { + var pnode = node; + for (var level = 0; level < 4; level++) { + pnode = pnode.parentElement; + if (!pnode) break; + + if (ignore_ids_for_localization[pnode.id] == parentType) return false; + } + } + + if (re_num.test(text)) return false; + if (re_emoji.test(text)) return false; + return true; +} + +function getTranslation(text) { + if (!text) return undefined; + + if (translated_lines[text] === undefined) { + original_lines[text] = 1; + } + + 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; + + tl = getTranslation(text); + if (tl !== undefined) { + node.textContent = tl; + } +} + +function processNode(node) { + if (node.nodeType == 3) { + processTextNode(node); + return; + } + + if (node.title) { + tl = getTranslation(node.title); + if (tl !== undefined) { + node.title = tl; + } + } + + if (node.placeholder) { + tl = getTranslation(node.placeholder); + if (tl !== undefined) { + node.placeholder = tl; + } + } + + textNodesUnder(node).forEach(function(node) { + processTextNode(node); + }); +} + +function dumpTranslations() { + if (!hasLocalization()) { + // If we don't have any localization, + // we will not have traversed the app to find + // original_lines, so do that now. + processNode(gradioApp()); + } + var dumped = {}; + if (localization.rtl) { + dumped.rtl = true; + } + + for (const text in original_lines) { + if (dumped[text] !== undefined) continue; + dumped[text] = localization[text] || text; + } + + return dumped; +} + +function download_localization() { + var text = JSON.stringify(dumpTranslations(), null, 4); + + 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); + + element.click(); + + 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); + }); + }); + }); + + processNode(gradioApp()); + + 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 }); + } +}); -- cgit v1.2.1 From 57b75f4a037658c1122aa092d1775ac52036b2cf Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Thu, 18 May 2023 09:59:10 +0300 Subject: eslint related file edits --- javascript/localization.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'javascript/localization.js') diff --git a/javascript/localization.js b/javascript/localization.js index 3d043a9a..eb22b8a7 100644 --- a/javascript/localization.js +++ b/javascript/localization.js @@ -1,10 +1,9 @@ // localization = {} -- the dict with translations is created by the backend -ignore_ids_for_localization = { +var ignore_ids_for_localization = { setting_sd_hypernetwork: 'OPTION', setting_sd_model_checkpoint: 'OPTION', - setting_realesrgan_enabled_models: 'OPTION', modelmerger_primary_model_name: 'OPTION', modelmerger_secondary_model_name: 'OPTION', modelmerger_tertiary_model_name: 'OPTION', @@ -19,11 +18,11 @@ ignore_ids_for_localization = { extras_upscaler_2: 'SPAN', }; -re_num = /^[\.\d]+$/; -re_emoji = /[\p{Extended_Pictographic}\u{1F3FB}-\u{1F3FF}\u{1F9B0}-\u{1F9B3}]/u; +var re_num = /^[.\d]+$/; +var re_emoji = /[\p{Extended_Pictographic}\u{1F3FB}-\u{1F3FF}\u{1F9B0}-\u{1F9B3}]/u; -original_lines = {}; -translated_lines = {}; +var original_lines = {}; +var translated_lines = {}; function hasLocalization() { return window.localization && Object.keys(window.localization).length > 0; @@ -31,7 +30,7 @@ function hasLocalization() { function textNodesUnder(el) { var n, a = [], walk = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, null, false); - while (n = walk.nextNode()) a.push(n); + while ((n = walk.nextNode())) a.push(n); return a; } @@ -64,7 +63,7 @@ function getTranslation(text) { original_lines[text] = 1; } - tl = localization[text]; + var tl = localization[text]; if (tl !== undefined) { translated_lines[tl] = 1; } @@ -77,7 +76,7 @@ function processTextNode(node) { if (!canBeTranslated(node, text)) return; - tl = getTranslation(text); + var tl = getTranslation(text); if (tl !== undefined) { node.textContent = tl; } @@ -90,14 +89,14 @@ function processNode(node) { } if (node.title) { - tl = getTranslation(node.title); + let tl = getTranslation(node.title); if (tl !== undefined) { node.title = tl; } } if (node.placeholder) { - tl = getTranslation(node.placeholder); + let tl = getTranslation(node.placeholder); if (tl !== undefined) { node.placeholder = tl; } @@ -157,21 +156,21 @@ document.addEventListener("DOMContentLoaded", function() { processNode(gradioApp()); - if (localization.rtl) { // if the language is from right to left, + 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 + 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 + x.media.appendMedium('all'); // enable them } } } }); }); - })).observe(gradioApp(), { childList: true }); + })).observe(gradioApp(), {childList: true}); } }); -- cgit v1.2.1