import {
configuration,
getPathWithoutLocale,
localeFlatMap,
type Locales,
} from 'intlayer';
import { createIntlayerClient } from 'vue-intlayer';
import { createRouter, createWebHistory } from 'vue-router';
import HomeView from './views/home/HomeView.vue';
import RootView from './views/root/Root.vue';
// Get internationalization configuration
const { internationalization, middleware } = configuration;
const { defaultLocale } = internationalization;
const routes = localeFlatMap((localizedData) => [
{
path: `${localizedData.urlPrefix}/`,
name: `Root-${localizedData.locale}`,
component: RootView,
meta: {
locale: localizedData.locale,
},
},
{
path: `${localizedData.urlPrefix}/home`,
name: `Home-${localizedData.locale}`,
component: HomeView,
meta: {
locale: localizedData.locale,
},
},
]);
// Create the router instance
export const router = createRouter({
history: createWebHistory(),
routes,
});
// Add navigation guard for locale handling
router.beforeEach((to, _from, next) => {
const client = createIntlayerClient();
const metaLocale = to.meta.locale as Locales | undefined;
if (metaLocale) {
// Reuse the locale defined in the route meta
client.setLocale(metaLocale);
next();
} else {
// Fallback: no locale in meta, possibly unmatched route
// Optional: handle 404 or redirect to default locale
client.setLocale(defaultLocale);
if (middleware.prefixDefault) {
next(`/${defaultLocale}${getPathWithoutLocale(to.path)}`);
} else {
next(getPathWithoutLocale(to.path));
}
}
});