This repository has been archived by the owner on Apr 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
languaje.mjs
75 lines (60 loc) · 2.6 KB
/
languaje.mjs
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
/* languaje.mjs
Controls which language is being displayed in use. It will use a "php-esque" query varibale in the url:
www.example.com/?lang=en
The main language is gonna be english, so if no variable is provided it should display english (And maybe
change the URL?)
Language codes are going to be using the IANA code (es, en, ja...)
https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
It will detect the language and the region. If the region is not avaliable/not in the list it will pick another
*/
// Avaliable languages. Manual change (so outdated languages can be removed)
const avaliable_lang_reg = ['en-EN'];
function getLanguage(){
// Get the language parameter from the URL
const url_params = new URLSearchParams(window.location.search);
const lang_param = url_params.get('lang') ?? 'en-EN'; // Default is English.
console.log("Querried language is: ", lang_param)
console.log("AVALIABLE: ", avaliable_lang_reg)
setLanguage(lang_param)
}
function setLanguage(lang) {
// Implement logic to set the language as needed
console.log('Language:', lang);
let language_file = './lang/' + lang + '.json'
loadJsonFile(language_file)
.then(trans_data => {
// Thinking about what the fuck I'm gonna write here.
console.log(trans_data)
const translatable_elements = document.querySelectorAll('.translatable')
translatable_elements.forEach(element => {
let translation_key = element.dataset.translate;
if (trans_data[translation_key] != undefined){
// If the key does not exist ignore it.
element.innerHTML = trans_data[translation_key];
};
})
})
.catch(error => {
console.error('Error loading JSON: ', error)
})
}
async function loadJsonFile(filePath){
// Basically gets a public json file in the server.
try {
const response = await fetch(filePath);
if (!response.ok) {
throw new Error(`Failed to load JSON file: ${response.statusText}`);
}
const jsonData = await response.json();
return jsonData;
} catch (error) {
console.error(error.message);
}
}
// [Not Used] Function that would be run manually before deploying the website,
// just in case the list of languages is huge.
function avaliableLanguages(avaliable_lang_reg) {
// This will take avaliableLangReg and generate a list of avaliableLangs
return avaliable_lang_reg.map(tag => tag.split('-')[0]);
}
export { getLanguage }