-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
fix: Fix default language #2202
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
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 appears mostly correct but has a couple of minor optimizations and improvements:
Optimized code: const useUserStore = defineStore({
state() {
return {
userType: 1,
isXPack: false,
userInfo: {}
};
},
getters: {
getLanguage(): string {
return (
this.userType === 1 && localStorage.getItem('MAXKB-locale')
? localStorage.getItem('MAXKB-locale') || getBrowserLang()
: sessionStorage.getItem('language') || getBrowserLang()
);
}
},
actions: {
async profile(lang?: string): Promise<void> {
const data = await UserApi.profile();
this.userInfo = data.data;
// Update local storage with new language preference if provided
this.updateLocale(lang);
await this.asyncGetProfile();
},
updateLocale(language?: string): void {
const key = language ? 'en-US' : "zh-CN";
useLocalStorage(key).value = language || this.getLanguage();
// Optionally reload page to apply changes
window.location.reload(false);
},
async postLanguage(lang: string): Promise<void> {
await UserApi.postLanguage({language}, (loading) => {});
// Update local storage after successful change
this.updateLocale(lang);
}
}
}); Explanation of 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 snippet has some small improvements and optimizations:
Here's the revised version: @@ -383,6 +383,7 @@ import { FormInstance, FormRules, UploadFiles } from 'element-plus'
import { isAppIcon, isWorkFlow } from '@/utils/application'
import applicationXpackApi from '@/api/application-xpack'
+// Removed because not used
// import { isAppIcon, isWorkFlow } from '@/utils/application'
import { MsgSuccess, MsgError } from '@/utils/message'
import { getBrowserLang, langList, t } from '@/locales'
import useStore from '@/stores'
import { cloneDeep } from 'lodash'
@@ -398,6 +399,7 @@ const emit = defineEmits(['refresh'])
const defaultSetting = {
- show_source: false,
- language: 'zh-CN',
+ show_source: false,
+ // Use browser-detected language for localization
+ language: getBrowserLang(),
show_history: true,
draggable: true,
show_guide: true,
@@ -427,6 +428,7 @@ const form = ref<any>({
const xpackForm = ref<any>({
show_source: false,
- language: 'zh-CN',
+ language: getBrowserLang(), // Use browser-detected language for localization
show_history: false,
draggable: false,
show_guide: false); Additional SuggestionConsider adding a fallback mechanism for cases where language: getBrowserLang() || 'en-US'; This ensures that your application still functions correctly even if the user's browser does not detect a supported language. |
||
|
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.
Your code seems to be well-structured and follows best practices for using Pinia and Vue.js store. Here are some minor suggestions for improvement:
Type Annotations: The
applicationStore
function is returning a Promise immediately after being resolved without awaiting it in the.then()
block. This might cause unexpected behavior. Consider awaiting it explicitly.Consistent Usage of
await
: Ensure that you consistently use either synchronous or asynchronous operations based on where promises need resolution.Variable Naming: Use consistent naming conventions for variables and functions, especially those related to localization, like
sessionStorage.setItem
,getBrowserLang()
. Using descriptive names can make the code more readable.Here's an optimized version of your code with these improvements in mind:
Optimization Suggestions:
browserLang
to only fetch it when necessary.In summary, everything looks good syntactically. However, consider making changes around the promise handling and variable naming to maintain consistency and potentially improve performance.