From 2f3508ea23a9ce470fd6570235aa63e90e50df7f Mon Sep 17 00:00:00 2001 From: Bobby Date: Wed, 7 Jun 2023 22:45:06 -0400 Subject: Remove Middleware and Handle Translate Client Side --- static/css/styles.css | 5 +++++ static/js/tl.js | 43 ++++++++++++++++++++++++++++++++++++ templates/blog/partials/base.html | 28 +++++++++++++++-------- templates/blog/partials/sidebar.html | 4 +++- templates/blog/post.html | 8 +++---- thatcomputerscientist/settings.py | 2 +- 6 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 static/js/tl.js diff --git a/static/css/styles.css b/static/css/styles.css index 061c7deb..491780c7 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -3,6 +3,7 @@ @import url('https://fonts.googleapis.com/css2?family=Mali:ital,wght@0,400;0,700;1,400;1,700&display=swap'); body { + position: unset !important; width: 100%; background: black; font-size: 11px; @@ -13,6 +14,10 @@ body { box-sizing: border-box; } +.skiptranslate { + display: none !important; +} + input, textarea { font-family: 'Mali', sans-serif !important; diff --git a/static/js/tl.js b/static/js/tl.js new file mode 100644 index 00000000..98bd9529 --- /dev/null +++ b/static/js/tl.js @@ -0,0 +1,43 @@ +// Google Translate (Only English and Japanese) +function googleTranslateElementInit() { + new google.translate.TranslateElement( + { + pageLanguage: "en", + includedLanguages: "ja", + }, + "google_translate_element" + ); +} + +var currentLang = document.cookie.replace( + /(?:(?:^|.*;\s*)lang\s*\=\s*([^;]*).*$)|^.*$/, + "$1" +); + +console.log("currentLang: " + currentLang); + +if (currentLang == "ja") { + triggerTranslation("ja"); +} else { + triggerTranslation("en"); +} + +function triggerTranslation(language) { + var selectEl = document.querySelector("select.goog-te-combo"); + if (!selectEl) { + setTimeout(function () { + triggerTranslation(language); + }, 100); + } else if (!selectEl.options || selectEl.options.length === 0) { + setTimeout(function () { + triggerTranslation(language); + }, 100); + } else { + console.log("triggering translation"); + console.log(selectEl.options, selectEl.options.length); // HTMLOptionsCollection with options + + // Continue with the logic for handling the available options + selectEl.value = language; // Change the value of the select element + selectEl.dispatchEvent(new Event("change")); // Trigger change event + } +} diff --git a/templates/blog/partials/base.html b/templates/blog/partials/base.html index ddb12727..e194a6b6 100644 --- a/templates/blog/partials/base.html +++ b/templates/blog/partials/base.html @@ -62,6 +62,25 @@ white-space: nowrap; /* Prevent line breaks */ } + {% if request.COOKIES.lang == 'ja' %} + + + {% endif %} + + + + + {% if request.COOKIES.summonOneko == 'true' %} + + {% endif %} {% block scripts %} {% endblock %} + +