1let translations = {}; 2 3// Apply translations to the page 4function applyTranslations(lang) { 5 const elements = document.querySelectorAll('[data-i18n]'); 6 elements.forEach(element => { 7 const key = element.getAttribute('data-i18n'); 8 if (translations[key]) { 9 element.innerHTML = translations[key]; 10 } 11 }); 12 updateLanguageToggleText(lang); 13 updateIframeTranslations(); // Update iframe content 14} 15 16// Detect browser language 17function getBrowserLanguage() { 18 const browserLang = navigator.language || navigator.userLanguage; 19 return browserLang.startsWith('fr') ? 'fr' : 'en'; 20} 21 22// Load language from local storage or fallback to browser language 23function loadLanguage() { 24 let lang = localStorage.getItem('language'); 25 if (!lang) { 26 lang = getBrowserLanguage(); 27 localStorage.setItem('language', lang); // Save detected language 28 } 29 return lang; 30} 31 32// Load translations from a JSON file 33async function loadTranslations(lang) { 34 try { 35 const response = await fetch(`/translations/${lang}.json`); 36 if (!response.ok) { 37 throw new Error(`Could not load ${lang} translations`); 38 } 39 translations = await response.json(); 40 applyTranslations(lang); 41 } catch (error) { 42 console.error("Error loading translations:", error); 43 } 44} 45 46// Toggle language and save it to local storage 47function toggleLanguage() { 48 const currentLang = localStorage.getItem('language') || 'en'; 49 const newLang = currentLang === 'en' ? 'fr' : 'en'; 50 localStorage.setItem('language', newLang); 51 loadTranslations(newLang); // Reload translations 52} 53 54// Update the language toggle button text 55function updateLanguageToggleText(lang) { 56 const button = document.getElementById('language-toggle'); 57 if (button) { 58 button.textContent = lang === 'en' ? 'FR' : 'EN'; 59 } 60} 61 62// Update translations in the iframe 63function updateIframeTranslations() { 64 const iframe = document.getElementById('trackingIframe'); 65 if (iframe && iframe.contentWindow) { 66 iframe.contentWindow.postMessage({ 67 action: "translate", 68 translations: translations // Pass the entire translations object 69 }, 'https://track.twigroup.com'); // Update URL if needed 70 } 71} 72 73// Initialize language and apply immediately 74(function initializeLanguage() { 75 const lang = loadLanguage(); 76 document.documentElement.lang = lang; // Set lang attribute immediately 77 document.documentElement.classList.add('translations-loading'); // Add loading class 78})(); 79 80// Remove the loading class after translations are applied 81document.addEventListener('DOMContentLoaded', () => { 82 const lang = document.documentElement.lang; // Use the already set language 83 loadTranslations(lang); 84 85 // Listen for messages from the iframe if needed 86 window.addEventListener('message', (event) => { 87 // Handle potential messages from iframe 88 console.log('Message from iframe:', event.data); 89 }); 90});
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.