forked from VAIBHAVBABELE/vaibhavbabele.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage.js
More file actions
54 lines (46 loc) · 2.11 KB
/
language.js
File metadata and controls
54 lines (46 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Prevent Google from using its cookies/auto-detect
var gtCookie = document.cookie.split(';').filter(c => c.includes('googtrans'));
if (gtCookie.length) {
document.cookie = 'googtrans=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
}
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en', // force English
includedLanguages: 'en,hi', // only allow English and Hindi
autoDisplay: false
}, 'google_translate_element');
}
document.addEventListener("DOMContentLoaded", function () {
let currentLang = localStorage.getItem("lang") || 'en'; // default English
const checkDropdown = setInterval(() => {
const select = document.querySelector(".goog-te-combo");
if (select) {
clearInterval(checkDropdown);
// Force English first if first visit
if (!localStorage.getItem("lang")) {
currentLang = 'en';
localStorage.setItem("lang", currentLang);
}
// Force dropdown to English or Hindi only
select.value = currentLang;
select.dispatchEvent(new Event("change"));
const langToggle = document.getElementById("lang-toggle");
if (langToggle) {
langToggle.textContent = currentLang === 'en' ? "हिंदी" : "English";
langToggle.onclick = () => {
currentLang = currentLang === 'en' ? 'hi' : 'en';
select.value = currentLang;
select.dispatchEvent(new Event("change"));
langToggle.textContent = currentLang === 'en' ? "हिंदी" : "English";
localStorage.setItem("lang", currentLang);
};
}
}
}, 200);
});
// Load Google Translate script once
if (!document.querySelector('script[src*="translate.google.com"]')) {
const translateScript = document.createElement('script');
translateScript.src = "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit";
document.body.appendChild(translateScript);
}