-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
94 lines (74 loc) · 3.02 KB
/
script.js
File metadata and controls
94 lines (74 loc) · 3.02 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
// SEARCH BAR FUNCTIONALITY
const search = document.getElementById('search');
search.addEventListener('input', () => {
const query = search.value.toLowerCase();
document.querySelectorAll('.topic').forEach(topic => {
const headingText = topic.querySelector('h2').textContent.toLowerCase();
const paragraphText = topic.querySelector('p').textContent.toLowerCase();
if (headingText.includes(query) || paragraphText.includes(query)) {
topic.style.display = 'block';
} else {
topic.style.display = 'none';
}
});
});
// THEME TOGGLE FUNCTIONALITY
const themeToggle = document.querySelectorAll('#theme-toggle i');
document.getElementById('theme-toggle').addEventListener('click', () => {
document.body.classList.toggle('light-theme');
themeToggle.forEach((icon) => {
icon.classList.toggle('fa-sun');
icon.classList.toggle('fa-moon');
});
// save the theme preference in local storage
const isLightTheme = document.body.classList.contains('light-theme');
localStorage.setItem('isLightTheme', isLightTheme);
}
);
// get the theme preference from local storage
const isLightTheme = JSON.parse(localStorage.getItem('isLightTheme'));
if (isLightTheme) {
document.body.classList.add('light-theme');
themeToggle.forEach((icon) => {
icon.classList.add('fa-sun');
icon.classList.remove('fa-moon');
});
}
/* SCRIPT FOR THE CHANGING OF THE LANGUAGE */
document.getElementById('language-select').addEventListener('change', function() {
const selectedLanguage = this.value;
localStorage.setItem('preferredLanguage', selectedLanguage);
loadTranslations(selectedLanguage);
});
function loadTranslations(language) {
fetch(`locales/${language}.json`)
.then(response => response.json())
.then(translations => {
updateLanguage(translations);
})
.catch(error => console.error('Error loading translations:', error));
}
function updateLanguage(translations) {
document.querySelectorAll('[data-key]').forEach(element => {
const key = element.getAttribute('data-key');
const keys = key.split('.');
let translation = translations;
keys.forEach(k => {
translation = translation[k];
});
// Replace \n with <br> to create line breaks
if (translation) {
translation = translation.replace(/\n/g, '<br>');
}
// If it's an input, set placeholder. Otherwise, set HTML.
if (element.tagName.toLowerCase() === 'input') {
element.setAttribute('placeholder', translation);
} else {
element.innerHTML = translation;
}
});
}
// Load saved language preference or default to 'it'
const savedLanguage = localStorage.getItem('preferredLanguage') || 'it';
document.getElementById('language-select').value = savedLanguage;
loadTranslations(savedLanguage);