forked from linkong/planet
94 lines
2.9 KiB
TypeScript
94 lines
2.9 KiB
TypeScript
import i18n from './index'
|
|
import { legacyUiTextEnUS } from './legacy-ui'
|
|
import { normalizeLocale } from './locale'
|
|
|
|
type ApiErrorPayload = {
|
|
detail?: unknown
|
|
error?: unknown
|
|
message?: unknown
|
|
}
|
|
|
|
type ApiErrorLike = {
|
|
message?: unknown
|
|
response?: {
|
|
data?: ApiErrorPayload
|
|
}
|
|
}
|
|
|
|
const cjkPattern = /[\u3400-\u9fff]/
|
|
const genericEnglishFallback = 'Operation failed'
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return Boolean(value && typeof value === 'object' && !Array.isArray(value))
|
|
}
|
|
|
|
function currentLocale() {
|
|
return normalizeLocale(i18n.resolvedLanguage || i18n.language)
|
|
}
|
|
|
|
export function hasCjkText(value: string) {
|
|
return cjkPattern.test(value)
|
|
}
|
|
|
|
function englishFallbackFor(fallback: string) {
|
|
const normalized = fallback.trim()
|
|
if (!normalized) return genericEnglishFallback
|
|
if (!hasCjkText(normalized)) return normalized
|
|
return legacyUiTextEnUS[normalized] || genericEnglishFallback
|
|
}
|
|
|
|
function stringFromApiValue(value: unknown): string {
|
|
if (value === null || value === undefined || value === '') return ''
|
|
if (typeof value === 'string') return value
|
|
if (typeof value === 'number' || typeof value === 'boolean') return String(value)
|
|
if (Array.isArray(value)) {
|
|
const firstMessage = value
|
|
.map((item) => stringFromApiValue(item))
|
|
.find(Boolean)
|
|
return firstMessage || ''
|
|
}
|
|
if (isRecord(value)) {
|
|
const directMessage = stringFromApiValue(value.message || value.detail || value.error)
|
|
if (directMessage) return directMessage
|
|
const code = stringFromApiValue(value.code)
|
|
if (code) return code
|
|
try {
|
|
return JSON.stringify(value)
|
|
} catch {
|
|
return ''
|
|
}
|
|
}
|
|
return ''
|
|
}
|
|
|
|
export function localizeApiMessage(message: string, fallback = genericEnglishFallback) {
|
|
const normalized = message.trim()
|
|
if (currentLocale() !== 'en-US') return normalized || fallback
|
|
const englishFallback = englishFallbackFor(fallback)
|
|
if (!normalized) return englishFallback
|
|
const exactLegacy = legacyUiTextEnUS[normalized]
|
|
if (exactLegacy) return exactLegacy
|
|
if (hasCjkText(normalized)) return englishFallback
|
|
return normalized
|
|
}
|
|
|
|
export function describeApiValue(value: unknown, fallback = genericEnglishFallback) {
|
|
return localizeApiMessage(stringFromApiValue(value), fallback)
|
|
}
|
|
|
|
export function describeApiError(error: unknown, fallback = genericEnglishFallback) {
|
|
if (isRecord(error)) {
|
|
const apiError = error as ApiErrorLike
|
|
const payload = apiError.response?.data
|
|
if (payload) {
|
|
const responseValue = payload.detail ?? payload.message ?? payload.error
|
|
const responseMessage = describeApiValue(responseValue, fallback)
|
|
if (responseMessage) return responseMessage
|
|
}
|
|
const errorMessage = stringFromApiValue(apiError.message)
|
|
if (errorMessage) return localizeApiMessage(errorMessage, fallback)
|
|
}
|
|
if (error instanceof Error) return localizeApiMessage(error.message, fallback)
|
|
return localizeApiMessage('', fallback)
|
|
}
|