Replace cookie by local storage to store language

This commit is contained in:
Nolway
2021-12-18 22:18:35 +01:00
committed by Alexis Faizeau
parent 8286cdd41d
commit 41ef9fd49f
5 changed files with 10 additions and 34 deletions
+7 -8
View File
@@ -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 {