diff options
| author | Bobby <[email protected]> | 2023-06-16 18:20:20 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-06-16 18:20:20 -0400 |
| commit | 00dda0f1434eeb122515eedaf0538e92f58f1085 (patch) | |
| tree | 5eda4a0785314465b14c09fce3c538a61beadde0 /static | |
| parent | 7fece49eb0d3759ee015746a8c322c543b9a6e3c (diff) | |
| download | thatcomputerscientist-00dda0f1434eeb122515eedaf0538e92f58f1085.tar.xz thatcomputerscientist-00dda0f1434eeb122515eedaf0538e92f58f1085.zip | |
Instant Translation Support"
Diffstat (limited to 'static')
| -rw-r--r-- | static/js/globals.js | 7 | ||||
| -rw-r--r-- | static/js/tl.js | 53 |
2 files changed, 43 insertions, 17 deletions
diff --git a/static/js/globals.js b/static/js/globals.js index ad0f4518..9100251b 100644 --- a/static/js/globals.js +++ b/static/js/globals.js @@ -7,8 +7,11 @@ gtag("js", new Date()); gtag("config", "G-72XTC500FR"); function changeLang(lang) { - document.cookie = "lang=" + lang + "; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/"; - location.reload(); + if (lang === "ja") { + translateJapanese(); + } else { + restoreLang(); + } } // Smooth scroll to anchor diff --git a/static/js/tl.js b/static/js/tl.js index 7eaf14a2..4cfdb7ff 100644 --- a/static/js/tl.js +++ b/static/js/tl.js @@ -9,31 +9,54 @@ function googleTranslateElementInit() { ); } -var currentLang = document.cookie.replace( - /(?:(?:^|.*;\s*)lang\s*\=\s*([^;]*).*$)|^.*$/, - "$1" -); - -if (currentLang == "ja") { - triggerTranslation("ja"); -} else { - triggerTranslation("en"); +function restoreLang() { + localStorage.setItem("lang", "en"); + $('#tl_ja').hide(); + $('#tl_en').show(); + var translateContainers = $('iframe'); + if (translateContainers.length === 0) { + // nothing + } else { + translateContainers.each(function (index, element) { + if (element.contentWindow.document.getElementById(":1.restore")) { + console.log(element.contentWindow.document.getElementById(":1.restore")); + element.contentWindow.document.getElementById(":1.restore").click(); + } + }); + } } -function triggerTranslation(language) { +function translateJapanese() { + localStorage.setItem("lang", "ja"); + $('#tl_en').hide(); + $('#tl_ja').show(); var selectEl = document.querySelector("select.goog-te-combo"); + console.log(selectEl); if (!selectEl) { setTimeout(function () { - triggerTranslation(language); + translateJapanese(); }, 10); } else if (!selectEl.options || selectEl.options.length === 0) { setTimeout(function () { - triggerTranslation(language); + translateJapanese(); }, 10); } else { - selectEl.value = language; + selectEl.value = 'ja'; selectEl.dispatchEvent(new Event("change")); - // visiblity of #main-section is hidden until translation is done, show it after translation is done - document.getElementById("main-section").style.visibility = "visible"; } } + +// init +var currentLang = localStorage.getItem("lang"); +if (!currentLang) { + currentLang = "en"; + localStorage.setItem("lang", "en"); +} +if (currentLang === "ja") { + $('#tl_en').hide(); + $('#tl_ja').show(); +} else { + $('#tl_ja').hide(); + $('#tl_en').show(); + restoreLang(); +} |
