diff --git a/src/store/user.ts b/src/store/user.ts index d894434..1dea5a0 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -16,18 +16,23 @@ export const useUserStore = defineStore('user', { }, actions: { async setLocale(locale: string) { - let matchingLocale = Object.keys(this.localeOptions).find((option: string) => option === locale) - // If exact locale is not found, try to match the first two characters i.e primary code - matchingLocale = matchingLocale || Object.keys(this.localeOptions).find((option: string) => option.slice(0, 2) === locale.slice(0, 2)) - const newLocale = matchingLocale || this.locale - + let newLocale, matchingLocale + newLocale = this.locale + // handling if locale is not coming from userProfile try { - // update locale in state and globally - i18n.global.locale.value = newLocale - this.locale = newLocale - await userContext.setUserLocale({ newLocale }) + if (locale) { + matchingLocale = Object.keys(this.localeOptions).find((option: string) => option === locale) + // If exact locale is not found, try to match the first two characters i.e primary code + matchingLocale = matchingLocale || Object.keys(this.localeOptions).find((option: string) => option.slice(0, 2) === locale.slice(0, 2)) + newLocale = matchingLocale || this.locale + // update locale in state and globally + await userContext.setUserLocale({ newLocale }) + } } catch (error) { console.error(error) + } finally { + i18n.global.locale.value = newLocale + this.locale = newLocale } } },