Files
web-temp/temp/language-redirect.global.ts
2025-09-09 04:09:54 +00:00

41 lines
1.4 KiB
TypeScript

export default defineNuxtRouteMiddleware(async (to, from) => {
// 서버 사이드에서만 실행
if (import.meta.client) {
return;
}
const gameDataStore = useGameDataStore();
// gameData가 로드되지 않았다면 gameData API 호출
if (!gameDataStore.gameData) {
try {
await $fetch('/api/gameData');
} catch (error) {
console.error('gameData 로드 실패:', error);
return;
}
}
const availableLangCodes = gameDataStore.gameData?.lang_codes || ['ko'];
const defaultLangCode = gameDataStore.gameData?.default_lang_code || availableLangCodes[0];
// 현재 경로에서 언어 코드 추출
const pathSegments = to.path.split('/').filter(Boolean);
const currentLangCode = pathSegments[0];
// 언어 코드가 유효한지 확인
const isValidLangCode = availableLangCodes.includes(currentLangCode);
// 유효하지 않은 언어 코드인 경우 기본 언어로 리다이렉트
if (currentLangCode && !isValidLangCode) {
const newPath = `/${defaultLangCode}${to.path.replace(`/${currentLangCode}`, '')}`;
return navigateTo(newPath, { replace: true });
}
// 언어 코드가 없는 경우 기본 언어 코드 추가
if (!currentLangCode || !availableLangCodes.includes(currentLangCode)) {
const newPath = `/${defaultLangCode}${to.path}`;
return navigateTo(newPath, { replace: true });
}
});