File size: 1,615 Bytes
692af3a
 
 
 
 
d27e372
 
692af3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27e372
692af3a
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener('DOMContentLoaded', fetchLanguages);  
  
async function fetchLanguages() {  
    try {  
        const [languagesResponse, currentLanguageResponse] = await Promise.all([  
            fetch(`${url_prefix}/get-languages`),  
            fetch(`${url_prefix}/get-locale`)  
        ]);  
  
        const languages = await languagesResponse.json();  
        const currentLanguage = await currentLanguageResponse.text();  
  
        const languageSelect = document.getElementById('language');  
        languages.forEach(lang => {  
            const option = document.createElement('option');  
            option.value = lang;  
            option.textContent = lang;  
            languageSelect.appendChild(option);  
        });  
  
        const savedLanguage = localStorage.getItem("language") || currentLanguage;  
        setLanguageOnPageLoad(savedLanguage);  
    } catch (error) {  
        console.error("Failed to fetch languages or current language");  
    }  
}  
  
function setLanguageOnPageLoad(language) {  
    document.getElementById("language").value = language;  
}  
  
function changeLanguage(lang) {  
    fetch(`${url_prefix}/change-language`, {  
        method: "POST",  
        headers: {  
            "Content-Type": "application/json",  
        },  
        body: JSON.stringify({ language: lang }),  
    }).then((response) => {  
        if (response.ok) {  
            localStorage.setItem("language", lang);  
            location.reload();  
        } else {  
            console.error("Failed to change language");  
        }  
    });  
}