diff --git a/EmojiNerdYellowBackground.jpg b/EmojiNerdYellowBackground.jpg deleted file mode 100644 index 0c1720d..0000000 Binary files a/EmojiNerdYellowBackground.jpg and /dev/null differ diff --git a/frontend/static/js/api/LanguageManager.js b/frontend/static/js/api/LanguageManager.js new file mode 100644 index 0000000..3e1c9b3 --- /dev/null +++ b/frontend/static/js/api/LanguageManager.js @@ -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); + } +} diff --git a/frontend/static/js/api/MyProfile.js b/frontend/static/js/api/MyProfile.js index 208531a..de3f46e 100644 --- a/frontend/static/js/api/MyProfile.js +++ b/frontend/static/js/api/MyProfile.js @@ -15,7 +15,7 @@ class MyProfile extends Profile /** * * @param {*} form_data - * @returns + * @returns {Promise} */ async change_avatar(form_data) { diff --git a/frontend/static/js/api/account.js b/frontend/static/js/api/account.js index 6abb89b..b8b7e83 100644 --- a/frontend/static/js/api/account.js +++ b/frontend/static/js/api/account.js @@ -16,7 +16,7 @@ class Account /** * @param {String} username * @param {String} password - * @returns + * @returns {?Promise} */ async create(username, password) { @@ -33,7 +33,7 @@ class Account /** * @param {String} password - * @returns + * @returns {?Promise} */ async delete(password) { @@ -52,7 +52,7 @@ class Account /** * Get account data (username) - * @returns + * @returns {?Promise} */ async get() { @@ -71,7 +71,7 @@ class Account * * @param {*} data * @param {Number} password - * @returns + * @returns {?Object} */ async update(data, password) { diff --git a/frontend/static/js/api/client.js b/frontend/static/js/api/client.js index 455fc22..3d0a900 100644 --- a/frontend/static/js/api/client.js +++ b/frontend/static/js/api/client.js @@ -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,11 +71,13 @@ class Client * @type {Notice} */ this.notice = new Notice(this); + + this.lang = new LanguageManager; } /** * The only right way to determine is the user is logged - * @returns {Boolean} + * @returns {Promise} */ async isAuthentificate() { @@ -87,7 +90,7 @@ class Client * Send a GET request to %uri% * @param {String} uri * @param {*} data - * @returns {Response} + * @returns {Promise} */ async _get(uri, data) { @@ -102,7 +105,7 @@ class Client * Send a POST request * @param {String} uri * @param {*} data - * @returns {Response} + * @returns {Promise} */ async _post(uri, data) { @@ -121,7 +124,7 @@ class Client * Send a DELETE request * @param {String} uri * @param {String} data - * @returns {Response} + * @returns {Promise} */ async _delete(uri, data) { @@ -140,7 +143,7 @@ class Client * Send a PATCH request with json * @param {String} uri * @param {*} data - * @returns {Response} + * @returns {Promise} */ async _patch_json(uri, data) { @@ -159,7 +162,7 @@ class Client * Send a PATCH request with file * @param {String} uri * @param {*} file - * @returns {Response} + * @returns {Promise} */ async _patch_file(uri, file) { @@ -175,7 +178,7 @@ class Client /** * Change logged state. Use It if you recv an 403 error - * @param {Boolean} state + * @param {Promise} state * @returns */ async _update_logged(state) @@ -207,7 +210,7 @@ class Client * Loggin the user * @param {String} username * @param {String} password - * @returns + * @returns {Promise} */ async login(username, password) { @@ -220,6 +223,7 @@ class Client /** * Logout the user + * @returns {Promise} */ async logout() { @@ -229,7 +233,7 @@ class Client /** * Determine if the user is logged. NEVER USE IT, USE isAuthentificated() - * @returns {Boolean} + * @returns {Promise} */ async _test_logged() { diff --git a/frontend/static/js/api/matchmaking.js b/frontend/static/js/api/matchmaking.js index 1178803..c63dd4d 100644 --- a/frontend/static/js/api/matchmaking.js +++ b/frontend/static/js/api/matchmaking.js @@ -19,7 +19,7 @@ class MatchMaking * @param {CallableFunction} receive_func * @param {CallableFunction} disconnect_func * @param {Number} mode The number of players in a game - * @returns {undefined} + * @returns {Promise} */ async start(receive_func, disconnect_func, mode) { @@ -50,6 +50,9 @@ class MatchMaking this.disconnect_func(event); } + /** + * @returns {Promise} + */ async stop() { if (this._socket) diff --git a/frontend/static/js/api/profile.js b/frontend/static/js/api/profile.js index 39717b5..918761d 100644 --- a/frontend/static/js/api/profile.js +++ b/frontend/static/js/api/profile.js @@ -33,6 +33,10 @@ class Profile this.isBlocked = false; } + /** + * + * @returns {Promise<*>} + */ async init() { let response = await this.client._get(`/api/profiles/${this.username}`); diff --git a/frontend/static/js/api/profiles.js b/frontend/static/js/api/profiles.js index 52206ec..3d46efe 100644 --- a/frontend/static/js/api/profiles.js +++ b/frontend/static/js/api/profiles.js @@ -15,7 +15,7 @@ class Profiles /** * - * @returns {[Profile]} + * @returns {Promise<[Profile]>} */ async all() { @@ -45,7 +45,7 @@ class Profiles /** * Block a user * @param {Number} user_id - * @returns + * @returns {Promise} */ async block(user_id) { @@ -62,7 +62,7 @@ class Profiles /** * Unblock a user * @param {Number} user_id - * @returns + * @returns {Promise} */ async deblock(user_id) { diff --git a/frontend/static/js/api/tournament/tournament.js b/frontend/static/js/api/tournament/tournament.js index b0ac785..46a9502 100644 --- a/frontend/static/js/api/tournament/tournament.js +++ b/frontend/static/js/api/tournament/tournament.js @@ -73,6 +73,10 @@ class Tourmanent this.connected = false; } + /** + * + * @returns {Promise} + */ async init() { let response = await this.client._get(`/api/tournaments/${id}`); @@ -108,6 +112,12 @@ class Tourmanent this._socket.send(JSON.stringify({participate: ""})); } + /** + * Join the tournament Websocket + * @param {CallableFunction} receive_func + * @param {CallableFunction} disconnect_func + * @returns {?} + */ async join(receive_func, disconnect_func) { if (!await this.client.isAuthentificate()) diff --git a/frontend/static/js/api/tournament/tournaments.js b/frontend/static/js/api/tournament/tournaments.js index 2ae420a..9f00564 100644 --- a/frontend/static/js/api/tournament/tournaments.js +++ b/frontend/static/js/api/tournament/tournaments.js @@ -17,7 +17,7 @@ class Tourmanents /** * * @param {Number} id - * @returns + * @returns {?Promise} */ async getTournament(id) { @@ -47,6 +47,7 @@ class Tourmanents /** * @param {String} state must be "finished", or "started", or "waiting". Any other return all elements + * @returns {?Promise<[Tourmanent]>} */ async search(state) { @@ -76,6 +77,10 @@ class Tourmanents return tournaments; } + /** + * Get all tournaments + * @returns {?Promise<[Tourmanent]>} + */ async all() { return await this.search(""); diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index 443c6ad..8b9de7c 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -136,6 +136,12 @@ document.addEventListener("DOMContentLoaded", async () => { navigateTo(e.target.href.slice(location.origin.length)); } }); + + //Languages + Array.from(document.getElementById('languageSelector').children).forEach(el => { + el.onclick = _ => client.lang.changeLanguage(el.value); + }); + await client.isAuthentificate(); router(location.pathname); document.querySelector('a[href=\'' + location.pathname + '\']')?.classList.add('active'); diff --git a/frontend/static/js/lang/en.json b/frontend/static/js/lang/en.json new file mode 100644 index 0000000..3d9b14e --- /dev/null +++ b/frontend/static/js/lang/en.json @@ -0,0 +1,9 @@ +{ + "navbarSearch": "Search", + "navbarHome": "Home", + "navbarLogin": "Login", + "navbarRegister": "Register", + "navbarProfile": "My Profile", + "navbarSettings": "Settings", + "navbarLogout": "Logout" +} diff --git a/frontend/static/js/lang/fr.json b/frontend/static/js/lang/fr.json new file mode 100644 index 0000000..3307204 --- /dev/null +++ b/frontend/static/js/lang/fr.json @@ -0,0 +1,9 @@ +{ + "navbarSearch": "Recherche", + "navbarHome": "Maison", + "navbarLogin": "Se connecter", + "navbarRegister": "S'inscrire", + "navbarProfile": "Mon Profil", + "navbarSettings": "Paramètres", + "navbarLogout": "Se déconnecter" +} diff --git a/frontend/templates/index.html b/frontend/templates/index.html index d248029..499c006 100644 --- a/frontend/templates/index.html +++ b/frontend/templates/index.html @@ -12,23 +12,32 @@
-