-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
183 lines (156 loc) · 6 KB
/
Copy pathscript.js
File metadata and controls
183 lines (156 loc) · 6 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Translation system with external JSON files
let translations = {};
let currentLang = 'en'; // Default to English
// Load translation file
async function loadTranslation(lang) {
try {
const response = await fetch(`translations/${lang}.json`);
if (!response.ok) {
throw new Error(`Failed to load ${lang}.json`);
}
const data = await response.json();
translations[lang] = data;
return data;
} catch (error) {
console.error(`Error loading translation for ${lang}:`, error);
// Fallback to English if translation fails
if (lang !== 'en') {
return loadTranslation('en');
}
return null;
}
}
// Set language and update UI
async function setLanguage(lang) {
console.log('Setting language to:', lang);
currentLang = lang;
localStorage.setItem('preferredLanguage', lang);
// Load translation if not already loaded
if (!translations[lang]) {
console.log('Loading translation for:', lang);
await loadTranslation(lang);
}
console.log('Available translations:', Object.keys(translations));
console.log('Current translation data:', translations[lang]);
// Update all elements with data-i18n attribute
const elements = document.querySelectorAll('[data-i18n]');
console.log('Found', elements.length, 'elements to translate');
elements.forEach(element => {
const key = element.getAttribute('data-i18n');
const keys = key.split('.');
let translation = translations[lang];
for (const k of keys) {
translation = translation?.[k];
}
if (translation) {
element.textContent = translation;
} else {
console.warn('Missing translation for key:', key);
}
});
// Update dropdown selected value
const dropdown = document.getElementById('languageSelect');
if (dropdown) {
dropdown.value = lang;
}
// Update HTML lang attribute
document.documentElement.lang = lang;
console.log('Language set successfully to:', lang);
}
// Initialize language system on page load
document.addEventListener('DOMContentLoaded', async () => {
// Load all translations
await Promise.all([
loadTranslation('en'),
loadTranslation('hi'),
loadTranslation('ta'),
loadTranslation('ml')
]);
// Check if user has a saved preference, otherwise default to English
const savedLang = localStorage.getItem('preferredLanguage');
currentLang = savedLang || 'en';
// Set initial language
await setLanguage(currentLang);
// Add change listener to dropdown
const dropdown = document.getElementById('languageSelect');
console.log('Dropdown element:', dropdown);
if (dropdown) {
dropdown.addEventListener('change', async (e) => {
console.log('Dropdown changed!');
console.log('Language changed to:', e.target.value);
await setLanguage(e.target.value);
});
console.log('Dropdown listener attached successfully');
} else {
console.error('Language dropdown not found!');
}
});
// Mobile Menu Toggle (needs to wait for DOM)
document.addEventListener('DOMContentLoaded', () => {
const mobileMenuToggle = document.querySelector('.mobile-menu-toggle');
const navMenu = document.querySelector('.nav-menu');
if (mobileMenuToggle && navMenu) {
mobileMenuToggle.addEventListener('click', () => {
navMenu.classList.toggle('active');
mobileMenuToggle.classList.toggle('active');
});
// Close mobile menu when clicking on a nav link
const navLinks = document.querySelectorAll('.nav-menu a');
navLinks.forEach(link => {
link.addEventListener('click', () => {
navMenu.classList.remove('active');
mobileMenuToggle.classList.remove('active');
});
});
// Close mobile menu when clicking outside
document.addEventListener('click', (e) => {
if (!e.target.closest('.navbar')) {
navMenu.classList.remove('active');
mobileMenuToggle.classList.remove('active');
}
});
// Smooth scroll enhancement (optional - already handled by CSS, but adds offset for sticky nav)
navLinks.forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
const targetId = link.getAttribute('href');
const targetSection = document.querySelector(targetId);
if (targetSection) {
const navHeight = document.querySelector('.navbar').offsetHeight;
const targetPosition = targetSection.offsetTop - navHeight;
window.scrollTo({
top: targetPosition,
behavior: 'smooth'
});
}
});
});
}
// Add scroll effect to navbar
const navbar = document.querySelector('.navbar');
window.addEventListener('scroll', () => {
if (window.pageYOffset > 50) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
});
// Results announcement popup
const popup = document.getElementById('resultsPopup');
const popupClose = document.getElementById('popupClose');
const popupCta = document.getElementById('popupCta');
if (popup) {
// Show after a short delay
setTimeout(() => {
popup.classList.add('active');
}, 800);
const closePopup = () => {
popup.classList.remove('active');
};
popupClose.addEventListener('click', closePopup);
popupCta.addEventListener('click', closePopup);
popup.addEventListener('click', (e) => {
if (e.target === popup) closePopup();
});
}
});