add pocketbase
This commit is contained in:
32
plugins/i18n.ts
Normal file
32
plugins/i18n.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import type { VueI18n } from "vue-i18n";
|
||||
import { usePB } from "~/composables/usePB";
|
||||
|
||||
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}'`,
|
||||
});
|
||||
|
||||
if (translation.totalItems === 1) {
|
||||
i18n.setLocaleMessage(newLocale, translation.items[0].data);
|
||||
} else {
|
||||
i18n.setLocaleMessage(newLocale, {});
|
||||
}
|
||||
|
||||
loaded.push(newLocale);
|
||||
} catch (err) {
|
||||
console.error("❌ Failed to load translation for locale:", newLocale);
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
});
|
Reference in New Issue
Block a user