// Load translations based on current language function loadTranslations(lang) { return fetch(`locales/${lang}.json`) .then(response => response.json()) .catch(() => { console.warn(`Failed to load ${lang} translations, falling back to English`); return fetch('locales/en.json').then(response => response.json()); }); } // Apply translations to the page function applyTranslations(translations) { // Update meta tags document.title = translations.meta.title; document.querySelector('meta[name="description"]').setAttribute('content', translations.meta.description); // Update navbar links document.querySelectorAll('[data-i18n]').forEach(el => { const key = el.dataset.i18n; const parts = key.split('.'); let value = translations; for (const part of parts) { value = value[part]; if (!value) break; } if (value) { el.textContent = value; if (el.placeholder) el.placeholder = value; } }); } document.addEventListener('DOMContentLoaded', function() { // Set initial language from localStorage or browser const currentLang = localStorage.getItem('userLanguage') || navigator.language.split('-')[0] || 'en'; // Set HTML lang and dir attributes document.documentElement.lang = currentLang; if (currentLang === 'ar') { document.documentElement.dir = 'rtl'; } else { document.documentElement.dir = 'ltr'; } // Load and apply translations loadTranslations(currentLang).then(applyTranslations); // Animate elements with the 'fade-in' class const fadeElements = document.querySelectorAll('.fade-in'); fadeElements.forEach((el, index) => { setTimeout(() => { el.style.opacity = 1; }, index * 150); }); // Animate elements with the 'slide-up' class const slideElements = document.querySelectorAll('.slide-up'); slideElements.forEach((el, index) => { setTimeout(() => { el.style.opacity = 1; el.style.transform = 'translateY(0)'; }, index * 100); }); // Theme switcher functionality const themeToggle = document.getElementById('theme-toggle'); if (themeToggle) { themeToggle.addEventListener('click', function() { document.documentElement.classList.toggle('dark'); localStorage.setItem('theme', document.documentElement.classList.contains('dark') ? 'dark' : 'light'); }); } // Check for saved theme preference if (localStorage.getItem('theme') === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) { document.documentElement.classList.add('dark'); } else { document.documentElement.classList.remove('dark'); } // Mobile menu toggle const mobileMenuButton = document.getElementById('mobile-menu-button'); const mobileMenu = document.getElementById('mobile-menu'); if (mobileMenuButton && mobileMenu) { mobileMenuButton.addEventListener('click', function() { mobileMenu.classList.toggle('hidden'); const icon = mobileMenuButton.querySelector('i'); if (mobileMenu.classList.contains('hidden')) { icon.setAttribute('data-feather', 'menu'); } else { icon.setAttribute('data-feather', 'x'); } feather.replace(); }); } // Apply RTL styles if Arabic if (document.documentElement.lang === 'ar') { document.documentElement.dir = 'rtl'; } }); // Intersection Observer for scroll animations const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.classList.add('animate-fadeInUp'); } }); }, { threshold: 0.1 }); document.querySelectorAll('.animate-on-scroll').forEach(el => { observer.observe(el); });