Skip to content

Commit 7892f5b

Browse files
committed
Fixed: Fix the language window synchronization issue
1 parent 053cba8 commit 7892f5b

10 files changed

Lines changed: 138 additions & 145 deletions

File tree

ui/components/Modal/modal.vue

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
<script setup lang="ts">
22
withDefaults(
33
defineProps<{
4-
open: boolean
5-
title?: string
6-
description?: string
4+
open: boolean;
5+
title?: string;
6+
description?: string;
77
}>(),
88
{
9-
title: '',
10-
description: ''
9+
title: "",
10+
description: ""
1111
}
1212
);
1313
1414
const emits = defineEmits<{
15-
(e: 'update:open', value: boolean): void
16-
(e: 'clipboard', value: string): void
17-
(e: 'confirm'): void
15+
(e: "update:open", value: boolean): void;
16+
(e: "clipboard", value: string): void;
17+
(e: "confirm"): void;
1818
}>();
1919
2020
const { t } = useI18n();
2121
2222
const updateOpen = () => {
23-
emits('update:open', false);
23+
emits("update:open", false);
2424
};
2525
2626
const handleConfirm = () => {
27-
emits('confirm');
27+
emits("confirm");
2828
};
2929
3030
const handleContextMenu = async (e: Event) => {
@@ -33,12 +33,11 @@ const handleContextMenu = async (e: Event) => {
3333
3434
try {
3535
const clipboardText = await useTauriClipboardManagerReadText();
36-
console.log(clipboardText);
3736
3837
if (clipboardText) {
39-
emits('clipboard', clipboardText);
38+
emits("clipboard", clipboardText);
4039
} else {
41-
emits('clipboard', '');
40+
emits("clipboard", "");
4241
}
4342
} catch (error) {
4443
console.error(error);
@@ -56,26 +55,14 @@ const handleContextMenu = async (e: Event) => {
5655
@update:open="updateOpen"
5756
>
5857
<template #body>
59-
<div
60-
@contextmenu.stop.prevent="handleContextMenu"
61-
@keydown.enter="handleConfirm"
62-
>
58+
<div @contextmenu.stop.prevent="handleContextMenu" @keydown.enter="handleConfirm">
6359
<slot />
6460
</div>
6561
</template>
6662

6763
<template #footer="{ close }">
68-
<UButton
69-
:label="t('Common.Cancel')"
70-
color="neutral"
71-
variant="outline"
72-
@click="close"
73-
/>
74-
<UButton
75-
:label="t('Common.Confirm')"
76-
color="neutral"
77-
@click="handleConfirm"
78-
/>
64+
<UButton :label="t('Common.Cancel')" color="neutral" variant="outline" @click="close" />
65+
<UButton :label="t('Common.Confirm')" color="neutral" @click="handleConfirm" />
7966
</template>
8067
</UModal>
8168
</template>

ui/components/SideBar/profile.vue

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { UnlistenFn } from "@tauri-apps/api/event";
44
import type { WebviewWindow } from "@tauri-apps/api/webviewWindow";
55
import type { PermissionOrgs, PermOrgItem, UserData, UserIntiInfo, RoleType } from "~/types/index";
66
7+
import { resolveLanguageFromCookies } from "~/utils";
78
import { LogicalPosition } from "@tauri-apps/api/dpi";
89
import { useUserInfoStore } from "~/store/modules/userInfo";
910
@@ -62,14 +63,11 @@ const profileMenuItems = computed<DropdownMenuItem[][]>(() => [
6263
]
6364
]);
6465
65-
const userDescription = computed(() => {
66-
return currentUser.value?.system_roles.map((role: RoleType) => role.display_name).join(";");
67-
});
68-
6966
watch(
7067
() => currentLanguage.value,
7168
(lang) => {
7269
const target = lang === "zh" ? "zh" : "en";
70+
7371
setLocale(target as any);
7472
},
7573
{ immediate: true }
@@ -104,27 +102,6 @@ function initSelectOrganization(permissionOrgData: PermissionOrgs) {
104102
return uniqueOrgs;
105103
}
106104
107-
/**
108-
* @description 处理 cooklies 中的 django_language
109-
* @param cookies
110-
*/
111-
function resolveLanguageFromCookies(cookies: string | undefined | null): "zh" | "en" {
112-
if (!cookies) return "en";
113-
114-
const langEntry = cookies
115-
.split(";")
116-
.map((chunk) => chunk.trim())
117-
.find((chunk) => chunk.toLowerCase().startsWith("django_language="));
118-
119-
if (!langEntry) return "en";
120-
121-
const value = langEntry.split("=")[1]?.trim().toLowerCase();
122-
123-
if (!value) return "en";
124-
125-
return value === "zh-hans" || value.startsWith("zh") ? "zh" : "en";
126-
}
127-
128105
/**
129106
* @description 打开登录页面
130107
*/
@@ -351,7 +328,6 @@ onMounted(async () => {
351328
352329
if (status === "success" && profileData) {
353330
const language = resolveLanguageFromCookies(cookies);
354-
await setLocale(language as any);
355331
356332
toast.add({
357333
title: t("Login.LoginSuccess"),
@@ -436,7 +412,6 @@ onBeforeUnmount(() => {
436412
:avatar="{
437413
src: '/user_avatar.png'
438414
}"
439-
:description="userDescription"
440415
:ui="props.collapse ? { root: 'justify-center gap-0' } : undefined"
441416
>
442417
<template #name>

ui/composables/useAssetAction.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,6 @@ export const useAssetAction = () => {
363363
}
364364

365365
const payload = event.payload as eventPayload;
366-
367-
console.log("listenFavoriteSuccessEvent", payload);
368366
});
369367

370368
unlistenFavoriteFailed = await useTauriEventListen("set-favorite-failure", (event) => {
@@ -373,7 +371,6 @@ export const useAssetAction = () => {
373371
}
374372

375373
const payload = event.payload as eventPayload;
376-
console.log("listenFavoriteFailedEvent", payload);
377374
});
378375

379376
unlistenGetAssetDetailSuccess = await useTauriEventListen(
@@ -389,7 +386,6 @@ export const useAssetAction = () => {
389386

390387
if (payload.status === "success") {
391388
const assetDetail = JSON.parse(payload.data) as any;
392-
console.log("assetDetail", assetDetail);
393389
const permedAccounts = assetDetail.permed_accounts ?? [];
394390
const permedProtocols = assetDetail.permed_protocols ?? [];
395391

ui/composables/usePlatform.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,26 @@
33
* 提供跨平台的平台检测功能
44
*/
55
export const usePlatform = () => {
6-
const platform = ref<string>('');
6+
const platform = ref<string>("");
77
const isLoading = ref(true);
88

99
// 计算属性:判断是否为 macOS
10-
const isMacOS = computed(() => platform.value === 'darwin' || platform.value === 'macos');
11-
10+
const isMacOS = computed(() => platform.value === "darwin" || platform.value === "macos");
11+
1212
// 计算属性:判断是否为 Windows
13-
const isWindows = computed(() => platform.value === 'win32' || platform.value === 'windows');
14-
13+
const isWindows = computed(() => platform.value === "win32" || platform.value === "windows");
14+
1515
// 计算属性:判断是否为 Linux
16-
const isLinux = computed(() => platform.value === 'linux');
16+
const isLinux = computed(() => platform.value === "linux");
1717

18-
// 获取平台信息
18+
// 获取平台信息,如果无法获取平台信息,默认为 windows
1919
const getPlatform = async () => {
2020
try {
2121
isLoading.value = true;
2222
const currentPlatform = await useTauriOsPlatform();
2323
platform.value = currentPlatform;
24-
console.log('Platform detected:', platform.value);
2524
} catch (error) {
26-
console.error('Failed to get platform info:', error);
27-
// 如果无法获取平台信息,默认为 windows
28-
platform.value = 'win32';
25+
platform.value = "win32";
2926
} finally {
3027
isLoading.value = false;
3128
}
@@ -37,12 +34,11 @@ export const usePlatform = () => {
3734
});
3835

3936
return {
40-
platform: readonly(platform),
41-
isLoading: readonly(isLoading),
4237
isMacOS,
43-
isWindows,
4438
isLinux,
39+
isWindows,
4540
getPlatform,
41+
platform: readonly(platform),
42+
isLoading: readonly(isLoading)
4643
};
4744
};
48-

ui/composables/useSettingManager.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import type { SortType, ThemeType, LayoutsType, AppConfigType } from "~/types";
1+
import type { SortType, ThemeType, LayoutsType, AppConfigType, LangType } from "~/types";
22
import { useSettingStorage, type UserSettingPersistedState } from "~/composables/useSettingStorage";
33

4-
let managerSingleton: ReturnType<typeof createSettingManager> | null = null;
5-
6-
const createSettingManager = () => {
4+
export const useSettingManager = () => {
75
const storage = useSettingStorage();
86

97
const isHydrated = ref(false);
@@ -61,16 +59,16 @@ const createSettingManager = () => {
6159
});
6260
};
6361

64-
const setLang = (lang: string) => {
62+
const setLang = (lang: LangType) => {
6563
state.language = lang;
6664
persist({ language: lang });
6765
};
6866

69-
const setLangGlobal = (lang: string) => {
67+
const setLangGlobal = (lang: LangType) => {
7068
state.language = lang;
7169

7270
// 覆盖所有站点:以存储中的 siteLanguages 为准,避免仅依据当前窗口内存键集合
73-
void ensureHydration()
71+
ensureHydration()
7472
.then(async () => {
7573
try {
7674
const saved = await storage.load();
@@ -81,14 +79,28 @@ const createSettingManager = () => {
8179
...Object.keys(state.siteLanguages || {})
8280
]);
8381

84-
const updated: Record<string, string> = {};
82+
// 如果持久化与内存的联合集合中全部已是目标语言,则直接跳过,避免重复应用与重复日志
83+
const allAlreadyTarget = [...unionSites].every((site) => {
84+
const current =
85+
(state.siteLanguages && state.siteLanguages[site]) ??
86+
(saved.siteLanguages && saved.siteLanguages[site]) ??
87+
saved.language ??
88+
state.language;
89+
return current === lang;
90+
});
91+
92+
if ((saved.language ?? state.language) === lang && allAlreadyTarget) {
93+
return;
94+
}
95+
96+
const updated: Record<string, LangType> = {};
8597
unionSites.forEach((site) => (updated[site] = lang));
8698

8799
state.siteLanguages = updated;
88100
persist({ language: lang, siteLanguages: updated });
89101
} catch (err) {
90102
console.error("setLangGlobal load failed, fallback to memory keys", err);
91-
const fallback: Record<string, string> = { ...state.siteLanguages };
103+
const fallback: Record<string, LangType> = { ...state.siteLanguages };
92104
Object.keys(fallback).forEach((site) => (fallback[site] = lang));
93105
state.siteLanguages = fallback;
94106
persist({ language: lang, siteLanguages: fallback });
@@ -148,25 +160,27 @@ const createSettingManager = () => {
148160
persist({ appConfig: state.appConfig });
149161
};
150162

151-
void ensureHydration();
163+
ensureHydration();
152164

153165
const hasSiteLanguage = (site: string) => site in state.siteLanguages;
154166

155-
const setSiteLanguage = (site: string, lang: string) => {
167+
const setSiteLanguage = (site: string, lang: LangType) => {
156168
if (state.siteLanguages[site] === lang) return;
169+
157170
state.siteLanguages = { ...state.siteLanguages, [site]: lang };
158171
persist({ siteLanguages: state.siteLanguages });
159172
};
160173

161174
const removeSiteLanguage = (site: string) => {
162175
if (!(site in state.siteLanguages)) return;
176+
163177
const updated = { ...state.siteLanguages };
164178
delete updated[site];
165179
state.siteLanguages = updated;
166180
persist({ siteLanguages: updated });
167181
};
168182

169-
const getSiteLanguage = (site: string) => state.siteLanguages[site] || state.language;
183+
const getSiteLanguage = (site: string): LangType => state.siteLanguages[site] || state.language;
170184
const getDefaultLanguage = () => state.language;
171185

172186
return {
@@ -193,11 +207,3 @@ const createSettingManager = () => {
193207
setPrimaryColorLight
194208
};
195209
};
196-
197-
export const useSettingManager = () => {
198-
if (!managerSingleton) {
199-
managerSingleton = createSettingManager();
200-
}
201-
202-
return managerSingleton;
203-
};

ui/composables/useSettingStorage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { UnlistenFn } from "@tauri-apps/api/event";
22
import { Store } from "@tauri-apps/plugin-store";
33

4-
import type { AppConfigType, SortType } from "~/types";
4+
import type { AppConfigType, SortType, LangType } from "~/types";
55

66
export type ThemeType = "light" | "dark" | "withSystem" | "";
77
export type LayoutsType = "grid" | "table";
88

99
export type UserSettingPersistedState = {
10-
language: string;
11-
siteLanguages: Record<string, string>;
10+
language: LangType;
11+
siteLanguages: Record<string, LangType>;
1212
collapse: boolean;
1313
sort: SortType;
1414
theme: ThemeType;

0 commit comments

Comments
 (0)