From 41ef9fd49fcd316cdd2999ece60dd6c8aed37356 Mon Sep 17 00:00:00 2001 From: Nolway Date: Sat, 18 Dec 2021 22:18:35 +0100 Subject: [PATCH] Replace cookie by local storage to store language --- front/src/Phaser/Game/GameScene.ts | 6 ++---- front/src/Phaser/Login/EntryScene.ts | 1 - front/src/Translator/Translator.ts | 15 +++++++-------- front/src/Utils/Cookies.ts | 20 -------------------- front/translations/how-to-translate.md | 2 +- 5 files changed, 10 insertions(+), 34 deletions(-) delete mode 100644 front/src/Utils/Cookies.ts diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index c7147993..33e0a862 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -76,7 +76,7 @@ import { userIsAdminStore } from "../../Stores/GameStore"; import { contactPageStore } from "../../Stores/MenuStore"; import type { WasCameraUpdatedEvent } from "../../Api/Events/WasCameraUpdatedEvent"; import { audioManagerFileStore, audioManagerVisibilityStore } from "../../Stores/AudioManagerStore"; -import { Translator } from "../../Translator/Translator"; +import { translator } from "../../Translator/Translator"; import EVENT_TYPE = Phaser.Scenes.Events; import Texture = Phaser.Textures.Texture; @@ -212,7 +212,6 @@ export class GameScene extends DirtyScene { private loader: Loader; private lastCameraEvent: WasCameraUpdatedEvent | undefined; private firstCameraUpdateSent: boolean = false; - private translator: Translator; constructor(private room: Room, MapUrlFile: string, customKey?: string | undefined) { super({ @@ -232,7 +231,6 @@ export class GameScene extends DirtyScene { this.connectionAnswerPromiseResolve = resolve; }); this.loader = new Loader(this); - this.translator = Translator.getInstance(); } //hook preload scene @@ -1324,7 +1322,7 @@ ${escapedMessage} startLayerName: this.startPositionCalculator.startLayerName, uuid: localUserStore.getLocalUser()?.uuid, nickname: this.playerName, - language: Translator.getStringByLanguage(this.translator.getCurrentLanguage()), + language: translator.getStringByLanguage(translator.getCurrentLanguage()), roomId: this.roomUrl, tags: this.connection ? this.connection.getAllTags() : [], variables: this.sharedVariablesManager.variables, diff --git a/front/src/Phaser/Login/EntryScene.ts b/front/src/Phaser/Login/EntryScene.ts index d75ead03..37b77048 100644 --- a/front/src/Phaser/Login/EntryScene.ts +++ b/front/src/Phaser/Login/EntryScene.ts @@ -34,7 +34,6 @@ export class EntryScene extends Scene { .loadCurrentLanguageObject(this.cache) .catch((e: unknown) => { console.error("Error during language loading!", e); - throw e; }) .finally(() => { gameManager diff --git a/front/src/Translator/Translator.ts b/front/src/Translator/Translator.ts index b1e0bdf4..35785e95 100644 --- a/front/src/Translator/Translator.ts +++ b/front/src/Translator/Translator.ts @@ -1,5 +1,4 @@ import { FALLBACK_LANGUAGE } from "../Enum/EnvironmentVariable"; -import { getCookie } from "../Utils/Cookies"; export type Language = { language: string; @@ -90,7 +89,7 @@ class Translator { ); if (!languageObject) { - return reject(); + return reject(new Error("Language not found in cache")); } this.currentLanguageObject = languageObject as LanguageObject; @@ -135,13 +134,13 @@ class Translator { */ private defineCurrentLanguage() { const navigatorLanguage: string | undefined = navigator.language; - const cookieLanguage = getCookie("language"); + const localStorageLanguage = localStorage.getItem("language"); let currentLanguage = undefined; - if (cookieLanguage && typeof cookieLanguage === "string") { - const cookieLanguageObject = this.getLanguageByString(cookieLanguage); - if (cookieLanguageObject) { - currentLanguage = cookieLanguageObject; + if (localStorageLanguage && typeof localStorageLanguage === "string") { + const localStorageLanguageObject = this.getLanguageByString(localStorageLanguage); + if (localStorageLanguageObject) { + currentLanguage = localStorageLanguageObject; } } @@ -207,7 +206,7 @@ class Translator { /** * Get translation by a key and formatted with params by {{ }} tag * @param {string} key Translation key - * @param {{ [key: string]: string | number }} params Tags to replace by value + * @param {TranslationParams} params Tags to replace by value * @returns {string} Translation formatted */ public _(key: string, params?: TranslationParams): string { diff --git a/front/src/Utils/Cookies.ts b/front/src/Utils/Cookies.ts deleted file mode 100644 index 3ca418c2..00000000 --- a/front/src/Utils/Cookies.ts +++ /dev/null @@ -1,20 +0,0 @@ -export const setCookie = (name: string, value: unknown, days: number) => { - let expires = ""; - if (days) { - const date = new Date(); - date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); - expires = "; expires=" + date.toUTCString(); - } - document.cookie = name + "=" + (value || "") + expires + "; path=/"; -}; - -export const getCookie = (name: string): unknown | undefined => { - const nameEquals = name + "="; - const ca = document.cookie.split(";"); - for (let i = 0; i < ca.length; i++) { - let c = ca[i]; - while (c.charAt(0) == " ") c = c.substring(1, c.length); - if (c.indexOf(nameEquals) == 0) return c.substring(nameEquals.length, c.length); - } - return undefined; -}; diff --git a/front/translations/how-to-translate.md b/front/translations/how-to-translate.md index 1586c17e..72c35c49 100644 --- a/front/translations/how-to-translate.md +++ b/front/translations/how-to-translate.md @@ -4,7 +4,7 @@ In the translations folder, all json files of the form [namespace].[language code].json are interpreted to create languages by the language code in the file name. -The only mandatory file that is the entry point is the index.[language code].json which must contain the properties language, country and default so that the language can be taken into account. +The only mandatory file (the entry point) is the `index.[language code].json` which must contain the properties language, country and default so that the language can be taken into account. Example: ```json