-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
69 lines (62 loc) · 3.06 KB
/
index.html
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
<!-- Dev by TheBigBoss33 21/02/2021-->
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Lang-Manager</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
</head>
<body onload="initLang()">
<h1 id="welcomeMessage">Hello</h1>
<select name="langs" id="lang-select">
</select>
<label id="labelSelectLang" for="lang-select">Chose lang please</label>
<button id="LangDoneBtn" onclick="validateLang()">Done</button>
<script>
var langMap = new Map(); // New map for all langs
const fs = require('fs');
function initLang (){
const langFiles = fs.readdirSync('./langs').filter(file => file.endsWith('.json')); //Get all langs files .json
var langSelect = document.getElementById("lang-select");
var langNumber = 0;
var length = langSelect.options.length;
for (i = length-1; i >= 0; i--) {
langSelect.options[i] = null; //Clear all option
}
for (const file of langFiles) { //Execute for each lang file
langNumber ++;
const lang = require(`./langs/${file}`);
var option = document.createElement('option'); //Create the option
option.text = lang.name;//Set option
langSelect.add(option);//Add the option
langMap.set(lang.name, file);
}
if(localStorage.getItem('lang') && localStorage.getItem('lang-file')){ // if have localstorage
document.getElementById('lang-select').value = localStorage.getItem('lang'); // Set selector option to the lang selected
const fs = require('fs');
fs.readFile('./langs/' + localStorage.getItem("lang-file"), 'utf8' , (err, data) => { // Read the lang file selectioned
if (err) {
console.error(err) // Log if have a error
return
}
const file = JSON.parse(data); // Parse the json
document.getElementById("labelSelectLang").innerHTML = file.ChooseLanguage; // Change text for the text in lang file
document.getElementById("welcomeMessage").innerHTML = file.WelcomeMessage; // You can add line here for change lang of other components of the page
console.log(data);// Log the json object
})
}
}
function validateLang (){ // When button done is pressed
const langSelect = document.getElementById("lang-select"); // Get selector
localStorage.removeItem('lang'); // Clear localstorage
localStorage.removeItem('lang-file'); // Clear localstorage
localStorage.setItem('lang', langSelect.value); // Add the lang name at localstorage
localStorage.setItem('lang-file', langMap.get(langSelect.value));// Add the lang name at localstorage
console.log(localStorage.getItem('lang')); // Log to console
console.log(localStorage.getItem('lang-file'));// Log to console
initLang()//Re init for save change
}
</script>
</body>
</html>