1// wazistore_dist/i18n.js 2 3(function () { 4 const defaultLang = 'pt'; 5 const supportedLangs = ['pt', 'en']; 6 let currentLang = localStorage.getItem('lang'); 7 8 // Force Portuguese for users in PT/MZ regions, even if they have 'en' stuck in localStorage from previous testing 9 try { 10 const urlParams = new URLSearchParams(window.location.search); 11 const simularEstrangeiro = urlParams.has('simular_estrangeiro'); 12 13 const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone || ''; 14 const isPTorMZ = timeZone.includes('Maputo') || timeZone.includes('Lisbon') || timeZone.includes('Madeira') || timeZone.includes('Azores'); 15 16 if (isPTorMZ && !simularEstrangeiro) { 17 currentLang = 'pt'; 18 localStorage.setItem('lang', 'pt'); 19 } 20 } catch (e) { 21 // Ignore timezone errors 22 } 23 24 if (!currentLang || !supportedLangs.includes(currentLang)) { 25 // Default to Portuguese (PT) for all new users. 26 // Language switching is delegated entirely to the translation toast and toggle UI. 27 currentLang = defaultLang; 28 localStorage.setItem('lang', currentLang); 29 } 30 31 // Set document lang attribute 32 document.documentElement.lang = currentLang; 33 34 // Global translation function 35 window.t = function (key) { 36 if (!window.translations) return key; 37 38 const dict = window.translations[currentLang] || window.translations[defaultLang]; 39 const keys = key.split('.'); 40 let val = dict; 41 42 for (let k of keys) { 43 if (val === undefined) break; 44 val = val[k]; 45 } 46 47 if (val !== undefined) return val; 48 49 // Fallback to default language if missing 50 val = window.translations[defaultLang]; 51 if (val) { 52 for (let k of keys) { 53 if (val === undefined) break; 54 val = val[k]; 55 } 56 } 57 58 return val !== undefined ? val : key; 59 }; 60 61 window.changeLanguage = function (lang) { 62 if (!supportedLangs.includes(lang)) return; 63 currentLang = lang; 64 localStorage.setItem('lang', lang); 65 document.documentElement.lang = lang; 66 window.applyTranslations(); 67 }; 68 69 window.getCurrentLanguage = function () { 70 return currentLang; 71 }; 72 73 // Apply translations to all DOM elements with data-i18n attribute 74 window.applyTranslations = function (root = document) { 75 const elements = root.querySelectorAll('[data-i18n]'); 76 elements.forEach(el => { 77 const key = el.getAttribute('data-i18n'); 78 const translation = window.t(key); 79 80 if (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA') { 81 if (el.hasAttribute('placeholder')) { 82 el.placeholder = translation; 83 } else if (el.type === 'submit' || el.type === 'button') { 84 el.value = translation; 85 } 86 } else { 87 el.innerHTML = translation; 88 } 89 }); 90 }; 91 92 // Auto-apply on load 93 document.addEventListener('DOMContentLoaded', () => { 94 window.applyTranslations(); 95 }); 96 97})();
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.