|
| 1 | +{{/* cSpell:ignore goog |
| 2 | +<!-- prettier-ignore --> |
| 3 | +*/ -}} |
| 4 | +{{ $pageProseLang := partial "i18n/lang.html" . -}} |
| 5 | +{{ $siteLang := .Site.Language.Lang -}} |
| 6 | +{{- if ne $siteLang $pageProseLang -}} |
| 7 | + |
| 8 | +<script type="text/javascript"> |
| 9 | + const infoToConsole = false; |
| 10 | + const consoleInfo = (msg) => { |
| 11 | + if (infoToConsole) console.log(msg); |
| 12 | + }; |
| 13 | + |
| 14 | + const gtContainerID = 'google_translate_element'; |
| 15 | + const siteLang = '{{ $siteLang }}'; |
| 16 | + let translationBarSeen = false; |
| 17 | + |
| 18 | + const getGtSelectElt = () => document.querySelector('select.goog-te-combo'); |
| 19 | + |
| 20 | + function waitForGtSelectElt(siteLang) { |
| 21 | + const observer = new MutationObserver((mutations, obs) => { |
| 22 | + if (!getGtSelectElt()) return; |
| 23 | + obs.disconnect(); // Stop observing |
| 24 | + observeGoogleTranslateBar(); |
| 25 | + }); |
| 26 | + const elt = document.querySelector(`#${gtContainerID}`) || document.body; |
| 27 | + observer.observe(elt, { childList: true, subtree: true }); |
| 28 | + } |
| 29 | + |
| 30 | + function hasVisibleTranslationBar() { |
| 31 | + let iframe = document.querySelector('.skiptranslate > iframe'); |
| 32 | + let translateBar = iframe && iframe.parentElement; |
| 33 | + if (!translateBar) return false; |
| 34 | + |
| 35 | + let computedStyle = window.getComputedStyle(translateBar); |
| 36 | + let style = computedStyle.display; |
| 37 | + consoleInfo(`hVTB: style is '${style}'`); |
| 38 | + if (!style || style === 'none') return false; |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + function adjustNavbarPosition() { |
| 43 | + if (hasVisibleTranslationBar()) { |
| 44 | + const extraMargin = '40px'; // TODO: declare modifier style in SCSS file instead |
| 45 | + const navBarElt = document.querySelector('.td-navbar'); |
| 46 | + if (navBarElt.style.marginTop === extraMargin) return; |
| 47 | + navBarElt.style.marginTop = extraMargin; // Position navbar bellow GT banner |
| 48 | + consoleInfo( |
| 49 | + `adjustNavbarPos: GT banner present, navbar lowered by ${extraMargin}`, |
| 50 | + ); |
| 51 | + translationBarSeen = true; |
| 52 | + } else if (translationBarSeen) { |
| 53 | + translationBarSeen = false; |
| 54 | + let navBarElt = document.querySelector('.td-navbar'); |
| 55 | + navBarElt.style.marginTop = ''; // Reset position if GT banner is gone |
| 56 | + consoleInfo(`adjustNavbarPos: GT bar is gone; navbar style reset`); |
| 57 | + } else { |
| 58 | + consoleInfo(`adjustNavbarPos: no action`); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + function observeGoogleTranslateBar() { |
| 63 | + const observer = new MutationObserver((mutations, obs) => |
| 64 | + adjustNavbarPosition(), |
| 65 | + ); |
| 66 | + observer.observe(document.body, { childList: true, subtree: true }); |
| 67 | + } |
| 68 | + |
| 69 | + window.onload = function () { |
| 70 | + consoleInfo(`Doc loaded`); |
| 71 | + translationBarSeen = false; |
| 72 | + waitForGtSelectElt(siteLang); |
| 73 | + }; |
| 74 | + |
| 75 | + function googleTranslateElementInit() { |
| 76 | + const pageLanguage = '{{ $pageProseLang }}'; |
| 77 | + // Languages codes supported by Google for the locales in OTel.io site |
| 78 | + let allLangArr = ['en', 'es', 'fr', 'ja', 'pt', 'zh-cn']; |
| 79 | + const allLangButPageLang = allLangArr |
| 80 | + .filter((lang) => !lang.startsWith(pageLanguage)) |
| 81 | + .join(','); |
| 82 | + new google.translate.TranslateElement( |
| 83 | + { |
| 84 | + pageLanguage: pageLanguage, |
| 85 | + includedLanguages: allLangButPageLang, |
| 86 | + // autoDisplay: true, |
| 87 | + multilanguagePage: true, |
| 88 | + }, |
| 89 | + gtContainerID, |
| 90 | + ); |
| 91 | + } |
| 92 | +</script> |
| 93 | + |
| 94 | +<script |
| 95 | + type="text/javascript" |
| 96 | + src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit" |
| 97 | +></script> |
| 98 | + |
| 99 | +{{- end -}} |
0 commit comments