mirror of
https://gh-proxy.org/https://github.com/klinsmaya/SQLBot.git
synced 2026-11-08 01:06:59 +08:00
feat: Backend i18n
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user