翻译功能增加手动选择源语言的选项 (#6916)

* feat(TranslatePage): add user-selectable source language with auto-detection

* fix: update detected language label for consistency across translations

---------

Co-authored-by: Pleasurecruise <3196812536@qq.com>
This commit is contained in:
FischLu 2025-06-10 10:25:22 +02:00 committed by GitHub
parent d19dd67213
commit cf230c4f9b
6 changed files with 36 additions and 15 deletions

View File

@ -1820,7 +1820,7 @@
"close": "Close",
"closed": "Translation closed",
"copied": "Translation content copied",
"detected.language": "Detected Language",
"detected.language": "Auto Detect",
"empty": "Translation content is empty",
"not.found": "Translation content not found",
"confirm": {

View File

@ -1853,7 +1853,7 @@
"menu": {
"description": "對當前輸入框內容進行翻譯"
},
"detected.language": "検出された言語"
"detected.language": "自動検出"
},
"tray": {
"quit": "終了",

View File

@ -1853,7 +1853,7 @@
"menu": {
"description": "Перевести содержимое текущего ввода"
},
"detected.language": "Обнаруженный язык"
"detected.language": "Автоматическое обнаружение"
},
"tray": {
"quit": "Выйти",

View File

@ -1853,7 +1853,7 @@
},
"title": "翻译",
"tooltip.newline": "换行",
"detected.language": "检测到的语言"
"detected.language": "自动检测"
},
"tray": {
"quit": "退出",

View File

@ -1853,7 +1853,7 @@
"menu": {
"description": "對當前輸入框內容進行翻譯"
},
"detected.language": "檢測到的語言"
"detected.language": "自動檢測"
},
"tray": {
"quit": "結束",

View File

@ -215,6 +215,7 @@ const TranslatePage: FC = () => {
const [bidirectionalPair, setBidirectionalPair] = useState<[string, string]>(['english', 'chinese'])
const [settingsVisible, setSettingsVisible] = useState(false)
const [detectedLanguage, setDetectedLanguage] = useState<string | null>(null)
const [sourceLanguage, setSourceLanguage] = useState<string>('auto') // 添加用户选择的源语言状态
const contentContainerRef = useRef<HTMLDivElement>(null)
const textAreaRef = useRef<TextAreaRef>(null)
const outputTextRef = useRef<HTMLDivElement>(null)
@ -288,10 +289,17 @@ const TranslatePage: FC = () => {
setLoading(true)
try {
const sourceLanguage = await detectLanguage(text)
console.log('检测到的语言:', sourceLanguage)
setDetectedLanguage(sourceLanguage)
const result = determineTargetLanguage(sourceLanguage, targetLanguage, isBidirectional, bidirectionalPair)
// 确定源语言:如果用户选择了特定语言,使用用户选择的;如果选择'auto',则自动检测
let actualSourceLanguage: string
if (sourceLanguage === 'auto') {
actualSourceLanguage = await detectLanguage(text)
console.log('检测到的语言:', actualSourceLanguage)
setDetectedLanguage(actualSourceLanguage) // 更新检测到的语言
} else {
actualSourceLanguage = sourceLanguage
}
const result = determineTargetLanguage(actualSourceLanguage, targetLanguage, isBidirectional, bidirectionalPair)
if (!result.success) {
let errorMessage = ''
if (result.errorType === 'same_language') {
@ -324,7 +332,7 @@ const TranslatePage: FC = () => {
}
})
await saveTranslateHistory(text, translatedText, sourceLanguage, actualTargetLanguage)
await saveTranslateHistory(text, translatedText, actualSourceLanguage, actualTargetLanguage)
setLoading(false)
} catch (error) {
console.error('Translation error:', error)
@ -498,15 +506,28 @@ const TranslatePage: FC = () => {
<Flex align="center" gap={20}>
<Select
showSearch
value="auto"
value={sourceLanguage}
style={{ width: 180 }}
optionFilterProp="label"
disabled
onChange={(value) => setSourceLanguage(value)}
options={[
{
label: detectedLanguage ? t(`languages.${detectedLanguage}`) : t('translate.detected.language'),
value: 'auto'
}
value: 'auto',
label: detectedLanguage
? `${t('translate.detected.language')}(${t(`languages.${detectedLanguage.toLowerCase()}`)})`
: t('translate.detected.language')
},
...translateLanguageOptions().map((lang) => ({
value: lang.value,
label: (
<Space.Compact direction="horizontal" block>
<span role="img" aria-label={lang.emoji} style={{ marginRight: 8 }}>
{lang.emoji}
</span>
<Space.Compact block>{lang.label}</Space.Compact>
</Space.Compact>
)
}))
]}
/>
<Button