lang: navbar translation

This commit is contained in:
AdrienLSH
2024-01-24 14:13:11 +01:00
parent 40d61233e5
commit 8eeb9ed4ab
6 changed files with 82 additions and 10 deletions

View File

@ -0,0 +1,45 @@
export default class LanguageManager {
constructor() {
this.availableLanguages = ['en', 'fr'];
this.currentLang = 'en'
this.chosenLang = localStorage.getItem('preferedLanguage') || this.currentLang;
if (this.chosenLang !== this.currentLang && this.availableLanguages.includes(this.chosenLang)) {
this.translatePage();
}
}
async translatePage() {
if (this.currentLang === this.chosenLang)
return;
let dictUrl = `${location.origin}/static/js/lang/${this.chosenLang}.json`;
let translation = await fetch(dictUrl).then(response => {
if (response.status !== 200)
return null;
return response.json();
});
if (!translation) {
console.log(`No translation found for language ${this.chosenLang}`);
return 1;
}
document.querySelectorAll('[data-i18n]').forEach(el => {
let key = el.getAttribute('data-i18n');
el.innerHTML = translation[key];
})
this.currentLang = this.chosenLang;
return 0;
}
async changeLanguage(lang) {
if (lang === this.currentLang || !this.availableLanguages.includes(lang))
return;
this.chosenLang = lang;
if (await this.translatePage() !== 0)
return;
localStorage.setItem('preferedLanguage', lang);
}
}

View File

@ -7,6 +7,7 @@ import { navigateTo } from "../index.js"
import { Tourmanents } from "./tournament/tournaments.js";
import {Notice} from "./chat/notice.js"
import { Channel } from "./chat/channel.js";
import LanguageManager from './LanguageManager.js'
function getCookie(name)
{
@ -70,6 +71,8 @@ class Client
* @type {Notice}
*/
this.notice = new Notice(this);
this.lang = new LanguageManager;
}
/**