Skip to content

Commit bffa148

Browse files
committed
Fix Google Translate widget not reappearing on versions
This commit fixes an issue with the Google Translate widget not reappearing when navigating from a version that has Japanese docs (like 3.13), to a version that doesn't have Japanese docs (like 3.12), then moving back to a version that has Japanese docs, (like 3.13), and then back to a version that doesn't have Japanese docs (like 3.11).
1 parent 6b43aaf commit bffa148

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

src/theme/NavbarItem/HtmlNavbarItem.tsx

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,46 @@ import clsx from 'clsx';
1010
import { useLocation } from '@docusaurus/router';
1111
import type { Props } from '@theme/NavbarItem/HtmlNavbarItem';
1212

13-
function loadGoogleTranslateScript() {
14-
if (typeof window !== "undefined" && typeof document !== "undefined") {
15-
// Check if the script is already added.
13+
function loadGoogleTranslateScript(callback: () => void) {
14+
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
1615
const existingScript = document.getElementById('google-translate-script');
1716
if (!existingScript) {
1817
const addScript = document.createElement('script');
1918
addScript.src = '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit';
2019
addScript.async = true;
21-
addScript.id = 'google-translate-script'; // Assign unique id.
20+
addScript.id = 'google-translate-script';
21+
addScript.onload = callback; // Trigger callback once loaded.
2222
document.body.appendChild(addScript);
23+
} else {
24+
callback(); // Script already exists, proceed to callback directly.
2325
}
2426
}
2527
}
2628

2729
function initializeGoogleTranslate() {
28-
// Check if window.google is available.
29-
if (typeof window !== 'undefined' && window.google) {
30+
if (typeof window !== 'undefined' && window.google && window.google.translate) {
3031
const targetElement = document.getElementById('google_translate_element');
3132
if (targetElement && !window.googleTranslateElement) {
32-
window.googleTranslateElement = new window.google.translate.TranslateElement(
33-
{ pageLanguage: 'en', includedLanguages: 'ja' },
34-
'google_translate_element'
35-
);
33+
try {
34+
window.googleTranslateElement = new window.google.translate.TranslateElement(
35+
{ pageLanguage: 'en', includedLanguages: 'ja' },
36+
'google_translate_element'
37+
);
38+
} catch (error) {
39+
console.error('Error initializing Google Translate:', error);
40+
}
3641
}
3742
}
3843
}
3944

45+
function removeGoogleTranslateWidget() {
46+
const existingElement = document.getElementById('google_translate_element');
47+
if (existingElement) existingElement.innerHTML = ''; // Clear content.
48+
const script = document.getElementById('google-translate-script');
49+
if (script) script.remove(); // Remove the Google Translate script.
50+
window.googleTranslateElement = null;
51+
}
52+
4053
export default function HtmlNavbarItem({
4154
value,
4255
className,
@@ -46,33 +59,31 @@ export default function HtmlNavbarItem({
4659
const Comp = isDropdownItem ? 'li' : 'div';
4760
const { pathname } = useLocation();
4861

49-
// List of specific versions to show Google Translate.
5062
const allowedVersions = ['3.4', '3.5', '3.6', '3.7', '3.8', '3.9', '3.10', '3.11', '3.12'];
51-
52-
// Extract the version from the pathname, ignoring "latest" and "current".
5363
const versionMatch = pathname.match(/\/docs\/(\d+\.\d+)\//);
5464
const currentVersion = versionMatch ? versionMatch[1] : null;
5565

5666
useEffect(() => {
57-
// Only load and initialize Google Translate if the version is in allowedVersions.
5867
if (currentVersion && allowedVersions.includes(currentVersion)) {
59-
loadGoogleTranslateScript();
60-
window.googleTranslateElementInit = initializeGoogleTranslate;
68+
// Ensure Google Translate script is loaded.
69+
loadGoogleTranslateScript(() => {
70+
if (!sessionStorage.getItem('googleTranslateInitialized')) {
71+
window.googleTranslateElementInit = () => {
72+
initializeGoogleTranslate();
73+
sessionStorage.setItem('googleTranslateInitialized', 'true');
74+
};
75+
} else {
76+
initializeGoogleTranslate(); // Re-initialize if already loaded.
77+
}
78+
});
6179
} else {
62-
// Clear the element content if it's not an allowed version.
63-
const existingElement = document.getElementById('google_translate_element');
64-
if (existingElement) {
65-
existingElement.innerHTML = ''; // Clear the element content.
66-
}
67-
68-
// Reset Google Translate instance.
69-
if (window.googleTranslateElement) {
70-
window.googleTranslateElement = null;
71-
}
80+
// Clear widget if on an unallowed version.
81+
removeGoogleTranslateWidget();
82+
sessionStorage.removeItem('googleTranslateInitialized');
7283
}
73-
}, [currentVersion, allowedVersions]);
84+
}, [currentVersion]);
7485

75-
// If the version is not in allowedVersions, return an empty div.
86+
// Hide component if version is not in allowed versions.
7687
if (currentVersion && !allowedVersions.includes(currentVersion)) {
7788
return null;
7889
}

0 commit comments

Comments
 (0)