34 lines
895 B
JavaScript
Executable File
34 lines
895 B
JavaScript
Executable File
let languageSelectorButton = document.getElementById('language-selector-button');
|
|
let languageSelector = document.getElementById('language-selector');
|
|
let selecting = false;
|
|
|
|
function addReplaceLangCode(url, langCode) {
|
|
let a = window.location;
|
|
let path = a.pathname.split('/');
|
|
path.shift();
|
|
|
|
if(path[0].length == 2) {
|
|
path[0] = langCode;
|
|
}else{
|
|
path.unshift(langCode);
|
|
}
|
|
return a.protocol + '//' +
|
|
a.host + '/' + path.join('/') +
|
|
(a.search != '' ? a.search : '') +
|
|
(a.hash != '' ? a.hash : '');
|
|
}
|
|
|
|
function update() {
|
|
let langCode = languageSelector.value;
|
|
let url = addReplaceLangCode(window.location.href, langCode);
|
|
window.location = url;
|
|
}
|
|
languageSelector.addEventListener('click', function() {
|
|
if (selecting) {
|
|
update();
|
|
selecting = false;
|
|
} else {
|
|
selecting = true;
|
|
}
|
|
});
|