From 8522a688d5f433a0a9340992a8b931a8886cb356 Mon Sep 17 00:00:00 2001 From: Pylogmon Date: Thu, 31 Aug 2023 15:26:00 +0800 Subject: [PATCH] refactor: out-of-the-box --- .../components/LanguageArea/index.jsx | 18 +++++++++++++----- .../Translate/components/SourceArea/index.jsx | 2 +- .../Translate/components/TargetArea/index.jsx | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/window/Translate/components/LanguageArea/index.jsx b/src/window/Translate/components/LanguageArea/index.jsx index a9134168ee..d04c0afbc3 100644 --- a/src/window/Translate/components/LanguageArea/index.jsx +++ b/src/window/Translate/components/LanguageArea/index.jsx @@ -18,10 +18,18 @@ export default function LanguageArea() { const { t } = useTranslation(); useEffect(() => { store.get('translate_source_language').then((v) => { - setSourceLanguage(v); + if (v) { + setSourceLanguage(v); + } else { + setSourceLanguage('auto'); + } }); store.get('translate_target_language').then((v) => { - setTargetLanguage(v); + if (v) { + setTargetLanguage(v); + } else { + setTargetLanguage(zh_cn); + } }); }, []); @@ -62,15 +70,15 @@ export default function LanguageArea() { setTargetLanguage(oldSourceLanguage); } else { if (detectLanguage !== '') { - const defaultLanguage = await store.get('translate_target_language'); + const defaultLanguage = (await store.get('translate_target_language')) ?? 'zh_cn'; if (targetLanguage === defaultLanguage) { setTargetLanguage(detectLanguage); } else { setTargetLanguage(defaultLanguage); } } else { - const secondLanguage = await store.get('translate_second_language'); - const defaultLanguage = await store.get('translate_target_language'); + const secondLanguage = (await store.get('translate_second_language')) ?? 'en'; + const defaultLanguage = (await store.get('translate_target_language')) ?? 'zh_cn'; if (targetLanguage === secondLanguage) { setTargetLanguage(defaultLanguage); } else { diff --git a/src/window/Translate/components/SourceArea/index.jsx b/src/window/Translate/components/SourceArea/index.jsx index 831f1bfd03..4b3c2b06c8 100644 --- a/src/window/Translate/components/SourceArea/index.jsx +++ b/src/window/Translate/components/SourceArea/index.jsx @@ -40,7 +40,7 @@ export default function SourceArea() { // image translate } else { let newText = text; - const deleteNewline = await store.get('translate_delete_newline'); + const deleteNewline = (await store.get('translate_delete_newline')) ?? false; if (deleteNewline) { newText = text.replace(/\s+/g, ' '); } else { diff --git a/src/window/Translate/components/TargetArea/index.jsx b/src/window/Translate/components/TargetArea/index.jsx index fc3e82823b..c78ae7f09a 100644 --- a/src/window/Translate/components/TargetArea/index.jsx +++ b/src/window/Translate/components/TargetArea/index.jsx @@ -41,7 +41,7 @@ export default function TargetArea(props) { if (sourceLanguage in LanguageEnum && targetLanguage in LanguageEnum) { let newTargetLanguage = targetLanguage; if (sourceLanguage === 'auto' && targetLanguage === detectLanguage) { - newTargetLanguage = await store.get('translate_second_language'); + newTargetLanguage = (await store.get('translate_second_language')) ?? 'en'; } setIsLoading(true); buildinServices[name]