// Language switching functionality for QR Rapido document.addEventListener('DOMContentLoaded', function () { // FORCE: Respect the URL culture above all else let currentCulture = getCurrentCulture(); console.log('Current culture:', currentCulture); localStorage.setItem('preferredLanguage', currentCulture); const languageDropdownItems = document.querySelectorAll('.dropdown-item[data-lang]'); const currentLangSpan = document.getElementById('current-lang'); // Get current culture from URL or default to pt-BR function getCurrentCulture() { const pathSegments = window.location.pathname.split('/').filter(segment => segment); const supportedCultures = ['pt-BR', 'es', 'en']; if (pathSegments.length > 0 && supportedCultures.includes(pathSegments[0])) { return pathSegments[0]; } return 'pt-BR'; } // Update current language display function updateCurrentLanguageDisplay(culture) { const langMap = { 'pt-BR': 'PT', 'es': 'ES', 'en': 'EN' }; if (currentLangSpan) { currentLangSpan.textContent = langMap[culture] || 'PT'; } } // Build new URL with selected culture function buildLocalizedUrl(newCulture) { const currentPath = window.location.pathname; const queryString = window.location.search; const hash = window.location.hash; // Remove existing culture from path if present const pathSegments = currentPath.split('/').filter(segment => segment); const supportedCultures = ['pt-BR', 'es', 'en']; // Remove current culture if it's the first segment if (pathSegments.length > 0 && supportedCultures.includes(pathSegments[0])) { pathSegments.shift(); } // Build new path with selected culture const newPath = '/' + newCulture + (pathSegments.length > 0 ? '/' + pathSegments.join('/') : ''); return newPath + queryString + hash; } // Handle language selection // Handle language selection languageDropdownItems.forEach(item => { item.addEventListener('click', function (e) { e.preventDefault(); const selectedLang = this.getAttribute('data-lang'); currentCulture = getCurrentCulture(); // Track language change for analytics if (typeof window.trackLanguageChange === 'function') { window.trackLanguageChange(currentCulture, selectedLang); } // Store language preference in localStorage localStorage.setItem('preferredLanguage', selectedLang); // Set culture cookie for server-side processing document.cookie = `culture=${selectedLang}; path=/; max-age=31536000; SameSite=Lax`; // Clear any cache and force full reload if ('caches' in window) { caches.keys().then(names => { names.forEach(name => caches.delete(name)); }); } // Force complete page reload with cache busting const newUrl = buildLocalizedUrl(selectedLang); // Na função de troca de idioma, substitua: window.location.href = newUrl; // Por: window.location.replace(newUrl); // OU window.location.href = newUrl + (newUrl.includes('?') ? '&' : '?') + '_t=' + Date.now(); //window.location.replace(newUrl + '?_refresh=' + Date.now()); }); }); // Initialize current language display currentCulture = getCurrentCulture(); updateCurrentLanguageDisplay(currentCulture); // Store current culture in localStorage if not already set if (!localStorage.getItem('preferredLanguage')) { localStorage.setItem('preferredLanguage', currentCulture); } }); // Utility function to get user's preferred language function getUserPreferredLanguage() { // Check localStorage first const storedLang = localStorage.getItem('preferredLanguage'); if (storedLang) { return storedLang; } // Check browser language const browserLang = navigator.language || navigator.userLanguage; // Map browser languages to supported cultures const langMap = { 'pt': 'pt-BR', 'pt-BR': 'pt-BR', 'es': 'es', 'es-PY': 'pt-BR', // Special case: Paraguay Spanish -> Portuguese 'en': 'en' }; // Check exact match first if (langMap[browserLang]) { return langMap[browserLang]; } // Check language part only (e.g., 'es' from 'es-AR') const langPart = browserLang.split('-')[0]; if (langMap[langPart]) { return langMap[langPart]; } }