0
1
Fork 0

Update lang

This commit is contained in:
William Buezas 2024-10-07 17:38:39 -03:00
parent c9812cd320
commit 4c041f42b2
6 changed files with 50 additions and 41 deletions

View File

@ -13,8 +13,13 @@ const appStore = useAppStore();
const refreshContentKey = ref(0); const refreshContentKey = ref(0);
const { mainUser, supplantedUser } = storeToRefs(userStore); const { mainUser, supplantedUser } = storeToRefs(userStore);
const { title, subtitle, useRightDrawer, rightDrawerOpen, filteredMenuItems } = const {
storeToRefs(appStore); menuTitle,
subtitle,
useRightDrawer,
rightDrawerOpen,
filteredMenuItems
} = storeToRefs(appStore);
const actions = ref(null); const actions = ref(null);
const leftDrawerOpen = ref(false); const leftDrawerOpen = ref(false);
@ -53,7 +58,7 @@ const logoutSupplantedUser = async () => {
@click="toggleLeftDrawer" @click="toggleLeftDrawer"
/> />
<QToolbarTitle> <QToolbarTitle>
{{ title }} {{ menuTitle }}
<div v-if="subtitle" class="subtitle text-caption"> <div v-if="subtitle" class="subtitle text-caption">
{{ subtitle }} {{ subtitle }}
</div> </div>

View File

@ -57,6 +57,15 @@ const formatMailData = data => {
data.isToBeMailed = Boolean(data.isToBeMailed); data.isToBeMailed = Boolean(data.isToBeMailed);
}; };
const updateConfigLang = async lang => {
try {
await vnFormRef.value.submit();
userStore.updateUserLang(lang);
} catch (error) {
console.error(error);
}
};
onMounted(() => fetchLanguagesSql()); onMounted(() => fetchLanguagesSql());
</script> </script>
@ -113,9 +122,7 @@ onMounted(() => fetchLanguagesSql());
option-label="name" option-label="name"
option-value="code" option-value="code"
:options="langOptions" :options="langOptions"
@update:model-value=" @update:model-value="updateConfigLang(data.lang)"
userStore.updateUserLang(data.lang)
"
/> />
</template> </template>
<template #extraForm> <template #extraForm>

View File

