1(function () { 2 3 const BHASHINI_URL = "https://dhruva-api.bhashini.gov.in/services/inference/pipeline"; 4 const AUTH_KEY = "llicdmBJGzIyxiIWICY4itm5FIkIzv-C3F2WomjbPGxT79oynHNF24EmABpYTFSE"; 5 6 let isTranslating = false; 7 const originalTextMap = new Map(); 8 9 /* ---------- SESSION ---------- */ 10 function getCurrentLang() { 11 return sessionStorage.getItem("bhashiniLang") || "en"; 12 } 13 14 function setCurrentLang(lang) { 15 sessionStorage.setItem("bhashiniLang", lang); 16 } 17 18 /* ---------- HTML LANG ---------- */ 19 function setHtmlLang(lang) { 20 document.documentElement.setAttribute("lang", lang); 21 } 22 23 /* ---------- TRANSLATION ---------- */ 24 async function bhashiniTranslate(text, from, to) { 25 if (!text || !text.trim()) return text; 26 27 const body = { 28 pipelineTasks: [{ 29 taskType: "translation", 30 config: { 31 language: { 32 sourceLanguage: from, 33 targetLanguage: to 34 } 35 } 36 }], 37 inputData: { 38 input: [{ source: text }] 39 } 40 }; 41 42 const res = await fetch(BHASHINI_URL, { 43 method: "POST", 44 headers: { 45 Authorization: AUTH_KEY, 46 "Content-Type": "application/json" 47 }, 48 body: JSON.stringify(body) 49 }); 50 51 const json = await res.json(); 52 return json?.pipelineResponse?.[0]?.output?.[0]?.target || text; 53 } 54 55 /* ---------- DOM HELPERS ---------- */ 56 function getTextNodes() { 57 const walker = document.createTreeWalker( 58 document.body, 59 NodeFilter.SHOW_TEXT, 60 { 61 acceptNode(node) { 62 if (!node.nodeValue?.trim()) return NodeFilter.FILTER_REJECT; 63 if (node.parentElement?.closest( 64 "script,style,noscript,iframe,select,option" 65 )) return NodeFilter.FILTER_REJECT; 66 return NodeFilter.FILTER_ACCEPT; 67 } 68 } 69 ); 70 71 const nodes = []; 72 let node; 73 while ((node = walker.nextNode())) nodes.push(node); 74 return nodes; 75 } 76 77 function getFormInputs() { 78 return Array.from( 79 document.querySelectorAll("input[type='text'], textarea") 80 ); 81 } 82 83 function chunkArray(arr, size) { 84 const chunks = []; 85 for (let i = 0; i < arr.length; i += size) { 86 chunks.push(arr.slice(i, i + size)); 87 } 88 return chunks; 89 } 90 91 /* ---------- APPLY HINDI ---------- */ 92 async function applyHindi() { 93 if (isTranslating) return; 94 isTranslating = true; 95 96 const elements = [ 97 ...getTextNodes(), 98 ...getFormInputs() 99 ]; 100 101 const chunks = chunkArray(elements, 20); 102 103 for (const chunk of chunks) { 104 await Promise.all(chunk.map(async el => { 105 const value = el.nodeType === 3 ? el.nodeValue : el.value; 106 107 if (!originalTextMap.has(el)) { 108 originalTextMap.set(el, value); 109 } 110 111 const translated = await bhashiniTranslate(value, "en", "hi"); 112 113 if (!el.isConnected) return; 114 115 if (el.nodeType === 3) el.nodeValue = translated; 116 else el.value = translated; 117 })); 118 } 119 120 isTranslating = false; 121 } 122 123 /* ---------- RESTORE ENGLISH ---------- */ 124 function restoreEnglish() { 125 originalTextMap.forEach((value, el) => { 126 if (!el.isConnected) return; 127 if (el.nodeType === 3) el.nodeValue = value; 128 else el.value = value; 129 }); 130 } 131 132 /* ---------- TOGGLE WITH ALERT ---------- */ 133 function toggleHindi() { 134 135 const currentLang = getCurrentLang(); 136 137 let targetLang = "hi"; 138 let message = "Do you want to change the website language to Hindi? The page content will change to Hindi."; 139 140 if (currentLang === "hi") { 141 targetLang = "en"; 142 message = "Do you want to change the website language to English? The page content will change to English."; 143 } 144 145 const confirmed = confirm(message); 146 147 if (!confirmed) return; 148 149 // Save language choice 150 setCurrentLang(targetLang); 151 152 // Update html lang before reload 153 if (targetLang === "hi") { 154 setHtmlLang("hi"); 155 } else { 156 setHtmlLang("en-US"); 157 } 158 159 // Reload page 160 location.reload(); 161 } 162 163 /* ---------- BIND BUTTON ---------- */ 164 function bindToggle() { 165 166 const btn = document.getElementById("toggleHindiBtn"); 167 if (!btn || btn.dataset.bound) return; 168 169 btn.dataset.bound = "true"; 170 btn.addEventListener("click", toggleHindi); 171 172 // Page load language restore 173 if (getCurrentLang() === "hi") { 174 btn.textContent = "English"; 175 setHtmlLang("hi"); 176 applyHindi(); 177 } else { 178 btn.textContent = "Hindi"; 179 setHtmlLang("en-US"); 180 } 181 } 182 183 bindToggle(); 184 185 /* ---------- LIFERAY SPA SUPPORT ---------- */ 186 if (window.Liferay) { 187 Liferay.on("endNavigate", function () { 188 bindToggle(); 189 });
190 } 191 192})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.