From 8ccc27127bd5abcba05f30f8a72fc37025b588ac Mon Sep 17 00:00:00 2001
From: Aarni Koskela <akx@iki.fi>
Date: Sun, 30 Apr 2023 22:08:52 +0300
Subject: Fix a whole bunch of implicit globals

---
 javascript/localization.js | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

(limited to 'javascript/localization.js')

diff --git a/javascript/localization.js b/javascript/localization.js
index 1a5a1dbb..e1ffa271 100644
--- a/javascript/localization.js
+++ b/javascript/localization.js
@@ -35,11 +35,11 @@ function canBeTranslated(node, text){
     if(! text) return false;
     if(! node.parentElement) return false;
 
-    parentType = node.parentElement.nodeName
+    var parentType = node.parentElement.nodeName
     if(parentType=='SCRIPT' || parentType=='STYLE' || parentType=='TEXTAREA') return false;
 
     if (parentType=='OPTION' || parentType=='SPAN'){
-        pnode = node
+        var pnode = node
         for(var level=0; level<4; level++){
             pnode = pnode.parentElement
             if(! pnode) break;
@@ -69,7 +69,7 @@ function getTranslation(text){
 }
 
 function processTextNode(node){
-    text = node.textContent.trim()
+    var text = node.textContent.trim()
 
     if(! canBeTranslated(node, text)) return
 
@@ -105,7 +105,7 @@ function processNode(node){
 }
 
 function dumpTranslations(){
-    dumped = {}
+    var dumped = {}
     if (localization.rtl) {
         dumped.rtl = true
     }
@@ -151,7 +151,7 @@ document.addEventListener("DOMContentLoaded", function() {
 })
 
 function download_localization() {
-    text = JSON.stringify(dumpTranslations(), null, 4)
+    var text = JSON.stringify(dumpTranslations(), null, 4)
 
     var element = document.createElement('a');
     element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
-- 
cgit v1.2.3


From 16f0739db022de6b49b6572d565f1c72e72dc3a7 Mon Sep 17 00:00:00 2001
From: Aarni Koskela <akx@iki.fi>
Date: Sun, 30 Apr 2023 23:01:39 +0300
Subject: Make localization.js do nothing if there's no localization to do

---
 javascript/localization.js | 68 +++++++++++++++++++++++++---------------------
 1 file changed, 37 insertions(+), 31 deletions(-)

(limited to 'javascript/localization.js')

diff --git a/javascript/localization.js b/javascript/localization.js
index e1ffa271..0123b877 100644
--- a/javascript/localization.js
+++ b/javascript/localization.js
@@ -25,6 +25,10 @@ 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);
@@ -119,37 +123,6 @@ function dumpTranslations(){
     return dumped
 }
 
-onUiUpdate(function(m){
-    m.forEach(function(mutation){
-        mutation.addedNodes.forEach(function(node){
-            processNode(node)
-        })
-    });
-})
-
-
-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();
-
-                        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 });
-    }
-})
-
 function download_localization() {
     var text = JSON.stringify(dumpTranslations(), null, 4)
 
@@ -163,3 +136,36 @@ function download_localization() {
 
     document.body.removeChild(element);
 }
+
+if(hasLocalization()) {
+    onUiUpdate(function (m) {
+        m.forEach(function (mutation) {
+            mutation.addedNodes.forEach(function (node) {
+                processNode(node)
+            })
+        });
+    })
+
+
+    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();
+
+                            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.3


From 999a03e4a770217395b5eb8f8165ed0d8ebe5656 Mon Sep 17 00:00:00 2001
From: Aarni Koskela <akx@iki.fi>
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.3


From cd6990c243e926672ff84e7db1ca34ae60015486 Mon Sep 17 00:00:00 2001
From: Aarni Koskela <akx@iki.fi>
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.3