Merge pull request #212 from bluelovers/pr/translate-by-csv-001

feat: 手動翻譯時包含 沒有完整被 csv 翻譯的提示詞
This commit is contained in:
Physton
2023-09-03 19:04:33 +08:00
committed by GitHub
2 changed files with 40 additions and 38 deletions

View File

@@ -1324,7 +1324,7 @@ export default {
let translateByCSV = (tags) => {
// 开启了使用tagcomplete翻译
console.log('translateByCSV', tags.map(tag => tag.value).join(', '))
console.log('translateByCSV', tags.map(tag => tag.value), { useNetwork })
let promises = []
tags.forEach(tag => {
// 是否被括号包裹
@@ -1335,7 +1335,7 @@ export default {
}
if (tag.toLocal) {
// 翻译到本地语言
promises.push(this.translateToLocalByCSV(tag.value))
promises.push(this.translateToLocalByCSV(tag.value, void 0, void 0, useNetwork))
} else {
// 翻译到英文
promises.push(this.translateToEnByCSV(tag.value))
@@ -1360,7 +1360,7 @@ export default {
setTag(tag, result)
}
})
console.log('No translated keywords: ', needs.map(tag => tag.value).join(', '))
console.log('No translated keywords: ', needs.map(tag => tag.value))
if (useNetwork) {
translate(needs)
} else {
@@ -1377,7 +1377,7 @@ export default {
let translateByGroupTags = (tags) => {
// 开启了使用关键词组翻译
console.log('translateByGroupTags', tags.map(tag => tag.value).join(', '))
console.log('translateByGroupTags', tags.map(tag => tag.value), { useNetwork })
let promises = []
tags.forEach(tag => {
// 是否被括号包裹
@@ -1388,13 +1388,14 @@ export default {
}
if (tag.toLocal) {
// 翻译到本地语言
promises.push(this.translateToLocalByGroupTags(tag.value))
promises.push(this.translateToLocalByGroupTags(tag.value, useNetwork))
} else {
// 翻译到英文
promises.push(this.translateToEnByGroupTags(tag.value))
promises.push(this.translateToEnByGroupTags(tag.value, useNetwork))
}
})
Promise.all(promises).then(results => {
Promise.allSettled(promises).then(results => {
let errors = []
let needs = []
results.forEach((result, index) => {
let tag = tags[index]
@@ -1403,21 +1404,31 @@ export default {
tag.value = tag.splits.left + tag.value + tag.splits.right
}
if (result === '') {
if (result.status !== 'fulfilled') {
errors.push(result.reason)
}
if (!result.value?.length || result.status !== 'fulfilled') {
needs.push(tag)
} else {
if (tag.splits) {
result = tag.splits.left + result + tag.splits.right
result = tag.splits.left + result.value + tag.splits.right
}
setLoading(tag, false)
setTag(tag, result)
setTag(tag, result.value)
}
})
console.log('No translated keywords: ', needs.map(tag => tag.value).join(', '))
console.log('No translated keywords: ', needs.map(tag => tag.value))
if (this.tagCompleteFile) {
// 开启了使用tagcomplete翻译
translateByCSV(needs)
} else {
if (errors.length) {
setLoadings(tags, false)
this.$toastr.error(errors[0])
reject(errors[0])
return
}
if (useNetwork) {
translate(needs)
} else {

View File

@@ -161,24 +161,31 @@ export default {
})
})
},
async translateToLocalByCSV(text, tagCompleteFile = null, reload = false) {
let res = await this.getCSV(tagCompleteFile, reload)
_translateToLocalBy(text, toLocal, useNetwork = false) {
text = text.trim().toLowerCase()
if (res.toLocal.has(text)) {
return res.toLocal.get(text)
let _localToString = value => (value.join?.(' / ') ?? value)
if (toLocal.has(text)) {
return _localToString(toLocal.get(text))
} else {
// 使用 , 分隔
const texts = text.split(',').map(t => t.trim())
let result = []
let needs = []
texts.forEach(t => {
if (res.toLocal.has(t)) {
result.push(res.toLocal.get(t))
if (toLocal.has(t)) {
result.push(_localToString(toLocal.get(t)))
} else if (useNetwork && t.length) {
needs.push(t)
}
})
if (result.length > 0) return result.join(', ')
if (result.length > 0 && !needs.length) return result.join(', ')
}
return ''
},
async translateToLocalByCSV(text, tagCompleteFile = null, reload = false, useNetwork = false) {
let res = await this.getCSV(tagCompleteFile, reload)
return this._translateToLocalBy(text, res.toLocal, useNetwork)
},
async translateToEnByCSV(text, tagCompleteFile = null, reload = false) {
let res = await this.getCSV(tagCompleteFile, reload)
text = text.trim().toLowerCase()
@@ -187,25 +194,9 @@ export default {
}
return ''
},
async translateToLocalByGroupTags(text) {
console.log(text)
text = text.trim().toLowerCase()
if (this.groupTagsTranslateCache.toLocal.has(text)) {
let value = this.groupTagsTranslateCache.toLocal.get(text)
return value.join(' / ')
} else {
// 使用 , 分隔
const texts = text.split(',').map(t => t.trim())
let result = []
texts.forEach(t => {
if (this.groupTagsTranslateCache.toLocal.has(t)) {
let value = this.groupTagsTranslateCache.toLocal.get(t)
result.push(value.join(' / '))
}
})
if (result.length > 0) return result.join(', ')
}
return ''
async translateToLocalByGroupTags(text, useNetwork = false) {
console.log('translateToLocalByGroupTags', text)
return this._translateToLocalBy(text, this.groupTagsTranslateCache.toLocal, useNetwork)
},
async translateToEnByGroupTags(text) {
text = text.trim().toLowerCase()
@@ -215,4 +206,4 @@ export default {
return ''
},
}
}
}