Replace cookie by local storage to store language
This commit is contained in:
parent
8286cdd41d
commit
41ef9fd49f
@ -76,7 +76,7 @@ import { userIsAdminStore } from "../../Stores/GameStore";
|
|||||||
import { contactPageStore } from "../../Stores/MenuStore";
|
import { contactPageStore } from "../../Stores/MenuStore";
|
||||||
import type { WasCameraUpdatedEvent } from "../../Api/Events/WasCameraUpdatedEvent";
|
import type { WasCameraUpdatedEvent } from "../../Api/Events/WasCameraUpdatedEvent";
|
||||||
import { audioManagerFileStore, audioManagerVisibilityStore } from "../../Stores/AudioManagerStore";
|
import { audioManagerFileStore, audioManagerVisibilityStore } from "../../Stores/AudioManagerStore";
|
||||||
import { Translator } from "../../Translator/Translator";
|
import { translator } from "../../Translator/Translator";
|
||||||
|
|
||||||
import EVENT_TYPE = Phaser.Scenes.Events;
|
import EVENT_TYPE = Phaser.Scenes.Events;
|
||||||
import Texture = Phaser.Textures.Texture;
|
import Texture = Phaser.Textures.Texture;
|
||||||
@ -212,7 +212,6 @@ export class GameScene extends DirtyScene {
|
|||||||
private loader: Loader;
|
private loader: Loader;
|
||||||
private lastCameraEvent: WasCameraUpdatedEvent | undefined;
|
private lastCameraEvent: WasCameraUpdatedEvent | undefined;
|
||||||
private firstCameraUpdateSent: boolean = false;
|
private firstCameraUpdateSent: boolean = false;
|
||||||
private translator: Translator;
|
|
||||||
|
|
||||||
constructor(private room: Room, MapUrlFile: string, customKey?: string | undefined) {
|
constructor(private room: Room, MapUrlFile: string, customKey?: string | undefined) {
|
||||||
super({
|
super({
|
||||||
@ -232,7 +231,6 @@ export class GameScene extends DirtyScene {
|
|||||||
this.connectionAnswerPromiseResolve = resolve;
|
this.connectionAnswerPromiseResolve = resolve;
|
||||||
});
|
});
|
||||||
this.loader = new Loader(this);
|
this.loader = new Loader(this);
|
||||||
this.translator = Translator.getInstance();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//hook preload scene
|
//hook preload scene
|
||||||
@ -1324,7 +1322,7 @@ ${escapedMessage}
|
|||||||
startLayerName: this.startPositionCalculator.startLayerName,
|
startLayerName: this.startPositionCalculator.startLayerName,
|
||||||
uuid: localUserStore.getLocalUser()?.uuid,
|
uuid: localUserStore.getLocalUser()?.uuid,
|
||||||
nickname: this.playerName,
|
nickname: this.playerName,
|
||||||
language: Translator.getStringByLanguage(this.translator.getCurrentLanguage()),
|
language: translator.getStringByLanguage(translator.getCurrentLanguage()),
|
||||||
roomId: this.roomUrl,
|
roomId: this.roomUrl,
|
||||||
tags: this.connection ? this.connection.getAllTags() : [],
|
tags: this.connection ? this.connection.getAllTags() : [],
|
||||||
variables: this.sharedVariablesManager.variables,
|
variables: this.sharedVariablesManager.variables,
|
||||||
|
@ -34,7 +34,6 @@ export class EntryScene extends Scene {
|
|||||||
.loadCurrentLanguageObject(this.cache)
|
.loadCurrentLanguageObject(this.cache)
|
||||||
.catch((e: unknown) => {
|
.catch((e: unknown) => {
|
||||||
console.error("Error during language loading!", e);
|
console.error("Error during language loading!", e);
|
||||||
throw e;
|
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
gameManager
|
gameManager
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { FALLBACK_LANGUAGE } from "../Enum/EnvironmentVariable";
|
import { FALLBACK_LANGUAGE } from "../Enum/EnvironmentVariable";
|
||||||
import { getCookie } from "../Utils/Cookies";
|
|
||||||
|
|
||||||
export type Language = {
|
export type Language = {
|
||||||
language: string;
|
language: string;
|
||||||
@ -90,7 +89,7 @@ class Translator {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (!languageObject) {
|
if (!languageObject) {
|
||||||
return reject();
|
return reject(new Error("Language not found in cache"));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.currentLanguageObject = languageObject as LanguageObject;
|
this.currentLanguageObject = languageObject as LanguageObject;
|
||||||
@ -135,13 +134,13 @@ class Translator {
|
|||||||
*/
|
*/
|
||||||
private defineCurrentLanguage() {
|
private defineCurrentLanguage() {
|
||||||
const navigatorLanguage: string | undefined = navigator.language;
|
const navigatorLanguage: string | undefined = navigator.language;
|
||||||
const cookieLanguage = getCookie("language");
|
const localStorageLanguage = localStorage.getItem("language");
|
||||||
let currentLanguage = undefined;
|
let currentLanguage = undefined;
|
||||||
|
|
||||||
if (cookieLanguage && typeof cookieLanguage === "string") {
|
if (localStorageLanguage && typeof localStorageLanguage === "string") {
|
||||||
const cookieLanguageObject = this.getLanguageByString(cookieLanguage);
|
const localStorageLanguageObject = this.getLanguageByString(localStorageLanguage);
|
||||||
if (cookieLanguageObject) {
|
if (localStorageLanguageObject) {
|
||||||
currentLanguage = cookieLanguageObject;
|
currentLanguage = localStorageLanguageObject;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,7 +206,7 @@ class Translator {
|
|||||||
/**
|
/**
|
||||||
* Get translation by a key and formatted with params by {{ }} tag
|
* Get translation by a key and formatted with params by {{ }} tag
|
||||||
* @param {string} key Translation key
|
* @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
|
* @returns {string} Translation formatted
|
||||||
*/
|
*/
|
||||||
public _(key: string, params?: TranslationParams): string {
|
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.
|
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:
|
Example:
|
||||||
```json
|
```json
|
||||||
|
Loading…
Reference in New Issue
Block a user