-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: Fix default language when chat language is null #2204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ const useUserStore = defineStore({ | |
getLanguage() { | ||
return this.userType === 1 | ||
? localStorage.getItem('MaxKB-locale') || getBrowserLang() | ||
: sessionStorage.getItem('language') | ||
: sessionStorage.getItem('language') || getBrowserLang() | ||
}, | ||
showXpack() { | ||
return this.isXPack | ||
|
@@ -124,7 +124,7 @@ const useUserStore = defineStore({ | |
async profile() { | ||
return UserApi.profile().then(async (ok) => { | ||
this.userInfo = ok.data | ||
useLocalStorage(localeConfigKey, 'zh-CN').value = ok.data?.language | ||
useLocalStorage(localeConfigKey, 'en-US').value = ok.data?.language || this.getLanguage() | ||
return this.asyncGetProfile() | ||
}) | ||
}, | ||
|
@@ -171,7 +171,7 @@ const useUserStore = defineStore({ | |
return new Promise((resolve, reject) => { | ||
UserApi.postLanguage({ language: lang }, loading) | ||
.then(async (ok) => { | ||
useLocalStorage(localeConfigKey, 'zh-CN').value = lang | ||
useLocalStorage(localeConfigKey, 'en-US').value = lang | ||
window.location.reload() | ||
resolve(ok) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code looks mostly correct but here are some minor improvements:
Here's a slightly revised version incorporating the above points: import { defineStore } from "pinia";
const useUserStore = defineStore({
id: 'user',
state: () => ({
userType: null,
userLang: '',
userInfo: {},
}),
getters: {
getLanguage() {
return this.$auth.isAuthenticated
? localStorage.getItem('MaxKB-locale') || getBrowserLang()
: sessionStorage.getItem('language');
},
showXpack() {
// Assuming $auth.isAuthenticated is part of your auth module
return this.$auth.isAuthenticated && this.useAuthModule.someFunction(); // Replace with actual function name
}
},
actions: {
...mapMutations(['setError']),
setUserInfo(userInfo) {
this.userInfo = userInfo;
},
async postLanguage(langParam) {
try {
this.setError(null);
const response = await UserApi.postLanguage({ language: langParam });
this.setUserInfo(response.data);
// Use local storage based on authentication status.
setLocale(this.$auth.isAuthenticated ? localeConfigKey : 'storage_key', langParam); // Replace with your custom logic
window.location.reload();
} catch (error) {
this.setError(error.message);
}
}
}
});
// Helper function to determine appropriate localization strategy
function setLocale(key, value) {
// Implement how you want to handle setting the locale (e.g., via store or environment variables)
}
export default useUserStore; Key Changes:
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -383,7 +383,7 @@ import type { FormInstance, FormRules, UploadFiles } from 'element-plus' | |
import { isAppIcon, isWorkFlow } from '@/utils/application' | ||
import applicationXpackApi from '@/api/application-xpack' | ||
import { MsgSuccess, MsgError } from '@/utils/message' | ||
import { langList, t } from '@/locales' | ||
import { getBrowserLang, langList, t } from '@/locales' | ||
import useStore from '@/stores' | ||
import { cloneDeep } from 'lodash' | ||
|
||
|
@@ -398,7 +398,7 @@ const emit = defineEmits(['refresh']) | |
|
||
const defaultSetting = { | ||
show_source: false, | ||
language: 'zh-CN', | ||
language: getBrowserLang(), | ||
show_history: true, | ||
draggable: true, | ||
show_guide: true, | ||
|
@@ -427,7 +427,7 @@ const form = ref<any>({ | |
|
||
const xpackForm = ref<any>({ | ||
show_source: false, | ||
language: 'zh-CN', | ||
language: getBrowserLang(), | ||
show_history: false, | ||
draggable: false, | ||
show_guide: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code has no major irregularities or potential issues based on the information available up to February 2025. Here are some recommendations for enhancement:
Overall, the code looks clean and functional for its intended purpose. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this code, there seems to be an issue with setting
sessionStorage.setItem('language')
. When using the variable directly (getBrowserLang
) without properly importing it or passing any arguments, it might result in errors. Ensure thatgetBrowserLang
is correctly imported and used.Suggested optimizations include:
getBrowserLang
at the beginning of the file to avoid confusion.getBrowserLang()
returns a value before assigning it tolocalStorage
.Also, consider handling cases where
res.data.language
might not be available (e.g., null or undefined). This could make your app more robust.