From 8be6d2b1583eca5db6a53d9e8d9f00ec4f2e4930 Mon Sep 17 00:00:00 2001 From: Samuel Luiz Date: Tue, 28 May 2024 13:56:41 -0300 Subject: [PATCH] persist locale in local storage --- frontend/src/App.vue | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/frontend/src/App.vue b/frontend/src/App.vue index d6f4e5d..d659827 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -50,6 +50,19 @@ const setThemeOnLocalStorage = (darkMode: boolean) => { localStorage.setItem("isDarkMode", JSON.stringify(darkMode)); }; +const getLocaleFromLocalStorage = () => { + const localeLs = localStorage.getItem("locale"); + if (localeLs) { + locale.value = JSON.parse(localeLs); + } else { + locale.value = isUserPortugueseSpeaker() ? "pt-BR" : "en-US"; + } +}; + +const setLocaleOnLocalStorage = (locale: string) => { + localStorage.setItem("locale", locale); +}; + onMounted(() => { getThemeFromLocalStorage(); @@ -57,11 +70,11 @@ onMounted(() => { setThemeOnLocalStorage(newVal); }); - if (isUserPortugueseSpeaker()) { - locale.value = "pt-BR"; - } else { - locale.value = "en-US"; - } + getLocaleFromLocalStorage(); + + watch(locale, (newVal) => { + setLocaleOnLocalStorage(newVal); + }); fetchData(); });