fix: stabilize admin editor and locale handling
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user