fix langs

This commit is contained in:
2025-06-25 21:19:40 +02:00
parent 9d7fd3d52a
commit edf3036e6a
24 changed files with 266 additions and 776 deletions

View File

@ -20,16 +20,17 @@ declare module 'vue' {
export class Session {
cookieData = reactive({} as CookieData);
cookieData = ref({} as CookieData);
urlParams = new URLSearchParams()
currentLanguageIso = ref("" as string )
currentCountryIso = ref("" as string )
currentCurrencyIso = ref("" as string )
currentLanguageIso = ref("" as string)
currentCountryIso = ref("" as string)
currentCurrencyIso = ref("" as string)
route = {} as RouteLocation
router = {} as Router
i18n = {} as VueI18n
sessionOngoing: boolean = false
constructor(i18n: VueI18n, router: Router) {
this.route = router.currentRoute.value
@ -44,6 +45,8 @@ export class Session {
async loadSession() {
if (this.sessionOngoing) return
this.sessionOngoing = true
this.setQueryParams()
const { data } = await useMyFetch<GenericResponse<CookieData>>(`/api/public/cookie?${this.urlParams.toString()}`, {
headers: {
@ -53,10 +56,11 @@ export class Session {
throw new Error(`HTTP error: ${status}`);
},
});
this.cookieData = data;
this.currentCountryIso.value = this.cookieData.country.iso_code
this.currentLanguageIso.value = this.cookieData.language.iso_code
this.currentCurrencyIso.value = this.cookieData.currency.iso_code
this.cookieData.value = data;
this.currentCountryIso.value = this.cookieData.value.country.iso_code
this.currentLanguageIso.value = this.cookieData.value.language.iso_code
this.currentCurrencyIso.value = this.cookieData.value.currency.iso_code
setTimeout(() => this.sessionOngoing = false, 2000)
}
setLanguage(iso: string) {
@ -94,33 +98,23 @@ export class Session {
}
let session = {} as Session;
export const useSession = () => session;
export default defineNuxtPlugin(async (nuxtApp) => {
const loaded = [] as Array<string>;
const { $i18n: i18n } = nuxtApp as unknown as { $i18n: VueI18n };
const { $router: router } = nuxtApp as unknown as { $router: Router };
i18n.onBeforeLanguageSwitch = async (oldLocale, newLocale) => {
if (import.meta.browser) {
session.setLanguage(newLocale)
await session.loadSession()
}
i18n.onBeforeLanguageSwitch = async (_, newLocale) => {
if (loaded.includes(newLocale)) return;
try {
loaded.push(newLocale);
const { data } = await useMyFetch<GenericResponse<object>>(
"/api/public/front/translation"
);
i18n.setLocaleMessage(newLocale, data);
loaded.push(newLocale);
} catch (err) {
console.error("❌ Failed to load translation for locale:", newLocale);
throw err;
@ -130,9 +124,9 @@ export default defineNuxtPlugin(async (nuxtApp) => {
session = new Session(i18n, router)
const session = new Session(i18n, router)
await session.loadSession();
nuxtApp.vueApp.provide("session", session);
nuxtApp.provide("session", session);
});

View File

@ -3,6 +3,8 @@ import { defineNuxtPlugin } from "#app";
export default defineNuxtPlugin(async () => {
const menuStore = useMenuStore();
await menuStore.loadMenu();
await menuStore.getCountryList();
await menuStore.getCurrencies();
await menuStore.getLocales();
const store = useStore();
await store.getMinValue();
await store.getCalculator();
});