Browse Source

debug: fix and log language redirect logic

unknown 2 ngày trước cách đây
mục cha
commit
6533c6b49c
1 tập tin đã thay đổi với 15 bổ sung10 xóa
  1. 15 10
      src/router/index.ts

+ 15 - 10
src/router/index.ts

@@ -45,15 +45,20 @@ router.beforeEach(async (to) => {
   let lang = to.params.lang as string;
   const savedLang = localStorage.getItem('locale') || 'en';
   
+  console.log(`Router: path=${to.path}, langParam=${lang}, stored=${savedLang}`);
+
   if (!lang) {
-    // If no lang prefix (e.g. /auth), redirect to /en/auth or /savedLang/auth
-    // Check if the path already starts with a lang-like prefix that isn't supported
-    const firstSegment = to.path.split('/')[1];
-    if (firstSegment && !supportedLangs.includes(firstSegment) && firstSegment.length === 2) {
-      // It looks like an invalid lang code (e.g. /fr/auth), replace it
-      const newPath = to.fullPath.replace(`/${firstSegment}`, `/${savedLang}`);
-      return { path: newPath };
+    const segments = to.path.split('/').filter(Boolean);
+    const firstSegment = segments[0];
+    
+    // Check if first segment looks like a language code but isn't supported
+    if (firstSegment && firstSegment.length === 2 && !supportedLangs.includes(firstSegment)) {
+      // It's a fake lang like /fr/auth -> redirect to /ru/auth
+      const remainingPath = segments.slice(1).join('/');
+      return { path: `/${savedLang}/${remainingPath}`, query: to.query, hash: to.hash };
     }
+    
+    // Simple path like /auth -> /ru/auth
     return { path: `/${savedLang}${to.fullPath}` };
   }
 
@@ -63,9 +68,9 @@ router.beforeEach(async (to) => {
       await setLanguage(lang);
     }
   } else {
-    // Unsupported lang in params? Redirect to savedLang
-    const newPath = to.fullPath.replace(`/${lang}`, `/${savedLang}`);
-    return { path: newPath };
+    // This part should technically not be reached with the current regex
+    // but as a safety measure:
+    return { path: `/${savedLang}/`, query: to.query };
   }
 
   // 3. Auth Guards