feat: enhance translation functionality in MessageMenubar

- Updated translateText function to accept an optional callback for handling translated text directly within the function.
- Refactored MessageMenubar to utilize the new callback mechanism, improving the flow of translated content handling.
- Enhanced error handling during translation to ensure better user feedback in case of failures.
This commit is contained in:
kangfenmao 2025-01-22 14:37:15 +08:00
parent 8aec8a60b3
commit 900b11bdf7
2 changed files with 5 additions and 4 deletions

View File

@ -126,8 +126,9 @@ const MessageMenubar: FC<Props> = (props) => {
setIsTranslating(true) setIsTranslating(true)
try { try {
const translatedText = await translateText(message.content, language) await translateText(message.content, language, (text) =>
onEditMessage?.({ ...message, translatedContent: translatedText }) onEditMessage?.({ ...message, translatedContent: text })
)
} catch (error) { } catch (error) {
console.error('Translation failed:', error) console.error('Translation failed:', error)
window.message.error({ window.message.error({

View File

@ -6,7 +6,7 @@ import { getDefaultTopic } from './AssistantService'
import { getDefaultTranslateAssistant } from './AssistantService' import { getDefaultTranslateAssistant } from './AssistantService'
import { getUserMessage } from './MessagesService' import { getUserMessage } from './MessagesService'
export const translateText = async (text: string, targetLanguage: string) => { export const translateText = async (text: string, targetLanguage: string, onResponse?: (text: string) => void) => {
const translateModel = store.getState().llm.translateModel const translateModel = store.getState().llm.translateModel
if (!translateModel) { if (!translateModel) {
@ -25,7 +25,7 @@ export const translateText = async (text: string, targetLanguage: string) => {
content: text content: text
}) })
const translatedText = await fetchTranslate({ message, assistant }) const translatedText = await fetchTranslate({ message, assistant, onResponse })
const trimmedText = translatedText.trim() const trimmedText = translatedText.trim()