fix: stabilize admin editor and locale handling

This commit is contained in:
Codex
2026-04-23 01:10:03 +08:00
parent c8ddfdf660
commit 79182ec374
+14 -4
View File
@@ -16,10 +16,20 @@ function normalizeLocale(value: unknown) {
}
}
if (typeof value === 'string') {
if (/(^|[?&])locale=zh([&#]|$)/.test(value)) return 'zh'
if (/(^|[?&])locale=en([&#]|$)/.test(value)) return 'en'
if (/(^|\/)zh(\/|$)/.test(value)) return 'zh'
if (/(^|\/)en(\/|$)/.test(value)) return 'en'
if (value === 'en' || value === 'zh') return value
try {
const normalizedUrl = value.startsWith('http://') || value.startsWith('https://') ? value : `http://localhost${value.startsWith('/') ? '' : '/'}${value}`
const parsed = new URL(normalizedUrl)
const locale = parsed.searchParams.get('locale')
if (locale === 'en' || locale === 'zh') return locale
const firstSegment = parsed.pathname.split('/').filter(Boolean)[0]
if (firstSegment === 'en' || firstSegment === 'zh') return firstSegment
} catch {
const localeMatch = value.match(/(?:^|[?&])locale=(en|zh)(?:[&#]|$)/)
if (localeMatch?.[1] === 'en' || localeMatch?.[1] === 'zh') return localeMatch[1]
}
}
return null
}