_
This commit is contained in:
@ -1,11 +1,17 @@
|
||||
import { reloadView } from '../index.js'
|
||||
|
||||
export default class LanguageManager {
|
||||
constructor() {
|
||||
this.availableLanguages = ['en', 'fr'];
|
||||
|
||||
this.dict = null;
|
||||
this.currentLang = 'en'
|
||||
this.chosenLang = localStorage.getItem('preferedLanguage') || this.currentLang;
|
||||
if (this.chosenLang !== this.currentLang && this.availableLanguages.includes(this.chosenLang)) {
|
||||
this.translatePage();
|
||||
this.currentLang = this.chosenLang;
|
||||
} else {
|
||||
this.loadDict(this.chosenLang);
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,33 +19,48 @@ export default class LanguageManager {
|
||||
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}`);
|
||||
await this.loadDict(this.chosenLang);
|
||||
if (!this.dict)
|
||||
return 1;
|
||||
}
|
||||
|
||||
document.querySelectorAll('[data-i18n]').forEach(el => {
|
||||
let key = el.getAttribute('data-i18n');
|
||||
el.innerHTML = translation[key];
|
||||
el.innerHTML = this.dict[key];
|
||||
})
|
||||
await reloadView();
|
||||
|
||||
this.currentLang = this.chosenLang;
|
||||
return 0;
|
||||
}
|
||||
|
||||
async changeLanguage(lang) {
|
||||
if (lang === this.currentLang || !this.availableLanguages.includes(lang))
|
||||
return;
|
||||
return 1;
|
||||
|
||||
this.chosenLang = lang;
|
||||
if (await this.translatePage() !== 0)
|
||||
return;
|
||||
return 1;
|
||||
|
||||
this.currentLang = this.chosenLang;
|
||||
localStorage.setItem('preferedLanguage', lang);
|
||||
return 0;
|
||||
}
|
||||
|
||||
async loadDict(lang) {
|
||||
let dictUrl = `${location.origin}/static/js/lang/${lang}.json`;
|
||||
let response = await fetch(dictUrl);
|
||||
|
||||
if (response.status !== 200) {
|
||||
console.log(`No translation found for language ${lang}`);
|
||||
return;
|
||||
}
|
||||
|
||||
this.dict = await response.json();
|
||||
}
|
||||
|
||||
get(key, defaultTxt) {
|
||||
if (!this.dict)
|
||||
return defaultTxt;
|
||||
|
||||
return this.dict[key] || defaultTxt;
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ class MyProfile extends Profile
|
||||
/**
|
||||
*
|
||||
* @param {*} form_data
|
||||
* @returns
|
||||
* @returns {Promise<Object>}
|
||||
*/
|
||||
async change_avatar(form_data)
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ class Account
|
||||
/**
|
||||
* @param {String} username
|
||||
* @param {String} password
|
||||
* @returns
|
||||
* @returns {?Promise<Object>}
|
||||
*/
|
||||
async create(username, password)
|
||||
{
|
||||
@ -33,7 +33,7 @@ class Account
|
||||
|
||||
/**
|
||||
* @param {String} password
|
||||
* @returns
|
||||
* @returns {?Promise<Object>}
|
||||
*/
|
||||
async delete(password)
|
||||
{
|
||||
@ -52,7 +52,7 @@ class Account
|
||||
|
||||
/**
|
||||
* Get account data (username)
|
||||
* @returns
|
||||
* @returns {?Promise<Object>}
|
||||
*/
|
||||
async get()
|
||||
{
|
||||
@ -71,7 +71,7 @@ class Account
|
||||
*
|
||||
* @param {*} data
|
||||
* @param {Number} password
|
||||
* @returns
|
||||
* @returns {?Object}
|
||||
*/
|
||||
async update(data, password)
|
||||
{
|
||||
|
@ -78,7 +78,7 @@ class Client
|
||||
|
||||
/**
|
||||
* The only right way to determine is the user is logged
|
||||
* @returns {Boolean}
|
||||
* @returns {Promise<Boolean>}
|
||||
*/
|
||||
async isAuthentificate()
|
||||
{
|
||||
@ -91,7 +91,7 @@ class Client
|
||||
* Send a GET request to %uri%
|
||||
* @param {String} uri
|
||||
* @param {*} data
|
||||
* @returns {Response}
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
async _get(uri, data)
|
||||
{
|
||||
@ -106,7 +106,7 @@ class Client
|
||||
* Send a POST request
|
||||
* @param {String} uri
|
||||
* @param {*} data
|
||||
* @returns {Response}
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
async _post(uri, data)
|
||||
{
|
||||
@ -115,7 +115,8 @@ class Client
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-CSRFToken": getCookie("csrftoken"),
|
||||
},
|
||||
'Accept-Language': this.lang.currentLang,
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return response;
|
||||
@ -125,7 +126,7 @@ class Client
|
||||
* Send a DELETE request
|
||||
* @param {String} uri
|
||||
* @param {String} data
|
||||
* @returns {Response}
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
async _delete(uri, data)
|
||||
{
|
||||
@ -144,7 +145,7 @@ class Client
|
||||
* Send a PATCH request with json
|
||||
* @param {String} uri
|
||||
* @param {*} data
|
||||
* @returns {Response}
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
async _patch_json(uri, data)
|
||||
{
|
||||
@ -163,7 +164,7 @@ class Client
|
||||
* Send a PATCH request with file
|
||||
* @param {String} uri
|
||||
* @param {*} file
|
||||
* @returns {Response}
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
async _patch_file(uri, file)
|
||||
{
|
||||
@ -179,7 +180,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)
|
||||
@ -211,7 +212,7 @@ class Client
|
||||
* Loggin the user
|
||||
* @param {String} username
|
||||
* @param {String} password
|
||||
* @returns
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
async login(username, password)
|
||||
{
|
||||
@ -224,6 +225,7 @@ class Client
|
||||
|
||||
/**
|
||||
* Logout the user
|
||||
* @returns {Promise<?>}
|
||||
*/
|
||||
async logout()
|
||||
{
|
||||
@ -233,7 +235,7 @@ class Client
|
||||
|
||||
/**
|
||||
* Determine if the user is logged. NEVER USE IT, USE isAuthentificated()
|
||||
* @returns {Boolean}
|
||||
* @returns {Promise<Boolean>}
|
||||
*/
|
||||
async _test_logged()
|
||||
{
|
||||
|
@ -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)
|
||||
|
@ -34,6 +34,10 @@ class Profile
|
||||
this.isFriend = false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
async init()
|
||||
{
|
||||
let response;
|
||||
|
@ -15,7 +15,7 @@ class Profiles
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {[Profile]}
|
||||
* @returns {Promise<[Profile]>}
|
||||
*/
|
||||
async all()
|
||||
{
|
||||
@ -53,7 +53,7 @@ class Profiles
|
||||
/**
|
||||
* Block a user
|
||||
* @param {Number} user_id
|
||||
* @returns
|
||||
* @returns {Promise<Object>}
|
||||
*/
|
||||
async block(user_id) {
|
||||
|
||||
@ -70,7 +70,7 @@ class Profiles
|
||||
/**
|
||||
* Unblock a user
|
||||
* @param {Number} user_id
|
||||
* @returns
|
||||
* @returns {Promise<Object>}
|
||||
*/
|
||||
async deblock(user_id) {
|
||||
|
||||
|
@ -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())
|
||||
|
@ -17,7 +17,7 @@ class Tourmanents
|
||||
/**
|
||||
*
|
||||
* @param {Number} id
|
||||
* @returns
|
||||
* @returns {?Promise<Tournament>}
|
||||
*/
|
||||
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("");
|
||||
|
Reference in New Issue
Block a user