Skip to content

Commit 3652f28

Browse files
authoredJan 30, 2025··
[i18n] Google translate button in fallback pages for all locales (#6117)
1 parent c78f66c commit 3652f28

File tree

6 files changed

+116
-2
lines changed

6 files changed

+116
-2
lines changed
 

‎.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[submodule "themes/docsy"]
33
path = themes/docsy
44
url = https://github.com/google/docsy.git
5-
docsy-pin = v0.11.0-37-ga854cb31
5+
docsy-pin = v0.11.0-38-g77da7e49
66
docsy-note = "2024-04-01 Switching to google/docsy.git from cncf/docsy.git since we don't have any CNCF customizations."
77
docsy-reminder = "Ensure that any tag referenced by `docsy-pin` is present in the remote repo (url), otherwise add (push) the tags to the repo."
88
[submodule "content-modules/opentelemetry-specification"]

‎.htmltest.yml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ IgnoreURLs: # list of regexs of paths or URLs to be ignored
3434

3535
- ^https://deploy-preview-\d+--opentelemetry.netlify.app/
3636
- ^https://www\.googletagmanager\.com
37+
- ^(https:)?//translate.google.com
3738

3839
- ^https?://localhost\b
3940
- ^https?://127\.0\.0\.1\b

‎assets/scss/_styles_project.scss

+12
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,15 @@ details {
323323
overflow-y: auto;
324324
}
325325
}
326+
327+
// Google translate select / dropdown
328+
329+
.goog-te-gadget {
330+
// cSpell:ignore goog
331+
padding-top: 1rem;
332+
margin-bottom: -0.5rem;
333+
334+
@include media-breakpoint-up(md) {
335+
padding-left: 0.6rem;
336+
}
337+
}

‎layouts/docs/baseof.html

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<div class="td-main">
1818
<div class="row flex-xl-nowrap">
1919
<aside class="col-12 col-md-3 col-xl-2 td-sidebar d-print-none">
20+
<div id="google_translate_element"></div>
2021
{{ partial "sidebar.html" . }}
2122
</aside>
2223
<aside class="d-none d-xl-block col-xl-2 td-sidebar-toc d-print-none">
@@ -36,5 +37,6 @@
3637
{{ partial "footer.html" . }}
3738
</div>
3839
{{ partial "scripts.html" . }}
40+
{{ partial "translate-scripts.html" . -}}
3941
</body>
4042
</html>
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 -}}

‎themes/docsy

0 commit comments

Comments
 (0)