翻译功能增加手动选择源语言的选项 (#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 90a84bb55a
commit 49f1b62848
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 36 additions and 15 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -215,6 +215,7 @@ const TranslatePage: FC = () => {
const [bidirectionalPair, setBidirectionalPair] = useState<[string, string]>(['english', 'chinese']) const [bidirectionalPair, setBidirectionalPair] = useState<[string, string]>(['english', 'chinese'])
const [settingsVisible, setSettingsVisible] = useState(false) const [settingsVisible, setSettingsVisible] = useState(false)
const [detectedLanguage, setDetectedLanguage] = useState<string | null>(null) const [detectedLanguage, setDetectedLanguage] = useState<string | null>(null)
const [sourceLanguage, setSourceLanguage] = useState<string>('auto') // 添加用户选择的源语言状态
const contentContainerRef = useRef<HTMLDivElement>(null) const contentContainerRef = useRef<HTMLDivElement>(null)
const textAreaRef = useRef<TextAreaRef>(null) const textAreaRef = useRef<TextAreaRef>(null)
const outputTextRef = useRef<HTMLDivElement>(null) const outputTextRef = useRef<HTMLDivElement>(null)
@ -288,10 +289,17 @@ const TranslatePage: FC = () => {
setLoading(true) setLoading(true)
try { try {
const sourceLanguage = await detectLanguage(text) // 确定源语言:如果用户选择了特定语言,使用用户选择的;如果选择'auto',则自动检测
console.log('检测到的语言:', sourceLanguage) let actualSourceLanguage: string
setDetectedLanguage(sourceLanguage) if (sourceLanguage === 'auto') {
const result = determineTargetLanguage(sourceLanguage, targetLanguage, isBidirectional, bidirectionalPair) actualSourceLanguage = await detectLanguage(text)
console.log('检测到的语言:', actualSourceLanguage)
setDetectedLanguage(actualSourceLanguage) // 更新检测到的语言
} else {
actualSourceLanguage = sourceLanguage
}
const result = determineTargetLanguage(actualSourceLanguage, targetLanguage, isBidirectional, bidirectionalPair)
if (!result.success) { if (!result.success) {
let errorMessage = '' let errorMessage = ''
if (result.errorType === 'same_language') { 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) setLoading(false)
} catch (error) { } catch (error) {
console.error('Translation error:', error) console.error('Translation error:', error)
@ -498,15 +506,28 @@ const TranslatePage: FC = () => {
<Flex align="center" gap={20}> <Flex align="center" gap={20}>
<Select <Select
showSearch showSearch
value="auto" value={sourceLanguage}
style={{ width: 180 }} style={{ width: 180 }}
optionFilterProp="label" optionFilterProp="label"
disabled onChange={(value) => setSourceLanguage(value)}
options={[ 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 <Button