class Translate {
constructor(props) {
this.attribute = props.attribute;
this.lng = props.lng;
}
process() {
const _self = this;
const xrhFile = new XMLHttpRequest();
xrhFile.open("GET", `./src/scripts/translations/${this.lng}.json`, false);
xrhFile.onreadystatechange = function () {
if (xrhFile.readyState === 4) {
if (xrhFile.status === 200 || xrhFile.status == 0) {
const LngObject = JSON.parse(xrhFile.responseText);
const allDom = document.getElementsByTagName("*");
for (let i = 0; i < allDom.length; i++) {
const elem = allDom[i];
const key = elem.getAttribute(_self.attribute);
if (key != null) {
elem.innerHTML = LngObject[key];
}
}
}
}
};
xrhFile.send();
}
}
function loadNewLang(new_lang) {
const translate = new Translate({ attribute: 'data-lang', lng: new_lang });
const currentLng = new_lang;
translate.process();
}
loadNewLang('en');
document.querySelector('.language-switcher').addEventListener('click', function (e) {
e.preventDefault();
if (this.classList.contains('ru')) {
this.classList.toggle('ru');
this.classList.toggle('en');
this.textContent = 'En';
loadNewLang('en');
}
else if (this.classList.contains('en')) {
this.classList.toggle('en');
this.classList.toggle('ru');
this.textContent = 'Ru';
loadNewLang('ru');
}
});
export {};