Merge branch 'pr@main@i18n'

This commit is contained in:
wangdan-fit2cloud 2025-01-20 21:48:05 +08:00
commit b1cd9578fc
8 changed files with 29 additions and 16 deletions

2
ui/env.d.ts vendored
View File

@ -1,5 +1,7 @@
/// <reference types="vite/client" /> /// <reference types="vite/client" />
declare module 'element-plus/dist/locale/zh-cn.mjs' declare module 'element-plus/dist/locale/zh-cn.mjs'
declare module 'element-plus/dist/locale/en.mjs'
declare module 'element-plus/dist/locale/zh-tw.mjs'
declare module 'markdown-it-task-lists' declare module 'markdown-it-task-lists'
declare module 'markdown-it-abbr' declare module 'markdown-it-abbr'
declare module 'markdown-it-anchor' declare module 'markdown-it-anchor'

View File

@ -16,7 +16,7 @@ import bus from '@/bus'
import { ref, nextTick, onMounted } from 'vue' import { ref, nextTick, onMounted } from 'vue'
import { t } from '@/locales' import { t } from '@/locales'
const isOpen = ref<boolean>(false) const isOpen = ref<boolean>(false)
const eventVal = ref() const eventVal = ref<any>({})
function getSelection() { function getSelection() {
const selection = window.getSelection() const selection = window.getSelection()
if (selection && selection.anchorNode == null) { if (selection && selection.anchorNode == null) {

View File

@ -2,6 +2,8 @@ import '@/styles/index.scss'
import ElementPlus from 'element-plus' import ElementPlus from 'element-plus'
import * as ElementPlusIcons from '@element-plus/icons-vue' import * as ElementPlusIcons from '@element-plus/icons-vue'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs' import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import enUs from 'element-plus/dist/locale/en.mjs'
import zhTW from 'element-plus/dist/locale/zh-tw.mjs'
import { createApp } from 'vue' import { createApp } from 'vue'
import { store } from '@/stores' import { store } from '@/stores'
import directives from '@/directives' import directives from '@/directives'
@ -10,7 +12,6 @@ import router from '@/router'
import Components from '@/components' import Components from '@/components'
import i18n from './locales' import i18n from './locales'
import { config } from 'md-editor-v3' import { config } from 'md-editor-v3'
import screenfull from 'screenfull' import screenfull from 'screenfull'
import katex from 'katex' import katex from 'katex'
@ -51,8 +52,13 @@ app.use(directives)
for (const [key, component] of Object.entries(ElementPlusIcons)) { for (const [key, component] of Object.entries(ElementPlusIcons)) {
app.component(key, component) app.component(key, component)
} }
const locale_map: any = {
'zh-CN': zhCn,
'zh-Hant': zhTW,
'en-US': enUs
}
app.use(ElementPlus, { app.use(ElementPlus, {
locale: zhCn locale: locale_map[localStorage.getItem('MaxKB-locale') || 'zh-CN']
}) })
app.use(router) app.use(router)

View File

@ -25,6 +25,8 @@ instance.interceptors.request.use(
} }
const { user } = useStore() const { user } = useStore()
const token = user.getToken() const token = user.getToken()
const language = user.getLanguage()
config.headers['Accept-Language'] = `${language}`
if (token) { if (token) {
config.headers['AUTHORIZATION'] = `${token}` config.headers['AUTHORIZATION'] = `${token}`
} }
@ -208,7 +210,7 @@ export const postStream: (url: string, data?: unknown) => Promise<Result<any> |
if (token) { if (token) {
headers['AUTHORIZATION'] = `${token}` headers['AUTHORIZATION'] = `${token}`
} }
headers['Accept-Language'] = `${language}` headers['Accept-Language'] = `${language}`
return fetch(url, { return fetch(url, {
method: 'POST', method: 'POST',
body: data ? JSON.stringify(data) : undefined, body: data ? JSON.stringify(data) : undefined,

View File

@ -4,12 +4,10 @@ import applicationXpackApi from '@/api/application-xpack'
import { type Ref } from 'vue' import { type Ref } from 'vue'
import useUserStore from './user' import useUserStore from './user'
const useApplicationStore = defineStore({ const useApplicationStore = defineStore({
id: 'application', id: 'application',
state: () => ({ state: () => ({
location: `${window.location.origin}/ui/chat/`, location: `${window.location.origin}/ui/chat/`
userLanguage: ''
}), }),
actions: { actions: {
async asyncGetAllApplication() { async asyncGetAllApplication() {
@ -80,9 +78,10 @@ const useApplicationStore = defineStore({
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
applicationApi applicationApi
.getAppProfile(loading) .getAppProfile(loading)
.then((data) => { .then((res) => {
this.userLanguage = data.data?.language sessionStorage.setItem('language', res.data?.language)
resolve(data)
resolve(res)
}) })
.catch((error) => { .catch((error) => {
reject(error) reject(error)

View File

@ -6,7 +6,6 @@ import UserApi from '@/api/user'
import ThemeApi from '@/api/theme' import ThemeApi from '@/api/theme'
import { useElementPlusTheme } from 'use-element-plus-theme' import { useElementPlusTheme } from 'use-element-plus-theme'
import { defaultPlatformSetting } from '@/utils/theme' import { defaultPlatformSetting } from '@/utils/theme'
import useApplicationStore from './application'
import { useLocalStorage } from '@vueuse/core' import { useLocalStorage } from '@vueuse/core'
import { localeConfigKey } from '@/locales/index' import { localeConfigKey } from '@/locales/index'
export interface userStateTypes { export interface userStateTypes {
@ -33,10 +32,9 @@ const useUserStore = defineStore({
}), }),
actions: { actions: {
getLanguage() { getLanguage() {
const application = useApplicationStore()
return this.userType === 1 return this.userType === 1
? this.userInfo?.language || localStorage.getItem('language') ? localStorage.getItem('MaxKB-locale')
: application?.userLanguage : sessionStorage.getItem('language')
}, },
showXpack() { showXpack() {
return this.isXPack return this.isXPack
@ -127,7 +125,6 @@ const useUserStore = defineStore({
return UserApi.profile().then(async (ok) => { return UserApi.profile().then(async (ok) => {
this.userInfo = ok.data this.userInfo = ok.data
useLocalStorage(localeConfigKey, 'zh-CN').value = ok.data?.language useLocalStorage(localeConfigKey, 'zh-CN').value = ok.data?.language
// localStorage.setItem('language', ok.data?.language)
return this.asyncGetProfile() return this.asyncGetProfile()
}) })
}, },
@ -174,8 +171,8 @@ const useUserStore = defineStore({
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
UserApi.postLanguage({ language: lang }, loading) UserApi.postLanguage({ language: lang }, loading)
.then(async (ok) => { .then(async (ok) => {
useLocalStorage(localeConfigKey, 'zh-CN').value = lang
window.location.reload() window.location.reload()
resolve(ok) resolve(ok)
}) })
.catch((error) => { .catch((error) => {

View File

@ -24,6 +24,8 @@ import { useRoute } from 'vue-router'
import useStore from '@/stores' import useStore from '@/stores'
import Auth from '@/views/chat/auth/index.vue' import Auth from '@/views/chat/auth/index.vue'
import { hexToRgba } from '@/utils/theme' import { hexToRgba } from '@/utils/theme'
import { useI18n } from 'vue-i18n'
const { locale } = useI18n({ useScope: 'global' })
const route = useRoute() const route = useRoute()
const { application, user } = useStore() const { application, user } = useStore()
@ -78,6 +80,7 @@ const init_data_end = ref<boolean>(false)
const applicationAvailable = ref<boolean>(true) const applicationAvailable = ref<boolean>(true)
function getAppProfile() { function getAppProfile() {
return application.asyncGetAppProfile(loading).then((res: any) => { return application.asyncGetAppProfile(loading).then((res: any) => {
locale.value = res.data?.language
show_history.value = res.data?.show_history show_history.value = res.data?.show_history
application_profile.value = res.data application_profile.value = res.data
}) })

View File

@ -106,8 +106,11 @@ import type { FormInstance, FormRules } from 'element-plus'
import useStore from '@/stores' import useStore from '@/stores'
import authApi from '@/api/auth-setting' import authApi from '@/api/auth-setting'
import { MsgConfirm, MsgSuccess } from '@/utils/message' import { MsgConfirm, MsgSuccess } from '@/utils/message'
import { t } from '@/locales' import { t } from '@/locales'
import QrCodeTab from '@/views/login/components/QrCodeTab.vue' import QrCodeTab from '@/views/login/components/QrCodeTab.vue'
import { useI18n } from 'vue-i18n'
const { locale } = useI18n({ useScope: 'global' })
const loading = ref<boolean>(false) const loading = ref<boolean>(false)
const { user } = useStore() const { user } = useStore()
const router = useRouter() const router = useRouter()
@ -212,6 +215,7 @@ const login = () => {
user user
.login(loginMode.value, loginForm.value.username, loginForm.value.password) .login(loginMode.value, loginForm.value.username, loginForm.value.password)
.then(() => { .then(() => {
locale.value = localStorage.getItem('MaxKB-locale') || 'zh-CN'
router.push({ name: 'home' }) router.push({ name: 'home' })
}) })
.finally(() => (loading.value = false)) .finally(() => (loading.value = false))