Replace cookie by local storage to store language

develop
Nolway 1 year ago committed by Alexis Faizeau
parent 8286cdd41d
commit 41ef9fd49f

@ -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,

@ -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

@ -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 {

@ -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;
};

@ -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

Loading…
Cancel
Save