From 9b8cc540716db5d0e009473278b7fdd25c58a94a Mon Sep 17 00:00:00 2001 From: an-lee Date: Sat, 14 Sep 2024 06:29:40 +0800 Subject: [PATCH] fix mdict import --- enjoy/src/i18n/en.json | 1 + enjoy/src/i18n/zh-CN.json | 1 + enjoy/src/main/mdict.ts | 3 ++- .../preferences/dict-settings/dict-import-button.tsx | 4 ++-- enjoy/src/renderer/context/dict-provider.tsx | 6 +++++- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/enjoy/src/i18n/en.json b/enjoy/src/i18n/en.json index 3d99bd71..e6f3b43c 100644 --- a/enjoy/src/i18n/en.json +++ b/enjoy/src/i18n/en.json @@ -762,6 +762,7 @@ "dictSettings": "Dictionaries Settings", "dictSettingsShort": "Dictionaries", "importDict": "Import Dictionary", + "failedToImportDict": "Failed to import dictionary: {{error}}", "dictRemoved": "Dictionary removed successfully", "interrupted": "Interrupted", "paused": "Paused", diff --git a/enjoy/src/i18n/zh-CN.json b/enjoy/src/i18n/zh-CN.json index a2ae5530..e8f68459 100644 --- a/enjoy/src/i18n/zh-CN.json +++ b/enjoy/src/i18n/zh-CN.json @@ -762,6 +762,7 @@ "dictSettings": "词典设置", "dictSettingsShort": "词典设置", "importDict": "导入词典", + "failedToImportDict": "导入词典失败: {{error}}", "dictRemoved": "成功删除词典", "interrupted": "已中断", "paused": "已暂停", diff --git a/enjoy/src/main/mdict.ts b/enjoy/src/main/mdict.ts index 8eb017c0..ba6064be 100644 --- a/enjoy/src/main/mdict.ts +++ b/enjoy/src/main/mdict.ts @@ -7,7 +7,7 @@ import { Mdict as MdictReader } from "@divisey/js-mdict"; import { hashFile } from "@/main/utils"; import settings from "./settings"; -const logger = log.scope("dict"); +const logger = log.scope("mdict"); export class MDictHandler { private cache = new LRUCache({ max: 20 }); @@ -26,6 +26,7 @@ export class MDictHandler { } async import(pathes: string[]) { + logger.info("Importing mdict: ", pathes); const mdxs = pathes.filter((_path) => _path.match(/\.mdx$/)); const mdds = pathes.filter((_path) => _path.match(/\.mdd$/)); diff --git a/enjoy/src/renderer/components/preferences/dict-settings/dict-import-button.tsx b/enjoy/src/renderer/components/preferences/dict-settings/dict-import-button.tsx index 4b8567ed..455257b0 100644 --- a/enjoy/src/renderer/components/preferences/dict-settings/dict-import-button.tsx +++ b/enjoy/src/renderer/components/preferences/dict-settings/dict-import-button.tsx @@ -39,7 +39,7 @@ export const DictImportButton = () => { await EnjoyApp.dict.import(pathes[0]); setOpen(false); } catch (err) { - toast.error(err.message); + toast.error(t("failedToImportDict", { error: err.message })); } setLoading(false); @@ -60,7 +60,7 @@ export const DictImportButton = () => { await importMDict(mdict); setOpen(false); } catch (err) { - toast.error(err.message); + toast.error(t("failedToImportDict", { error: err.message })); } setLoading(false); diff --git a/enjoy/src/renderer/context/dict-provider.tsx b/enjoy/src/renderer/context/dict-provider.tsx index 8fc0f9fb..a635119e 100644 --- a/enjoy/src/renderer/context/dict-provider.tsx +++ b/enjoy/src/renderer/context/dict-provider.tsx @@ -112,7 +112,11 @@ export const DictProvider = ({ children }: { children: React.ReactNode }) => { const fetchSettings = async () => { return EnjoyApp.userSettings.get(UserSettingKeyEnum.DICTS).then((res) => { - res && setSettings(res); + if (res) { + setSettings({ ...initialState.settings, ...res }); + } else { + setSettings(initialState.settings); + } }); };