mirror of
https://github.com/Moe-Sakura/frontend.git
synced 2026-03-15 04:53:18 +08:00
Refactor translation logic in VndbPanel.vue for improved efficiency and error handling
- Updated the translation process to capture the current data state before executing tasks, allowing for parallel execution of translation functions. - Implemented Promise.allSettled to ensure all translation tasks complete, enhancing reliability during the translation process. - Adjusted conditions for translating descriptions, tags, and quotes to streamline the logic and improve performance.
This commit is contained in:
@@ -676,7 +676,7 @@ watch(() => searchStore.vndbInfo, async (newInfo) => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
}, { immediate: true })
|
||||||
|
|
||||||
// 加载角色和名言
|
// 加载角色和名言
|
||||||
async function loadCharactersAndQuotes(vnId: string) {
|
async function loadCharactersAndQuotes(vnId: string) {
|
||||||
@@ -839,26 +839,32 @@ async function translateAllInternal(silent = false) {
|
|||||||
|
|
||||||
const vnIdAtStart = currentVnId.value
|
const vnIdAtStart = currentVnId.value
|
||||||
|
|
||||||
// 串行执行翻译任务,避免 API 限流
|
// 在调用前捕获当前数据状态,避免翻译期间数据被重置
|
||||||
|
const hasDescription = !!searchStore.vndbInfo?.description && !translatedDescription.value
|
||||||
|
const hasTags = !!searchStore.vndbInfo?.tags && searchStore.vndbInfo.tags.length > 0 && translatedTags.value.size === 0
|
||||||
|
const hasQuotes = quotes.value.length > 0 && translatedQuotes.value.size === 0
|
||||||
|
|
||||||
|
// 并行执行所有翻译任务
|
||||||
|
const tasks: Promise<void>[] = []
|
||||||
|
|
||||||
// 翻译简介
|
// 翻译简介
|
||||||
if (searchStore.vndbInfo?.description && !translatedDescription.value) {
|
if (hasDescription) {
|
||||||
await translateDescriptionInternal(vnIdAtStart, silent)
|
tasks.push(translateDescriptionInternal(vnIdAtStart, silent))
|
||||||
// 检查游戏是否已切换
|
|
||||||
if (currentVnId.value !== vnIdAtStart) { return }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 翻译标签
|
// 翻译标签
|
||||||
if (searchStore.vndbInfo?.tags && searchStore.vndbInfo.tags.length > 0 && translatedTags.value.size === 0) {
|
if (hasTags) {
|
||||||
await translateTagsInternal(vnIdAtStart, silent)
|
tasks.push(translateTagsInternal(vnIdAtStart, silent))
|
||||||
// 检查游戏是否已切换
|
|
||||||
if (currentVnId.value !== vnIdAtStart) { return }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 翻译名言
|
// 翻译名言
|
||||||
if (quotes.value.length > 0 && translatedQuotes.value.size === 0) {
|
if (hasQuotes) {
|
||||||
await translateQuotesInternal(vnIdAtStart, silent)
|
tasks.push(translateQuotesInternal(vnIdAtStart, silent))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 使用 allSettled 确保所有任务完成,即使某些失败
|
||||||
|
await Promise.allSettled(tasks)
|
||||||
|
|
||||||
// 如果有任何翻译成功且是当前游戏,播放成功音效
|
// 如果有任何翻译成功且是当前游戏,播放成功音效
|
||||||
if (!silent && currentVnId.value === vnIdAtStart) {
|
if (!silent && currentVnId.value === vnIdAtStart) {
|
||||||
if (translatedDescription.value || translatedTags.value.size > 0 || translatedQuotes.value.size > 0) {
|
if (translatedDescription.value || translatedTags.value.size > 0 || translatedQuotes.value.size > 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user