feat: establish i18n foundation

This commit is contained in:
biss committed 2026-10-30 21:13:30 +08:00
1 parent 3e7d6e43d6
commit c7499f05e3
20 files changed
+520 -28

No files matched your search

+14 -6
View File
@@ -1,5 +1,6 @@
import axios from 'axios'
import { goLogin } from '../utils/navigate'
import { currentLocale, hasTranslation, translate } from '../i18n'
import {
authStorageKeys,
clearAuthSession,
@@ -29,6 +30,7 @@ function isAuthenticationRequest(url?: string) {
}
http.interceptors.request.use(async (config) => {
config.headers['Accept-Language'] = currentLocale.value
if (!isAuthenticationRequest(config.url) && !config.skipAuthSession) {
const activeToken = await refreshIfNeeded(true)
if (activeToken) markActivity()
@@ -62,9 +64,15 @@ http.interceptors.response.use(
)
export function apiErrorMessage(error: unknown) {
if (!axios.isAxiosError(error)) return '操作失败,请稍后重试。'
if (!axios.isAxiosError(error)) return translate('errors.generic')
const status = error.response?.status
const data = error.response?.data
const code = typeof data?.code === 'string' ? data.code.trim() : ''
const codeKey = code ? `api.${code}` : ''
if (codeKey && hasTranslation(codeKey)) {
const args = data?.args && typeof data.args === 'object' ? data.args : undefined
return translate(codeKey, args)
}
const message = typeof data?.message === 'string' ? data.message.trim() : ''
if (message) return message
if (data?.errors && typeof data.errors === 'object') {
@@ -79,11 +87,11 @@ export function apiErrorMessage(error: unknown) {
if (detail) return detail
const title = typeof data?.title === 'string' ? data.title.trim() : ''
if (title) return title
if (status === 403) return '您没有权限执行此操作,请联系管理员。'
if (status === 404) return '请求的资源不存在。'
if (status === 401) return '登录已过期,请重新登录。'
if (status && status >= 500) return '服务器繁忙,请稍后重试。'
return '操作失败,请检查网络连接。'
if (status === 403) return translate('errors.forbidden')
if (status === 404) return translate('errors.notFound')
if (status === 401) return translate('errors.unauthorized')
if (status && status >= 500) return translate('errors.server')
return translate('errors.network')
}
export default http