-
Notifications
You must be signed in to change notification settings - Fork 5
/
commons.js
35 lines (32 loc) · 1.36 KB
/
commons.js
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
function get_parameter_from_url(param_name) {
param_name = param_name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + param_name + "=([^&#]*)"),
results = regex.exec(location.href);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
//detect language, get the appropriate translation file, translate html content
var available_languages = ["ca", "cs", "de", "en", "es", "fr", "hu", "it", "pl", "pt"];
var current_language = "en"; //also used to get Osmose issue and Wikipedia extract
var i18n_messages = {}
async function load_translation_strings(){
var user_language = navigator.language;
if (available_languages.includes(user_language)){
var i18n_file = `i18n/${user_language}.json`;
current_language = user_language;
} else {
var i18n_file = "i18n/en.json"
}
await fetch(i18n_file).then(function(data) {
return data.json()
})
.then(function(data) {
i18n_messages = data
})
.catch(function(error) {
console.error(`error loading ${i18n_file}: ${error}`);
});
var html_elements_to_translate = document.querySelectorAll("[data-i18n]");
html_elements_to_translate.forEach(function(elem) {
elem.textContent = i18n_messages[elem.getAttribute("data-i18n")]
});
}