|
|
@@ -43,16 +43,29 @@ router.beforeEach(async (to) => {
|
|
|
|
|
|
// 1. Handle Language Prefix
|
|
|
let lang = to.params.lang as string;
|
|
|
+ const savedLang = localStorage.getItem('locale') || 'en';
|
|
|
|
|
|
if (!lang) {
|
|
|
- const savedLang = localStorage.getItem('locale') || 'en';
|
|
|
- // Redirect /orders to /en/orders
|
|
|
+ // 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 };
|
|
|
+ }
|
|
|
return { path: `/${savedLang}${to.fullPath}` };
|
|
|
}
|
|
|
|
|
|
// 2. Sync i18n
|
|
|
- if (supportedLangs.includes(lang) && i18n.global.locale.value !== lang) {
|
|
|
- await setLanguage(lang);
|
|
|
+ if (supportedLangs.includes(lang)) {
|
|
|
+ if (i18n.global.locale.value !== lang) {
|
|
|
+ await setLanguage(lang);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // Unsupported lang in params? Redirect to savedLang
|
|
|
+ const newPath = to.fullPath.replace(`/${lang}`, `/${savedLang}`);
|
|
|
+ return { path: newPath };
|
|
|
}
|
|
|
|
|
|
// 3. Auth Guards
|