Skip to content
Snippets Groups Projects
index.js 1.74 KiB
Newer Older
  • Learn to ignore specific revisions
  • Cee Nell's avatar
    Cee Nell committed
    import { createRouter, createWebHistory } from 'vue-router';
    
    import { setLocale } from './../i18n';
    
    Cee Nell's avatar
    Cee Nell committed
    import VisualizationView from '../views/VisualizationView.vue';
    
    Cee Nell's avatar
    Cee Nell committed
    
    const routes = [
    
      {
        path: '/', // Root path
        redirect: () => {
          // Detect the user's browser language
          const userLang = navigator.language || navigator.userLanguage;
          const defaultLocale = userLang.startsWith('es') ? 'es' : 'en';
          return `/${defaultLocale}`; // Redirect to '/en' or '/es'
        },
      },
    
    Cee Nell's avatar
    Cee Nell committed
      {
    
        path: '/:lang(en|es)', // Language prefix is required
    
    Cee Nell's avatar
    Cee Nell committed
        name: 'VisualizationContent',
    
    Cee Nell's avatar
    Cee Nell committed
        component: VisualizationView,
        meta: {
    
          requiresLang: true,
    
    Cee Nell's avatar
    Cee Nell committed
        },
      },
    
    Cee Nell's avatar
    Cee Nell committed
      {
    
        path: '/index.html', // Handle direct navigation to /index.html
        redirect: () => {
          const userLang = navigator.language || navigator.userLanguage;
          const defaultLocale = userLang.startsWith('es') ? 'es' : 'en';
          return `/${defaultLocale}`; // Redirect to '/en' or '/es'
        },
      },
      {
        path: '/:lang(en|es)', // Language prefix route
    
    Cee Nell's avatar
    Cee Nell committed
        name: 'VisualizationContent',
        component: VisualizationView,
        meta: {
          requiresLang: true,
        },
      },
    
    Cee Nell's avatar
    Cee Nell committed
      {
    
        path: '/:lang(en|es)?/index.html', // Language prefix route
    
    Cee Nell's avatar
    Cee Nell committed
        name: 'Index',
    
    Cee Nell's avatar
    Cee Nell committed
        component: VisualizationView,
        meta: {
          requiresLang: true,
    
    Cee Nell's avatar
    Cee Nell committed
        },
      },
    ];
    
    
    Cee Nell's avatar
    Cee Nell committed
    
    const router = createRouter({
    
    Cee Nell's avatar
    Cee Nell committed
      history: createWebHashHistory(import.meta.env.BASE_URL),
    
    Cee Nell's avatar
    Cee Nell committed
      routes
    
    Cee Nell's avatar
    Cee Nell committed
    })
    
    
    Cee Nell's avatar
    Cee Nell committed
    // Global beforeEach guard for setting up the locale
    router.beforeEach(async (to, from, next) => {
      const lang = to.params.lang || 'en'; // Default to 'en' if no language is provided
    
      // Set the i18n locale
      if (to.meta.requiresLang) {
        await setLocale(lang); // Asynchronously update the locale
      }
    
      next();
    });
    
    export default router;