mirror of
https://gitee.com/mirrors/AllinSSL.git
synced 2026-03-14 02:20:53 +08:00
【新增】插件git同步模块,用于同步项目内容,加速项目开发
【调整】前端暗色问题
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import { TranslationAdapter } from './index.js'
|
||||
import { ZhipuAITranslator } from '../ai/zhipuAI.js'
|
||||
import { QianwenAITranslator } from '../ai/qianwenAI.js'
|
||||
import { DeepSeekAITranslator } from '../ai/deepseekAI.js'
|
||||
import config from '../../config/config.js'
|
||||
|
||||
/**
|
||||
* AI批量翻译适配器 - 用于处理大规模AI翻译服务
|
||||
*/
|
||||
export class AIBatchAdapter extends TranslationAdapter {
|
||||
constructor() {
|
||||
super()
|
||||
this.translator = new DeepSeekAITranslator(config.apiKey[config.translateMethod])
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染翻译名称
|
||||
* @returns {Promise<string>} 生成的唯一翻译名称
|
||||
*/
|
||||
renderTranslateName(index) {
|
||||
const timestamp = Date.now()
|
||||
return `t_${index}_${timestamp}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行AI批量翻译 - 包含错误重试机制
|
||||
* @param {string} text - 待翻译的文本内容
|
||||
* @param {string[]} languages - 目标语言列表
|
||||
* @param {number} maxRetries - 最大重试次数
|
||||
* @param {number} index - 翻译名称索引
|
||||
* @returns {Promise<{text: string, translations: Record<string, string}>} 翻译结果对象
|
||||
* @throws {Error} 当所有重试都失败时抛出错误
|
||||
*/
|
||||
async translate(text, languages, maxRetries, index) {
|
||||
let lastError = null
|
||||
let retryCount = 0
|
||||
while (retryCount <= maxRetries) {
|
||||
try {
|
||||
const result = await this.translator.translate({
|
||||
text,
|
||||
languages,
|
||||
})
|
||||
const key = this.renderTranslateName(index)
|
||||
return {
|
||||
text,
|
||||
key,
|
||||
translations: result.translations,
|
||||
}
|
||||
} catch (error) {
|
||||
lastError = error
|
||||
retryCount++
|
||||
// 如果还有重试机会,等待一段时间后重试
|
||||
if (retryCount <= maxRetries) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000 * retryCount))
|
||||
continue
|
||||
}
|
||||
|
||||
throw new Error(`AI批量翻译失败(已重试${retryCount}次) - ${lastError.message}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取AI翻译服务支持的语言列表
|
||||
* @returns {string[]} 支持的语言代码列表
|
||||
*/
|
||||
getSupportedLanguages() {
|
||||
return this.translator.getSupportedLanguages()
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证API密钥是否有效
|
||||
* @param {string} apiKey - 待验证的API密钥
|
||||
* @returns {Promise<boolean>} 密钥是否有效
|
||||
*/
|
||||
async validateApiKey(apiKey) {
|
||||
try {
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* 翻译适配器基类 - 用于统一不同翻译服务的接口实现
|
||||
*/
|
||||
export class TranslationAdapter {
|
||||
constructor() {
|
||||
if (this.constructor === TranslationAdapter) {
|
||||
throw new Error('翻译适配器:抽象类不能被直接实例化')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行翻译 - 将给定文本翻译为目标语言
|
||||
* @param {string} text - 待翻译的文本内容
|
||||
* @param {string} apiKey - 翻译服务的API密钥
|
||||
* @param {string[]} languages - 目标语言代码列表,如 ['enUS', 'jaJP']
|
||||
* @param {number} maxRetries - 翻译失败时的最大重试次数
|
||||
* @returns {Promise<{text: string, translations: Record<string, string}>} 翻译结果对象
|
||||
* @throws {Error} 当翻译失败且超过重试次数时抛出错误
|
||||
*/
|
||||
async translate(text, apiKey, languages, maxRetries) {
|
||||
throw new Error('翻译适配器:translate 方法必须在子类中实现')
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前适配器支持的语言列表
|
||||
* @returns {string[]} 支持的语言代码列表
|
||||
*/
|
||||
getSupportedLanguages() {
|
||||
throw new Error('翻译适配器:getSupportedLanguages 方法必须在子类中实现')
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查指定语言是否被当前适配器支持
|
||||
* @param {string} language - 需要检查的语言代码
|
||||
* @returns {boolean} 是否支持该语言
|
||||
*/
|
||||
isLanguageSupported(language) {
|
||||
return this.getSupportedLanguages().includes(language)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import { TranslationAdapter } from './index.js'
|
||||
import { translate as traditionalApiTranslate } from '../traditional/api1.js'
|
||||
|
||||
/**
|
||||
* 传统API翻译适配器 - 用于适配常规REST API类型的翻译服务
|
||||
*/
|
||||
export class TraditionalApiAdapter extends TranslationAdapter {
|
||||
constructor(apiModule) {
|
||||
super()
|
||||
if (!apiModule?.translate || typeof apiModule.translate !== 'function') {
|
||||
throw new Error('传统API适配器:无效的API模块,必须提供translate方法')
|
||||
}
|
||||
this.apiModule = apiModule
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行翻译请求 - 将数据转换为传统API格式并处理响应
|
||||
* @param {string} text - 待翻译的文本内容
|
||||
* @param {string} apiKey - API密钥
|
||||
* @param {string[]} languages - 目标语言列表
|
||||
* @param {number} maxRetries - 最大重试次数
|
||||
* @returns {Promise<{text: string, translations: Record<string, string>}>} 标准化的翻译结果
|
||||
* @throws {Error} 当翻译失败或语言不支持时抛出错误
|
||||
*/
|
||||
async translate(text, apiKey, languages, maxRetries) {
|
||||
// 检查所有目标语言是否支持
|
||||
for (const lang of languages) {
|
||||
if (!this.isLanguageSupported(lang)) {
|
||||
throw new Error(`传统API适配器:不支持的目标语言 "${lang}"`)
|
||||
}
|
||||
}
|
||||
|
||||
// 转换为API期望的请求格式
|
||||
const requestData = {
|
||||
text,
|
||||
apiKey,
|
||||
targetLanguages: languages,
|
||||
retryCount: maxRetries,
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await this.apiModule.translate(requestData)
|
||||
return {
|
||||
text,
|
||||
translations: result.translations,
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`传统API适配器:翻译失败 - ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取API支持的语言列表
|
||||
* @returns {string[]} 支持的语言代码数组
|
||||
*/
|
||||
getSupportedLanguages() {
|
||||
return this.apiModule.getSupportedLanguages?.() || []
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user