Files
your-gold/plugins/i18n.ts
2025-06-25 13:53:36 +02:00

33 lines
908 B
TypeScript

import type { VueI18n } from "vue-i18n";
import { usePB } from "~/composables/usePB";
import type { GenericResponse } from "~/types";
export default defineNuxtPlugin(async (nuxtApp) => {
const loaded = [] as Array<string>;
const i18n = nuxtApp.$i18n as VueI18n;
const pb = usePB();
i18n.onBeforeLanguageSwitch = async (oldLocale, newLocale) => {
if (loaded.includes(newLocale)) return;
try {
// const translation = await pb.collection("translation").getList(1, 1, {
// expand: "id_lang",
// filter: `id_lang.iso='${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;
}
};
});