File size: 3,966 Bytes
9114d88 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
// 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);
}); |