@ -5,14 +5,18 @@ import { useI18n } from 'vue-i18n';
import { useUserStore } from 'stores/user'; import { useUserStore } from 'stores/user';
import useNotify from 'src/composables/useNotify.js'; import useNotify from 'src/composables/useNotify.js';
import { useRouter, useRoute } from 'vue-router'; import { useRouter, useRoute } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useAppStore } from 'src/stores/app';
const { notify } = useNotify(); const { notify } = useNotify();
const { t } = useI18n(); const { t } = useI18n();
const { locale } = useI18n({ useScope: 'global' }); const { locale } = useI18n({ useScope: 'global' });
const userStore = useUserStore(); const userStore = useUserStore();
const appStore = useAppStore();
const route = useRoute(); const route = useRoute();
const router = useRouter(); const router = useRouter();
const { siteLang, localeOptions } = storeToRefs(appStore);
const email = ref(null); const email = ref(null);
const password = ref(null); const password = ref(null);
const remember = ref(false); const remember = ref(false);
@ -20,12 +24,11 @@ const showPwd = ref(false);
const selectedLocaleValue = computed({ const selectedLocaleValue = computed({
get() { get() {
return userStore.localeOptions.find( return siteLang.value;
option => option.lang === locale.value
).value;
}, },
set(value) { set(value) {
locale.value = value; locale.value = value;
appStore.updateSiteLocale(value);
} }
}); });
@ -44,7 +47,6 @@ onMounted(() => {
const onLogin = async () => { const onLogin = async () => {
await userStore.fetchUser(); await userStore.fetchUser();
await userStore.updateUserLang(selectedLocaleValue.value);
await router.push('/'); await router.push('/');
}; };
@ -90,7 +92,7 @@ const loginAsGuest = async () => {
/> />
<QSelect <QSelect
v-model="selectedLocaleValue" v-model="selectedLocaleValue"
:options="userStore.localeOptions" :options="localeOptions"
:label="t('language')" :label="t('language')"
option-value="lang" option-value="lang"
dense dense

View File

@ -7,7 +7,6 @@ import {
createWebHashHistory createWebHashHistory
} from 'vue-router'; } from 'vue-router';
import routes from './routes'; import routes from './routes';
import { i18n } from 'src/boot/i18n';
/* /*
* If not building with SSR mode, you can * If not building with SSR mode, you can
@ -40,10 +39,9 @@ export default route(function (/* { store, ssrContext } */) {
Router.afterEach((to, from) => { Router.afterEach((to, from) => {
if (from.name === to.name) return; if (from.name === to.name) return;
const app = useAppStore(); const app = useAppStore();
app.$patch({ app.$patch({
title: i18n.global.t( title: to.meta.title || 'Home',
to.meta.title ? `titles.${to.meta.title}` : 'titles.Home'
),
subtitle: null, subtitle: null,
useRightDrawer: false, useRightDrawer: false,
rightDrawerOpen: true rightDrawerOpen: true

View File

@ -6,6 +6,7 @@ import { useQuasar } from 'quasar';
const { notify } = useNotify(); const { notify } = useNotify();
const storageOrderName = 'hederaBasket'; const storageOrderName = 'hederaBasket';
const { t } = i18n.global;
export const useAppStore = defineStore('hedera', { export const useAppStore = defineStore('hedera', {
state: () => ({ state: () => ({
@ -23,7 +24,15 @@ export const useAppStore = defineStore('hedera', {
months: [], months: [],
daysShort: [], daysShort: [],
monthsShort: [] monthsShort: []
} },
siteLang: null,
localeOptions: [
{ label: t('langs.en'), lang: 'en-US', value: 'en' },
{ label: t('langs.es'), lang: 'es-ES', value: 'es' },
{ label: t('langs.ca'), lang: 'ca-ES', value: 'ca' },
{ label: t('langs.fr'), lang: 'fr-FR', value: 'fr' },
{ label: t('langs.pt'), lang: 'pt-PT', value: 'pt' }
]
}), }),
actions: { actions: {
async getMenuLinks() { async getMenuLinks() {
@ -67,6 +76,7 @@ export const useAppStore = defineStore('hedera', {
}, },
async init() { async init() {
this.updateSiteLocale(localStorage.getItem('siteLang') || 'es-ES');
this.getBasketOrderId(); this.getBasketOrderId();
this.getLocaleDates(); this.getLocaleDates();
}, },
@ -143,6 +153,13 @@ export const useAppStore = defineStore('hedera', {
onLogout() { onLogout() {
this.unloadOrder(); this.unloadOrder();
this.menuEssentialLinks = []; this.menuEssentialLinks = [];
},
updateSiteLocale(locale = null) {
const _locale = locale || 'es-ES';
i18n.global.locale.value = _locale;
this.siteLang = _locale;
localStorage.setItem('siteLang', _locale);
} }
}, },
getters: { getters: {
@ -151,6 +168,9 @@ export const useAppStore = defineStore('hedera', {
item => !state.hiddenMenuLinks.has(item.description) item => !state.hiddenMenuLinks.has(item.description)
); );
}, },
siteLocaleOption: state =>
state.localeOptions.find(l => l.value === state.siteLang),
menuTitle: state => t(`titles.${state.title}`),
isMobile() { isMobile() {
const $q = useQuasar(); const $q = useQuasar();
return $q?.screen?.width <= 768; return $q?.screen?.width <= 768;

View File

@ -1,11 +1,9 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { ref, computed, watch } from 'vue'; import { ref, computed, watch } from 'vue';
import { api, jApi } from 'boot/axios'; import { api, jApi } from 'boot/axios';
import { i18n } from 'src/boot/i18n';
import useNotify from 'src/composables/useNotify.js'; import useNotify from 'src/composables/useNotify.js';
import { useAppStore } from 'src/stores/app.js'; import { useAppStore } from 'src/stores/app.js';
const { t } = i18n.global;
const { notify } = useNotify(); const { notify } = useNotify();
const TOKEN_MULTIMEDIA = 'tokenMultimedia'; const TOKEN_MULTIMEDIA = 'tokenMultimedia';
const TOKEN = 'token'; const TOKEN = 'token';
@ -17,13 +15,7 @@ export const useUserStore = defineStore('user', () => {
const user = ref(null); // Usuario en uso => supplantedUser || mainUser const user = ref(null); // Usuario en uso => supplantedUser || mainUser
const supplantedUser = ref(null); // Usuario suplantado const supplantedUser = ref(null); // Usuario suplantado
const mainUser = ref(null); // Usuario principal logueado const mainUser = ref(null); // Usuario principal logueado
const localeOptions = ref([
{ label: t('langs.en'), lang: 'en-US', value: 'en' },
{ label: t('langs.es'), lang: 'es-ES', value: 'es' },
{ label: t('langs.ca'), lang: 'ca-ES', value: 'ca' },
{ label: t('langs.fr'), lang: 'fr-FR', value: 'fr' },
{ label: t('langs.pt'), lang: 'pt-PT', value: 'pt' }
]);
const keepLogin = ref(false); const keepLogin = ref(false);
const intervalId = ref(null); const intervalId = ref(null);
const isCheckingToken = ref(false); const isCheckingToken = ref(false);
@ -31,9 +23,6 @@ export const useUserStore = defineStore('user', () => {
let router; let router;
const loggedIn = computed(() => !!token.value); const loggedIn = computed(() => !!token.value);
const userLocaleOption = computed(() =>
localeOptions.value?.find(l => l.value === user.value?.lang)
);
const storage = computed(() => const storage = computed(() =>
keepLogin.value ? localStorage : sessionStorage keepLogin.value ? localStorage : sessionStorage
); );
@ -51,7 +40,6 @@ export const useUserStore = defineStore('user', () => {
await fetchTokenConfig(); await fetchTokenConfig();
await fetchUser(); await fetchUser();
await supplantInit(); await supplantInit();
updateSiteLocale();
startInterval(); startInterval();
}; };
@ -277,17 +265,9 @@ export const useUserStore = defineStore('user', () => {
await fetchUser(); await fetchUser();
}; };
const updateSiteLocale = (locale = null) => {
i18n.global.locale.value =
locale || userLocaleOption.value?.lang || 'en-US';
};
const updateUserLang = async lang => { const updateUserLang = async lang => {
if (!user.value || user.value.lang === lang) return; if (!user.value || user.value.lang === lang) return;
user.value.lang = lang; user.value.lang = lang;
updateSiteLocale();
notify(t('dataSaved'), 'positive');
}; };
const $reset = () => { const $reset = () => {
@ -316,13 +296,11 @@ export const useUserStore = defineStore('user', () => {
user, user,
supplantedUser, supplantedUser,
mainUser, mainUser,
localeOptions,
keepLogin, keepLogin,
intervalId, intervalId,
isCheckingToken, isCheckingToken,
tokenConfig, tokenConfig,
loggedIn, loggedIn,
userLocaleOption,
storage, storage,
getToken, getToken,
getTokenMultimedia, getTokenMultimedia,
@ -342,7 +320,6 @@ export const useUserStore = defineStore('user', () => {
supplantUser, supplantUser,
supplantInit, supplantInit,
logoutSupplantedUser, logoutSupplantedUser,
updateSiteLocale,
updateUserLang, updateUserLang,
init, init,
$reset $reset