2024-08-03 12:46:12 +08:00
import { DEFINE _STRING } from '../../define/define_string'
const tencentcloud = require ( 'tencentcloud-sdk-nodejs' )
import { define } from '../../define/define'
import { MD5 } from 'crypto-js'
import axios from 'axios'
import path from 'path'
import { Tools } from '../tools'
const alimt20181012 = require ( '@alicloud/alimt20181012' )
const OpenApi = require ( '@alicloud/openapi-client' )
const Util = require ( '@alicloud/tea-util' )
let fspromises = require ( 'fs' ) . promises
import { SoftwareService } from '../../define/db/service/SoftWare/softwareService'
import { isEmpty } from 'lodash'
2024-08-04 15:00:00 +08:00
import { ValidateJson } from '../../define/Tools/validate'
2024-08-09 12:28:28 +08:00
import { GetOpenAISuccessResponse } from '../../define/response/openAIResponse'
import { successMessage } from './generalTools'
2024-08-20 10:37:38 +08:00
import { RetryWithBackoff } from '../../define/Tools/common'
2024-08-03 12:46:12 +08:00
let { Signer } = require ( '@volcengine/openapi' )
2024-05-15 12:57:15 +08:00
export class Translate {
2024-08-03 12:46:12 +08:00
constructor ( global ) {
this . global = global
this . tools = new Tools ( )
}
/ * *
* 初始化翻译设置
* /
async InitTranslate ( ) {
if ( ! this . softwareService ) {
this . softwareService = await SoftwareService . getInstance ( )
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
// 获取翻译设置
2024-08-04 15:00:00 +08:00
let translateSetting = this . softwareService . GetSoftWarePropertyData ( 'translationSetting' )
if ( isEmpty ( translateSetting ) ) {
throw new Error ( '翻译设置为空,请先设置' )
}
let tryParse = ValidateJson ( translateSetting )
if ( ! tryParse ) {
throw new Error ( '翻译设置的格式错误,请重置后重新添加' )
}
let translateSettingData = JSON . parse ( translateSetting )
let selectModel = translateSettingData . selectModel
let translateIndex = translateSettingData . translates . findIndex (
( item ) => item . name == selectModel
)
if ( translateIndex < 0 ) {
throw new Error ( '没有找到对应的翻译API设置' )
}
let translateData = translateSettingData . translates [ translateIndex ]
for ( const key in translateData ) {
if ( ! translateData [ key ] ) {
throw new Error ( ` 翻译设置中的 ${ key } 不能为空 ` )
2024-08-03 12:46:12 +08:00
}
}
2024-08-04 15:00:00 +08:00
this . translationBusiness = translateData . translation _business
this . translationAppId = translateData . translation _app _id
this . translationSecret = translateData . translation _secret
2024-08-03 12:46:12 +08:00
}
/ * *
* 将当前的翻译任务添加到队列中
* @ param { * } value
* 0 第 0 个参数是要翻译的数据 ( 数组 )
* 1 第 1 个参数是源语言
* 2 第 2 个参数是目标语言
* 3 第 3 个参数是否分割 ( 默认不分割false )
* 4 第 4 个参数是要不要全局弹窗提示
* @ returns
* /
async TranslateReturnNowTask ( value ) {
try {
await this . InitTranslate ( )
value = JSON . parse ( value )
let data = value [ 0 ]
let to = value [ 2 ]
let batch = DEFINE _STRING . QUEUE _BATCH . TRANSLATE _RETURN _NOW _TASK
for ( let i = 0 ; i < data . length ; i ++ ) {
const element = data [ i ]
// 添加任务到队列
this . global . requestQuene . enqueue (
async ( ) => {
try {
let res = await this . TranslateReturnNow ( [
element . gpt _prompt ,
value [ 1 ] ,
to ,
value [ 3 ] ,
value [ 4 ]
] )
if ( res . code != 1 ) {
throw new Error ( res . message )
}
let res _p = null
if ( ! value [ 3 ] ) {
if ( to == 'zh' ) {
res _p = res . data . map ( ( item ) => item . dst ) . join ( ',' )
} else {
res _p = res . data . map ( ( item ) => item . src ) . join ( ',' )
}
} else {
res _p = res . data
}
// 修改chinese_prompt
this . global . fileQueue . enqueue ( async ( ) => {
let json _path = path . join (
this . global . config . project _path ,
` tmp/input_crop/ ${ element . name } .json `
)
let prompt _json = JSON . parse ( await fspromises . readFile ( json _path , 'utf-8' ) )
if ( ! value [ 3 ] ) {
prompt _json . gpt _prompt = res _p
} else {
prompt _json . chinese _prompt = res _p
}
await fspromises . writeFile ( json _path , JSON . stringify ( prompt _json ) )
} )
this . global . newWindow [ 0 ] . win . webContents . send (
DEFINE _STRING . TRANSLATE _RETURN _REFRESH ,
{
code : 1 ,
to : to ,
rowId : element . id ,
data : res _p
}
)
} catch ( error ) {
throw error
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
} ,
` ${ batch } _ ${ element . name } ` ,
batch
)
}
// 监听总批次完成
this . global . requestQuene . setBatchCompletionCallback ( batch , ( failedTasks ) => {
if ( failedTasks . length > 0 ) {
let message = `
2024-05-15 12:57:15 +08:00
翻译任务都已完成 。
但是以下任务执行失败 :
`
2024-08-03 12:46:12 +08:00
failedTasks . forEach ( ( { taskId , error } ) => {
message += ` ${ taskId } -, \n 错误信息: ${ error } ` + '\n'
} )
this . global . newWindow [ 0 ] . win . webContents . send ( DEFINE _STRING . SHOW _MESSAGE _DIALOG , {
code : 0 ,
message : message
} )
} else {
if ( value [ 4 ] ) {
this . global . newWindow [ 0 ] . win . webContents . send ( DEFINE _STRING . SHOW _MESSAGE _DIALOG , {
code : 1 ,
message : '翻译任务完成'
2024-05-15 12:57:15 +08:00
} )
2024-08-03 12:46:12 +08:00
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
} )
return {
code : 1 ,
message : '翻译任务已加入队列任务中'
}
} catch ( error ) {
return {
code : 0 ,
message : '翻译任务出错,错误信息: ' + error . toString ( )
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
}
/ * *
*
* @ param { * } value 0 : 当前要翻译的字符串
* 1 : 源语言
* 2 : 目标语言
* 3 : 是否拆分 ( 以逗号拆分 )
* [ tags , 'zh' , 'en' , false ]
* /
async TranslateReturnNow ( value ) {
try {
await this . InitTranslate ( )
// 百度翻译
if ( this . translationBusiness . includes ( 'baidu' ) ) {
return await this . TranslateReturnNowBaidu ( value )
} else if ( this . translationBusiness . includes ( 'volcengine' ) ) {
// 火山引擎
return await this . TranslateReturnNowVolcengine ( value )
} else if ( this . translationBusiness . includes ( 'tencent' ) ) {
// 腾讯翻译
return await this . TranslateReturnNowTencent ( value )
} else if ( this . translationBusiness . includes ( 'aliyun' ) ) {
return await this . TranslateReturnNowAliyun ( value )
2024-08-09 12:28:28 +08:00
} else if ( this . translationBusiness . includes ( 'laitool' ) ) {
return await this . TranslateReturnNowGPT ( value )
} else {
throw new Error ( '未知的翻译API, 请检查设置' )
2024-08-03 12:46:12 +08:00
}
} catch ( error ) {
return {
code : 0 ,
message : error . toString ( )
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
}
/ * *
* 添加翻译任务到队列中
* @ param {
* translateData : 要翻译的数据
* from : 源语言
* to : 目标语言
* window . id : 显示的窗体的ID
* isShow : 是不是提示
* [ translateData , from , to , window . id , isShow ]
* } value
* /
async TranslatePrompt ( value ) {
try {
await this . InitTranslate ( )
value [ 0 ] = JSON . parse ( value [ 0 ] )
// baidu翻译
if ( this . translationBusiness . includes ( 'baidu' ) ) {
return await this . TranslatePromptBaidu ( value )
} else if ( this . translationBusiness . includes ( 'volcengine' ) ) {
// 火山引擎
return await this . TranslatePromptVolcengine ( value )
} else if ( this . translationBusiness . includes ( 'tencent' ) ) {
// 腾讯翻译
return await this . TranslatePromptTencent ( value )
} else if ( this . translationBusiness . includes ( 'aliyun' ) ) {
// 阿里云翻译
return await this . TranslatePromptAliyun ( value )
2024-08-09 12:28:28 +08:00
} else {
throw new Error ( '反推这边不支持GPT翻译, 请选择其他的翻译API' )
2024-08-03 12:46:12 +08:00
}
} catch ( error ) {
return {
code : 0 ,
message : error . toString ( )
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
}
2024-08-09 12:28:28 +08:00
//#region LaiTool GPT翻译
/ * *
*
* @ param value 使用laitool GPT翻译
* /
async TranslateReturnNowGPT ( value ) {
try {
// 判断该当前的翻译API
let translateData = value [ 0 ]
let from = value [ 1 ]
let to = value [ 2 ]
if ( value [ 3 ] ) {
throw new Error ( '使用GPT翻译不支持拆分' )
}
let model = this . translationAppId
let token = this . translationSecret
let data = {
model : model ,
messages : [ ]
}
// 开始调用GPT进行翻译
2024-08-20 10:37:38 +08:00
if ( from == 'en' && to == 'zh' ) {
2024-08-09 12:28:28 +08:00
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'system' ,
content :
'我想让你充当英译中专家, 用中文100%还原描述,不要加其他的联想,只翻译字面意思,请检查所有信息是否准确,并在回答时保持简活,不需要任何其他反馈。'
2024-08-09 12:28:28 +08:00
} )
// 添加示例
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'user' ,
content :
'A woman in her twenties with messy hair and a look of shock and despair. She is wearing a simple white shirt and jeans. Her face shows mixed emotions. The background is a dim and quiet room. The historical background is modern. The screen content is a close-up of the woman’ s face with tear marks.'
2024-08-09 12:28:28 +08:00
} )
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'assistant' ,
content :
'一位二十多岁的女人,头发凌乱,表情震惊和绝望。她穿着一件简单的白色衬衫和牛仔裤。她的脸上显示出复杂的情感。背景是一个昏暗安静的房间。历史背景是现代的。屏幕内容是女人脸部的特写,带有泪痕。'
2024-08-09 12:28:28 +08:00
} )
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'user' ,
content :
'A twenty-something woman with short curly hair, smiling, wearing a casual white shirt and jeans, sitting on a comfortable sofa with a cup of coffee in her hand. She is in a small and cozy living room with a few books on the bookshelf, modern interior design, and natural light pouring into the room in the late afternoon. Screen content: Part of the sofa cushions.'
2024-08-09 12:28:28 +08:00
} )
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'assistant' ,
content :
'一位二十多岁的女性,留着短卷发,面带微笑,穿着休闲的白色衬衫和牛仔裤,手拿一杯咖啡,坐在一个舒适的沙发上。她所在的是一个小而温馨的客厅,书架上有几本书,现代室内设计,下午晚些时候,自然光线洒进房间。屏幕内容:沙发垫的一部分。'
2024-08-09 12:28:28 +08:00
} )
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'user' ,
content :
"In a modern city, a streamlined car is parked on the street. A man in his thirties, with short brown hair combed back, a calm, confident look, tall and thin in a clean white shirt and black pants, sits in the car. The interior of the car is clean and modern, and the background is blurred to highlight the man's calm demeanor. The man's cell phone is ringing. The scene is set in the present."
2024-08-09 12:28:28 +08:00
} )
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'assistant' ,
content :
'在现代城市,一辆流线型轿车停在街上。一个三十多岁的男人,短短的棕色头发梳向后,神情冷静,自信,穿着干净的白衬衫和黑裤子,身材高挑瘦长,坐在车里。车内干净而现代,背景模糊,以突出男人平静的神态。男人的手机正在响。场景设定在现在。'
2024-08-09 12:28:28 +08:00
} )
2024-08-20 10:37:38 +08:00
} else if ( from == 'zh' && to == 'en' ) {
2024-08-09 12:28:28 +08:00
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'system' ,
content :
'我想让你充当中译英专家, 用中文100%还原描述,不要加其他的联想,只翻译字面意思,请检查所有信息是否准确,并在回答时保持简活,不需要任何其他反馈。'
2024-08-09 12:28:28 +08:00
} )
// 添加示例
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'user' ,
content :
'一位二十多岁的女人,头发凌乱,表情震惊和绝望。她穿着一件简单的白色衬衫和牛仔裤。她的脸上显示出复杂的情感。背景是一个昏暗安静的房间。历史背景是现代的。屏幕内容是女人脸部的特写,带有泪痕。'
2024-08-09 12:28:28 +08:00
} )
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'assistant' ,
content :
'A woman in her twenties with messy hair and a look of shock and despair. She is wearing a simple white shirt and jeans. Her face shows mixed emotions. The background is a dim and quiet room. The historical background is modern. The screen content is a close-up of the woman’ s face with tear marks.'
2024-08-09 12:28:28 +08:00
} )
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'user' ,
content :
'一位二十多岁的女性,留着短卷发,面带微笑,穿着休闲的白色衬衫和牛仔裤,手拿一杯咖啡,坐在一个舒适的沙发上。她所在的是一个小而温馨的客厅,书架上有几本书,现代室内设计,下午晚些时候,自然光线洒进房间。屏幕内容:沙发垫的一部分。'
2024-08-09 12:28:28 +08:00
} )
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'assistant' ,
content :
'A twenty-something woman with short curly hair, smiling, wearing a casual white shirt and jeans, sitting on a comfortable sofa with a cup of coffee in her hand. She is in a small and cozy living room with a few books on the bookshelf, modern interior design, and natural light pouring into the room in the late afternoon. Screen content: Part of the sofa cushions.'
2024-08-09 12:28:28 +08:00
} )
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'user' ,
content :
'在现代城市,一辆流线型轿车停在街上。一个三十多岁的男人,短短的棕色头发梳向后,神情冷静,自信,穿着干净的白衬衫和黑裤子,身材高挑瘦长,坐在车里。车内干净而现代,背景模糊,以突出男人平静的神态。男人的手机正在响。场景设定在现在。'
2024-08-09 12:28:28 +08:00
} )
data . messages . push ( {
2024-08-20 10:37:38 +08:00
role : 'assistant' ,
content :
"In a modern city, a streamlined car is parked on the street. A man in his thirties, with short brown hair combed back, a calm, confident look, tall and thin in a clean white shirt and black pants, sits in the car. The interior of the car is clean and modern, and the background is blurred to highlight the man's calm demeanor. The man's cell phone is ringing. The scene is set in the present."
2024-08-09 12:28:28 +08:00
} )
2024-08-20 10:37:38 +08:00
} else {
throw new Error ( 'GPT翻译只支持中英互译' )
}
2024-08-09 12:28:28 +08:00
data . messages . push ( {
role : 'user' ,
content : translateData
} )
2025-02-17 18:26:47 +08:00
let content = ''
// 判断整体是不是需要LMS转发
if ( global . config . useTransfer ) {
let url = define . lms + '/lms/Forward/SimpleTransfer'
let config = {
method : 'post' ,
url : url ,
maxBodyLength : Infinity ,
headers : {
'Content-Type' : 'application/json'
} ,
data : JSON . stringify ( {
url : this . translationBusiness ,
apiKey : token ,
dataString : JSON . stringify ( data )
} )
}
// 重试机制
let res = await RetryWithBackoff (
async ( ) => {
return await axios . request ( config )
} ,
5 ,
2000
)
if ( res . data . code != 1 ) {
throw new Error ( res . data . message )
}
content = GetOpenAISuccessResponse ( res . data . data )
} else {
let config = {
method : 'post' ,
maxBodyLength : Infinity ,
url : this . translationBusiness ,
headers : {
'Content-Type' : 'application/json' ,
Authorization : ` Bearer ${ token } `
} ,
data : JSON . stringify ( data )
}
let res = await RetryWithBackoff (
async ( ) => {
return await axios . request ( config )
} ,
5 ,
2000
)
// 将返回的数据进行拼接数据处理
content = GetOpenAISuccessResponse ( res . data )
2024-08-09 12:28:28 +08:00
}
let res _data = [ ]
if ( to == 'zh' ) {
res _data . push ( {
src : translateData ,
dst : content
} )
} else if ( to == 'en' ) {
res _data . push ( {
src : content ,
dst : translateData
} )
}
// 直接返回数据
return {
code : 1 ,
to : to ,
data : res _data
}
} catch ( error ) {
throw error
}
}
//#endregion
2024-08-03 12:46:12 +08:00
/ * *
* 阿里云翻译写入队列中
* @ param { * } value
* /
async TranslatePromptAliyun ( value ) {
try {
let win = this . global . newWindow . filter ( ( item ) => item . id == value [ 3 ] ) [ 0 ]
if ( ! win ) {
win = this . global . newWindow [ 0 ]
}
let translateData = value [ 0 ]
let from = value [ 1 ]
let to = value [ 2 ]
let batch = DEFINE _STRING . QUEUE _BATCH . TRANSLATE _PROMPT
for ( let i = 0 ; i < translateData . length ; i ++ ) {
const element = translateData [ i ]
this . global . requestQuene . enqueue (
async ( ) => {
if ( translateData . length > 5 ) {
await this . tools . delay ( 2000 )
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
let arr _data = [ ]
if ( to == 'zh' ) {
let tmp _data = element . prompt
arr _data = tmp _data . replaceAll ( '_' , ' ' ) . replaceAll ( ', ' , ',' ) . split ( ',' )
arr _data = arr _data . filter ( ( item ) => item != '' && item != null )
} else if ( to == 'en' ) {
for ( let j = 0 ; j < element . chinese _prompt . length ; j ++ ) {
const item = element . chinese _prompt [ j ]
if ( item != '' && item != null ) {
arr _data . push ( item . dst )
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
// 如果为空(直接返回)
if ( arr _data . length <= 0 ) {
return
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
let req _data = { }
for ( let j = 0 ; j < arr _data . length ; j ++ ) {
const element = arr _data [ j ]
req _data [ j . toString ( ) ] = element
2024-05-15 12:57:15 +08:00
}
let config = new OpenApi . Config ( {
2024-08-03 12:46:12 +08:00
accessKeyId : this . translationAppId ,
accessKeySecret : this . translationSecret
} )
config . endpoint = ` mt.cn-hangzhou.aliyuncs.com `
2024-05-15 12:57:15 +08:00
2024-08-03 12:46:12 +08:00
let client = new alimt20181012 . default ( config )
2024-05-15 12:57:15 +08:00
let getBatchTranslateRequest = new alimt20181012 . GetBatchTranslateRequest ( {
2024-08-03 12:46:12 +08:00
apiType : 'translate_standard' ,
scene : 'general' ,
sourceLanguage : from ,
targetLanguage : to ,
formatType : 'text' ,
sourceText : JSON . stringify ( req _data )
} )
let runtime = new Util . RuntimeOptions ( { } )
2024-05-15 12:57:15 +08:00
// 复制代码运行请自行打印 API 的返回值
2024-08-03 12:46:12 +08:00
let res = await client . getBatchTranslateWithOptions ( getBatchTranslateRequest , runtime )
console . log ( res )
2024-05-15 12:57:15 +08:00
// 处理返回的数据
// 检出返回的数据和输入的数据是不是一样的
2024-08-03 12:46:12 +08:00
let translateList = res . body . translatedList
if ( translateList . length != arr _data . length ) {
throw new Error ( '请求的数据长度和返回的数据长度不一致。请重试' )
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
let res _data = [ ]
2024-05-15 12:57:15 +08:00
// {
// "src": "blush",
// "dst": "脸红"
// }
2024-08-03 12:46:12 +08:00
if ( to == 'zh' ) {
for ( let j = 0 ; j < arr _data . length ; j ++ ) {
const item = arr _data [ j ]
let res _tmp = translateList . find ( ( item ) => item . index == j )
let obj = {
src : item ,
dst : res _tmp . translated
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
res _data . push ( obj )
}
} else if ( to == 'en' ) {
for ( let j = 0 ; j < arr _data . length ; j ++ ) {
const item = arr _data [ j ]
// 获取指定的index的返回数据
let res _tmp = translateList . find ( ( item ) => item . index == j )
let obj = {
src : res _tmp . translated ,
dst : item
}
res _data . push ( obj )
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
// 数据返回。写入本地配置文件
// 修改chinese_prompt
this . global . fileQueue . enqueue ( async ( ) => {
let json _path = path . join (
this . global . config . project _path ,
` tmp/input_crop/ ${ element . name } .json `
)
let prompt _json = JSON . parse ( await fspromises . readFile ( json _path , 'utf-8' ) )
prompt _json . chinese _prompt = res _data
await fspromises . writeFile ( json _path , JSON . stringify ( prompt _json ) )
} )
win . win . webContents . send ( DEFINE _STRING . TRANSLATE _RETURN _REFRESH , {
code : 1 ,
to : to ,
rowId : element . id ,
data : res _data
} )
} ,
` ${ batch } _ ${ element . name } ` ,
batch
)
}
// 监听总批次完成
this . global . requestQuene . setBatchCompletionCallback ( batch , ( failedTasks ) => {
if ( failedTasks . length > 0 ) {
let message = `
翻译任务都已完成 。
但是以下任务执行失败 :
`
failedTasks . forEach ( ( { taskId , error } ) => {
message += ` ${ taskId } -, \n 错误信息: ${ error } ` + '\n'
} )
this . global . newWindow [ 0 ] . win . webContents . send ( DEFINE _STRING . SHOW _MESSAGE _DIALOG , {
code : 0 ,
message : message
} )
} else {
if ( value [ 4 ] ) {
this . global . newWindow [ 0 ] . win . webContents . send ( DEFINE _STRING . SHOW _MESSAGE _DIALOG , {
code : 1 ,
message : '批次翻译任务完成'
} )
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
} )
return {
code : 1 ,
message : '翻译任务已加入队列任务中'
}
} catch ( error ) {
throw error
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
}
/ * *
* 阿里云翻译实时返回
* @ param { * } value
* /
async TranslateReturnNowAliyun ( value ) {
try {
// 判断该当前的翻译API
let from = value [ 1 ]
let to = value [ 2 ]
let ts _d = value [ 0 ] . replaceAll ( '_' , ' ' ) . replaceAll ( ', ' , ',' )
let req _data = { }
let req _count = 0
let req _arr = [ ]
if ( value [ 3 ] ) {
let tmp _arr = ts _d . split ( ',' )
for ( let i = 0 ; i < tmp _arr . length ; i ++ ) {
const element = tmp _arr [ i ]
if ( element != '' && element != null ) {
req _data [ i . toString ( ) ] = element
req _arr . push ( element )
}
req _count += 1
}
} else {
req _data [ '0' ] = ts _d
req _count = 1
req _arr . push ( ts _d )
}
if ( req _count <= 0 ) {
throw new Error ( '没有传入数据' )
}
let config = new OpenApi . Config ( {
accessKeyId : this . translationAppId ,
accessKeySecret : this . translationSecret
} )
config . endpoint = ` mt.cn-hangzhou.aliyuncs.com `
let client = new alimt20181012 . default ( config )
let getBatchTranslateRequest = new alimt20181012 . GetBatchTranslateRequest ( {
apiType : 'translate_standard' ,
scene : 'general' ,
sourceLanguage : from ,
targetLanguage : to ,
formatType : 'text' ,
sourceText : JSON . stringify ( req _data )
} )
let runtime = new Util . RuntimeOptions ( { } )
// 复制代码运行请自行打印 API 的返回值
let res = await client . getBatchTranslateWithOptions ( getBatchTranslateRequest , runtime )
console . log ( res )
// 处理返回的数据
// 检出返回的数据和输入的数据是不是一样的
let translateList = res . body . translatedList
if ( translateList . length != req _count ) {
throw new Error ( '请求的数据长度和返回的数据长度不一致。请重试' )
}
// {
// "src": "blush",
// "dst": "脸红"
// }
// 数据处理
let res _data = [ ]
for ( let j = 0 ; j < req _arr . length ; j ++ ) {
const item = req _arr [ j ]
let res _tmp = translateList . find ( ( item ) => item . index == j )
if ( to == 'zh' ) {
let obj = {
src : item ,
dst : res _tmp . translated
}
res _data . push ( obj )
} else if ( to == 'en' ) {
let obj = {
src : res _tmp . translated ,
dst : item
}
res _data . push ( obj )
}
}
// 直接返回数据
return {
code : 1 ,
to : to ,
data : res _data
}
} catch ( error ) {
throw error
}
}
/ * *
* 腾讯翻译实时返回
* @ param { * } value
* /
async TranslateReturnNowTencent ( value ) {
try {
// 判断该当前的翻译API
let from = value [ 1 ]
let to = value [ 2 ]
let ts _d = value [ 0 ] . replaceAll ( '_' , ' ' ) . replaceAll ( ', ' , ',' )
let req _data = [ ]
if ( value [ 3 ] ) {
req _data = ts _d . split ( ',' )
} else {
req _data . push ( ts _d )
}
req _data = req _data . filter ( ( item ) => item != '' && item != null )
if ( req _data . length <= 0 ) {
throw new Error ( '没有传入数据' )
}
const CvmClient = tencentcloud . tmt . v20180321 . Client
const client = new CvmClient ( {
credential : {
secretId : this . translationAppId ,
secretKey : this . translationSecret
} ,
// 产品地域
region : 'ap-shanghai' ,
// 可选配置实例
profile : {
signMethod : 'TC3-HMAC-SHA256' , // 签名方法
httpProfile : {
reqMethod : 'POST' , // 请求方法
reqTimeout : 30 // 请求超时时间, 默认60s
}
}
} )
let res = await client . TextTranslateBatch ( {
SourceTextList : req _data ,
Source : from ,
Target : to ,
ProjectId : 0
} )
console . log ( res )
// 处理返回的数据
// 检出返回的数据和输入的数据是不是一样的
let translateList = res . TargetTextList
if ( translateList . length != req _data . length ) {
throw new Error ( '请求的数据长度和返回的数据长度不一致。请重试' )
}
// {
// "src": "blush",
// "dst": "脸红"
// }
// 数据处理
let res _data = [ ]
for ( let j = 0 ; j < req _data . length ; j ++ ) {
const item = req _data [ j ]
if ( to == 'zh' ) {
let obj = {
src : item ,
dst : translateList [ j ]
}
res _data . push ( obj )
} else if ( to == 'en' ) {
let obj = {
src : translateList [ j ] ,
dst : item
}
res _data . push ( obj )
}
}
// 直接返回数据
return {
code : 1 ,
to : to ,
data : res _data
}
} catch ( error ) {
throw error
}
}
/ * *
* 腾讯翻译将翻译的消息写入到队列中
* @ param { * } value
* /
async TranslatePromptTencent ( value ) {
try {
let win = this . global . newWindow . filter ( ( item ) => item . id == value [ 3 ] ) [ 0 ]
if ( ! win ) {
win = this . global . newWindow [ 0 ]
}
let translateData = value [ 0 ]
let from = value [ 1 ]
let to = value [ 2 ]
let batch = DEFINE _STRING . QUEUE _BATCH . TRANSLATE _PROMPT
let secretId = this . translationAppId
let secretKey = this . translationSecret
const CvmClient = tencentcloud . tmt . v20180321 . Client
const client = new CvmClient ( {
credential : {
secretId : secretId ,
secretKey : secretKey
} ,
region : 'ap-shanghai' ,
profile : {
signMethod : 'TC3-HMAC-SHA256' , // 签名方法
httpProfile : {
reqMethod : 'POST' , // 请求方法
reqTimeout : 30 // 请求超时时间, 默认60s
}
}
} )
for ( let i = 0 ; i < translateData . length ; i ++ ) {
const element = translateData [ i ]
this . global . requestQuene . enqueue (
async ( ) => {
if ( translateData . length > 5 ) {
await this . tools . delay ( 2000 )
}
let arr _data = [ ]
if ( to == 'zh' ) {
let tmp _data = element . prompt
arr _data = tmp _data . replaceAll ( '_' , ' ' ) . replaceAll ( ', ' , ',' ) . split ( ',' )
arr _data = arr _data . filter ( ( item ) => item != '' && item != null )
} else if ( to == 'en' ) {
for ( let j = 0 ; j < element . chinese _prompt . length ; j ++ ) {
const item = element . chinese _prompt [ j ]
if ( item != '' && item != null ) {
arr _data . push ( item . dst )
}
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
// 如果为空(直接返回)
if ( arr _data . length <= 0 ) {
return
}
// 请求数据
let req _data = {
Source : from ,
Target : to ,
SourceTextList : arr _data ,
ProjectId : 0
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
let res = await client . TextTranslateBatch ( req _data )
console . log ( res )
2024-05-15 12:57:15 +08:00
// 处理返回的数据
// 检出返回的数据和输入的数据是不是一样的
2024-08-03 12:46:12 +08:00
let translateList = res . TargetTextList
if ( translateList . length != arr _data . length ) {
throw new Error ( '请求的数据长度和返回的数据长度不一致。请重试' )
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
let res _data = [ ]
2024-05-15 12:57:15 +08:00
// {
// "src": "blush",
// "dst": "脸红"
// }
2024-08-03 12:46:12 +08:00
if ( to == 'zh' ) {
for ( let j = 0 ; j < arr _data . length ; j ++ ) {
const item = arr _data [ j ]
let obj = {
src : item ,
dst : translateList [ j ]
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
res _data . push ( obj )
}
} else if ( to == 'en' ) {
for ( let j = 0 ; j < arr _data . length ; j ++ ) {
const item = arr _data [ j ]
let obj = {
src : translateList [ j ] ,
dst : item
}
res _data . push ( obj )
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
// 数据返回。写入本地配置文件
// 修改chinese_prompt
this . global . fileQueue . enqueue ( async ( ) => {
let json _path = path . join (
this . global . config . project _path ,
` tmp/input_crop/ ${ element . name } .json `
)
let prompt _json = JSON . parse ( await fspromises . readFile ( json _path , 'utf-8' ) )
prompt _json . chinese _prompt = res _data
await fspromises . writeFile ( json _path , JSON . stringify ( prompt _json ) )
2024-05-15 12:57:15 +08:00
} )
2024-08-03 12:46:12 +08:00
win . win . webContents . send ( DEFINE _STRING . TRANSLATE _RETURN _REFRESH , {
code : 1 ,
to : to ,
rowId : element . id ,
data : res _data
} )
} ,
` ${ batch } _ ${ element . name } ` ,
batch
)
}
// 监听总批次完成
this . global . requestQuene . setBatchCompletionCallback ( batch , ( failedTasks ) => {
if ( failedTasks . length > 0 ) {
let message = `
2024-05-15 12:57:15 +08:00
翻译任务都已完成 。
但是以下任务执行失败 :
`
2024-08-03 12:46:12 +08:00
failedTasks . forEach ( ( { taskId , error } ) => {
message += ` ${ taskId } -, \n 错误信息: ${ error } ` + '\n'
} )
this . global . newWindow [ 0 ] . win . webContents . send ( DEFINE _STRING . SHOW _MESSAGE _DIALOG , {
code : 0 ,
message : message
} )
} else {
if ( value [ 4 ] ) {
this . global . newWindow [ 0 ] . win . webContents . send ( DEFINE _STRING . SHOW _MESSAGE _DIALOG , {
code : 1 ,
message : '批次翻译任务完成'
2024-05-15 12:57:15 +08:00
} )
2024-08-03 12:46:12 +08:00
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
} )
return {
code : 1 ,
message : '翻译任务已加入队列任务中'
}
} catch ( error ) {
throw error
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
}
/ * *
* 火山引擎翻译实时返回
* @ param { * } value
* /
async TranslateReturnNowVolcengine ( value ) {
try {
// 判断该当前的翻译API
let from = value [ 1 ]
let to = value [ 2 ]
let ts _d = value [ 0 ] . replaceAll ( '_' , ' ' ) . replaceAll ( ', ' , ',' )
let req _data = [ ]
if ( value [ 3 ] ) {
req _data = ts _d . split ( ',' )
} else {
req _data . push ( ts _d )
}
if ( req _data . length <= 0 ) {
throw new Error ( '没有传入数据' )
}
let signer = await this . GetVolcengineSinger ( )
let config = {
method : 'post' ,
maxBodyLength : Infinity ,
url : ` ${ this . translationBusiness } ${ signer } ` ,
headers : {
'Content-Type' : 'application/json'
} ,
data : JSON . stringify ( {
SourceLanguage : from ,
TargetLanguage : to ,
TextList : req _data
} )
}
let res = await axios . request ( config )
if ( res . status != 200 ) {
throw new Error ( '请求错误。请检查网络' )
}
// 判断是不是有返回错误
if ( res . data . ResponseMetadata && res . data . ResponseMetadata . Error ) {
let err = res . data . ResponseMetadata . Error
throw new Error (
` 错误码: ${ err . Code } 错误编号: ${ err . CodeN } 错误详细信息: ${ err . Message } `
)
}
// 处理返回的数据
// 检出返回的数据和输入的数据是不是一样的
let translateList = res . data . TranslationList
if ( translateList . length != req _data . length ) {
throw new Error ( '请求的数据长度和返回的数据长度不一致。请重试' )
}
// {
// "src": "blush",
// "dst": "脸红"
// }
// 数据处理
let res _data = [ ]
for ( let j = 0 ; j < req _data . length ; j ++ ) {
const item = req _data [ j ]
if ( to == 'zh' ) {
let obj = {
src : item ,
dst : translateList [ j ] . Translation
}
res _data . push ( obj )
} else if ( to == 'en' ) {
let obj = {
src : translateList [ j ] . Translation ,
dst : item
}
res _data . push ( obj )
}
}
// 直接返回数据
return {
code : 1 ,
to : to ,
data : res _data
}
} catch ( error ) {
throw error
}
}
/ * *
* 火山引擎翻译所有数据队列返回
* @ param { * } value
* /
async TranslatePromptVolcengine ( value ) {
try {
let win = this . global . newWindow . filter ( ( item ) => item . id == value [ 3 ] ) [ 0 ]
if ( ! win ) {
win = this . global . newWindow [ 0 ]
}
let signer = await this . GetVolcengineSinger ( )
let translateData = value [ 0 ]
let from = value [ 1 ]
let to = value [ 2 ]
let batch = DEFINE _STRING . QUEUE _BATCH . TRANSLATE _PROMPT
for ( let i = 0 ; i < translateData . length ; i ++ ) {
const element = translateData [ i ]
this . global . requestQuene . enqueue (
async ( ) => {
if ( translateData . length > 5 ) {
await this . tools . delay ( 2000 )
}
let arr _data = [ ]
if ( to == 'zh' ) {
let tmp _data = element . prompt
arr _data = tmp _data . replaceAll ( '_' , ' ' ) . replaceAll ( ', ' , ',' ) . split ( ',' )
arr _data = arr _data . filter ( ( item ) => item != '' && item != null )
} else if ( to == 'en' ) {
for ( let j = 0 ; j < element . chinese _prompt . length ; j ++ ) {
const item = element . chinese _prompt [ j ]
if ( item != '' && item != null ) {
arr _data . push ( item . dst )
}
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
// 如果为空(直接返回)
if ( arr _data . length <= 0 ) {
return
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
// 开始请求
let req _data = JSON . stringify ( {
SourceLanguage : from ,
TargetLanguage : to ,
TextList : arr _data
} )
2024-05-15 12:57:15 +08:00
let config = {
2024-08-03 12:46:12 +08:00
method : 'post' ,
maxBodyLength : Infinity ,
url : ` ${ this . translationBusiness } ${ signer } ` ,
headers : {
'Content-Type' : 'application/json'
} ,
data : req _data
}
let res = await axios . request ( config )
console . log ( res )
2024-05-15 12:57:15 +08:00
if ( res . status != 200 ) {
2024-08-03 12:46:12 +08:00
throw new Error ( '请求状态码错误。请检查网络' )
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
2024-05-15 12:57:15 +08:00
// 判断是不是有返回错误
if ( res . data . ResponseMetadata && res . data . ResponseMetadata . Error ) {
2024-08-03 12:46:12 +08:00
let err = res . data . ResponseMetadata . Error
throw new Error (
` 错误码: ${ err . Code } 错误编号: ${ err . CodeN } 错误详细信息: ${ err . Message } `
)
2024-05-15 12:57:15 +08:00
}
// 处理返回的数据
// 检出返回的数据和输入的数据是不是一样的
2024-08-03 12:46:12 +08:00
let translateList = res . data . TranslationList
if ( translateList . length != arr _data . length ) {
throw new Error ( '请求的数据长度和返回的数据长度不一致。请重试' )
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
let res _data = [ ]
2024-05-15 12:57:15 +08:00
// {
// "src": "blush",
// "dst": "脸红"
// }
2024-08-03 12:46:12 +08:00
if ( to == 'zh' ) {
for ( let j = 0 ; j < arr _data . length ; j ++ ) {
const item = arr _data [ j ]
let obj = {
src : item ,
dst : translateList [ j ] . Translation
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
res _data . push ( obj )
}
} else if ( to == 'en' ) {
for ( let j = 0 ; j < arr _data . length ; j ++ ) {
const item = arr _data [ j ]
let obj = {
src : translateList [ j ] . Translation ,
dst : item
}
res _data . push ( obj )
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
// 数据返回。写入本地配置文件
// 修改chinese_prompt
this . global . fileQueue . enqueue ( async ( ) => {
let json _path = path . join (
this . global . config . project _path ,
` tmp/input_crop/ ${ element . name } .json `
)
let prompt _json = JSON . parse ( await fspromises . readFile ( json _path , 'utf-8' ) )
prompt _json . chinese _prompt = res _data
await fspromises . writeFile ( json _path , JSON . stringify ( prompt _json ) )
} )
win . win . webContents . send ( DEFINE _STRING . TRANSLATE _RETURN _REFRESH , {
code : 1 ,
to : to ,
rowId : element . id ,
data : res _data
} )
} ,
` ${ batch } _ ${ element . name } ` ,
batch
)
}
// 监听总批次完成
this . global . requestQuene . setBatchCompletionCallback ( batch , ( failedTasks ) => {
if ( failedTasks . length > 0 ) {
let message = `
2024-05-15 12:57:15 +08:00
翻译任务都已完成 。
但是以下任务执行失败 :
`
2024-08-03 12:46:12 +08:00
failedTasks . forEach ( ( { taskId , error } ) => {
message += ` ${ taskId } -, \n 错误信息: ${ error } ` + '\n'
} )
this . global . newWindow [ 0 ] . win . webContents . send ( DEFINE _STRING . SHOW _MESSAGE _DIALOG , {
code : 0 ,
message : message
} )
} else {
if ( value [ 4 ] ) {
this . global . newWindow [ 0 ] . win . webContents . send ( DEFINE _STRING . SHOW _MESSAGE _DIALOG , {
code : 1 ,
message : '批次翻译任务完成'
2024-05-15 12:57:15 +08:00
} )
2024-08-03 12:46:12 +08:00
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
} )
return {
code : 1 ,
message : '翻译任务已加入队列任务中'
}
} catch ( error ) {
throw error
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
}
/ * *
* 获取火山引擎请求的签名
* /
async GetVolcengineSinger ( ) {
try {
const openApiRequestData = {
method : 'POST' ,
region : 'cn-north-1' ,
params : {
Action : 'TranslateText' ,
Version : '2020-06-01'
} ,
Service : 'translate'
}
const credentials = {
accessKeyId : this . translationAppId ,
secretKey : this . translationSecret
}
const signer = new Signer ( openApiRequestData , 'translate' )
// 最终经过加签的 HTTP Query Params
const signedQueryString = signer . getSignUrl ( credentials )
console . log ( signedQueryString )
return signedQueryString
} catch ( error ) {
throw error
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
}
/ * *
* 百度引擎翻译翻译所有数据
* @ param { } value
* @ returns
* /
async TranslatePromptBaidu ( value ) {
try {
let win = this . global . newWindow . filter ( ( item ) => item . id == value [ 3 ] ) [ 0 ]
if ( ! win ) {
win = this . global . newWindow [ 0 ]
}
let translateData = value [ 0 ]
let from = value [ 1 ]
let to = value [ 2 ]
let batch = DEFINE _STRING . QUEUE _BATCH . TRANSLATE _PROMPT
let appId = this . translationAppId
// 添加一个频次判断是不是演示
for ( let i = 0 ; i < translateData . length ; i ++ ) {
const element = translateData [ i ]
this . global . requestQuene . enqueue (
async ( ) => {
try {
if ( translateData . length > 5 ) {
await this . tools . delay ( 2000 )
}
let ts _d = ''
if ( to == 'zh' ) {
ts _d = element . prompt
. replaceAll ( '_' , ' ' )
. replaceAll ( ', ' , ',' )
. replaceAll ( ',' , '\n' )
} else if ( to == 'en' ) {
let tmp _arr = [ ]
// 中文转英文。重新拼接一下
for ( let j = 0 ; j < element . chinese _prompt . length ; j ++ ) {
const item = element . chinese _prompt [ j ]
tmp _arr . push ( item . dst )
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
ts _d = tmp _arr . join ( '\n' )
}
let salt = Date . now ( )
let sign = MD5 (
` ${ this . translationAppId } ${ ts _d } ${ salt } ${ this . translationSecret } `
) . toString ( )
let res = await axios . get ( this . translationBusiness , {
2024-05-15 12:57:15 +08:00
params : {
2024-08-03 12:46:12 +08:00
q : ts _d ,
appid : appId ,
salt : salt ,
from : from ,
to : to ,
sign : sign
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
} )
if ( res . status != 200 ) {
throw new Error ( '请求错误。请检查网络' )
}
// 判断是不是有错误码
if ( res . data . error _code ) {
throw new Error ( res . data . error _msg )
}
let res _data = [ ]
// 将所有的数据协会到本地(然后发送消息到前台界面)
if ( res . data . to == 'zh' ) {
2024-05-15 12:57:15 +08:00
res _data = res . data . trans _result
2024-08-03 12:46:12 +08:00
} else {
2024-05-15 12:57:15 +08:00
// 直接在这边处理(前端不用处理)
for ( let i = 0 ; i < res . data . trans _result . length ; i ++ ) {
2024-08-03 12:46:12 +08:00
const element = res . data . trans _result [ i ]
let obj = {
src : element . dst ,
dst : element . src
}
res _data . push ( obj )
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
}
// 修改chinese_prompt
this . global . fileQueue . enqueue ( async ( ) => {
let json _path = path . join (
this . global . config . project _path ,
` tmp/input_crop/ ${ element . name } .json `
)
let prompt _json = JSON . parse ( await fspromises . readFile ( json _path , 'utf-8' ) )
prompt _json . chinese _prompt = res _data
await fspromises . writeFile ( json _path , JSON . stringify ( prompt _json ) )
} )
win . win . webContents . send ( DEFINE _STRING . TRANSLATE _RETURN _REFRESH , {
2024-05-15 12:57:15 +08:00
code : 1 ,
to : to ,
2024-08-03 12:46:12 +08:00
rowId : element . id ,
2024-05-15 12:57:15 +08:00
data : res _data
2024-08-03 12:46:12 +08:00
} )
} catch ( error ) {
throw error
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
} ,
` ${ batch } _ ${ element . name } ` ,
batch
)
}
// 监听总批次完成
this . global . requestQuene . setBatchCompletionCallback ( batch , ( failedTasks ) => {
if ( failedTasks . length > 0 ) {
let message = `
翻译任务都已完成 。
但是以下任务执行失败 :
`
failedTasks . forEach ( ( { taskId , error } ) => {
message += ` ${ taskId } -, \n 错误信息: ${ error } ` + '\n'
} )
this . global . newWindow [ 0 ] . win . webContents . send ( DEFINE _STRING . SHOW _MESSAGE _DIALOG , {
code : 0 ,
message : message
} )
} else {
if ( value [ 4 ] ) {
this . global . newWindow [ 0 ] . win . webContents . send ( DEFINE _STRING . SHOW _MESSAGE _DIALOG , {
code : 1 ,
message : '批次翻译任务完成'
} )
}
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
} )
return {
code : 1 ,
message : '翻译任务已加入队列任务中'
}
} catch ( error ) {
throw error
2024-05-15 12:57:15 +08:00
}
2024-08-03 12:46:12 +08:00
}
/ * *
* 百度翻译引擎翻译单个数据 。 立即返回
* @ param { * } value
* @ returns
* /
async TranslateReturnNowBaidu ( value ) {
try {
// 判断该当前的翻译API
let from = value [ 1 ]
let to = value [ 2 ]
let appId = this . translationAppId
let ts _d = value [ 0 ] . replaceAll ( '_' , ' ' ) . replaceAll ( ', ' , ',' )
if ( value [ 3 ] ) {
ts _d = ts _d . replaceAll ( ',' , '\n' )
}
let salt = Date . now ( )
let sign = MD5 ( ` ${ this . translationAppId } ${ ts _d } ${ salt } ${ this . translationSecret } ` ) . toString ( )
let res = await axios . get ( this . translationBusiness , {
params : {
q : ts _d ,
appid : appId ,
salt : salt ,
from : from ,
to : to ,
sign : sign
}
} )
if ( res . status != 200 ) {
throw new Error ( '请求错误。请检查网络' )
}
// 判断是不是有错误码
if ( res . data . error _code ) {
throw new Error ( res . data . error _msg )
}
let res _data = [ ]
// 将所有的数据协会到本地(然后发送消息到前台界面)
if ( res . data . to == 'zh' ) {
res _data = res . data . trans _result
} else {
// 直接在这边处理(前端不用处理)
for ( let i = 0 ; i < res . data . trans _result . length ; i ++ ) {
const element = res . data . trans _result [ i ]
let obj = {
src : element . dst ,
dst : element . src
}
res _data . push ( obj )
}
}
// 直接返回数据
return {
code : 1 ,
to : to ,
data : res _data
}
} catch ( error ) {
throw error
}
}
}