feat: Backend i18n

This commit is contained in:
fit2cloud-chenyw
2025-07-07 14:16:05 +08:00
parent cd26d6594e
commit 17fd6ffbf7
8 changed files with 124 additions and 9 deletions
+11
View File
@@ -9,6 +9,7 @@ import axios, {
} from 'axios'
import { useCache } from '@/utils/useCache'
import { getLocale } from './utils'
const { wsCache } = useCache()
// Response data structure
export interface ApiResponse<T = unknown> {
@@ -69,6 +70,16 @@ class HttpService {
if (token && config.headers) {
config.headers['X-SQLBOT-TOKEN'] = `Bearer ${token}`
}
const locale = getLocale()
if (locale) {
/* const mapping = {
'zh-CN': 'zh-CN',
en: 'en-US',
tw: 'zh-TW',
} */
/* const val = mapping[locale] || locale */
config.headers['Accept-Language'] = locale
}
if (config.url?.includes('/xpack_static/') && config.baseURL) {
config.baseURL = config.baseURL.replace('/api/v1', '')
// Skip auth for xpack_static requests
+20 -1
View File
@@ -1,5 +1,6 @@
import dayjs from 'dayjs'
import { useCache } from '@/utils/useCache'
const { wsCache } = useCache()
const getCheckDate = (timestamp: any) => {
if (!timestamp) return false
const dt = new Date(timestamp)
@@ -35,3 +36,21 @@ export function getDate(time?: Date | string | number) {
}
return new Date(time)
}
export const getBrowserLocale = () => {
const language = navigator.language
if (!language) {
return 'zh-CN'
}
if (language.startsWith('en')) {
return 'en'
}
if (language.toLowerCase().startsWith('zh')) {
const temp = language.toLowerCase().replace('_', '-')
return temp === 'zh' ? 'zh-CN' : temp === 'zh-cn' ? 'zh-CN' : 'tw'
}
return language
}
export const getLocale = () => {
return wsCache.get('user.language') || getBrowserLocale() || 'zh-CN'
}