Skip to content

Commit

Permalink
feat: add Baidu translation engine support (#768)
Browse files Browse the repository at this point in the history
* feat: Added Baidu translation support

* fix: Baidu translation results are not displayed correctly

* Fix merge error in Turkish translation

---------

Co-authored-by: Alex Terehov <terales@users.noreply.github.com>
  • Loading branch information
end-candle and terales committed Aug 27, 2023
1 parent 6815d55 commit fd4af07
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 0 deletions.
2 changes: 2 additions & 0 deletions locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "Maximale Zeichenlänge für Hinweise die im Text angezeigt werden. Abgeschnitte Zeichen werden als (...) angezeigt",
"config.annotations": "Hinweise im Text aktivieren",
"config.auto_detection": "Automatische Erkennung von Übersetzung aktivieren",
"config.baidu_app_secret": "",
"config.baidu_appid": "",
"config.deepl_api_key": "API key für die DeepL translate engine",
"config.deepl_log": "DeepL debug logs anzeigen",
"config.deepl_use_free_api_entry": "Kostenlose DeepL Version benutzen",
Expand Down
2 changes: 2 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "Max number of characters shown in the inline annotations. Excess characters will display as an ellipses (...)",
"config.annotations": "Enable the inline annotations.",
"config.auto_detection": "Enable locales auto detection for projects",
"config.baidu_app_secret": "APP Secret to use Baidu translate engine",
"config.baidu_appid": "APP ID to use Baidu translate engine",
"config.deepl_api_key": "API key to use DeepL translate engine",
"config.deepl_log": "Show DeepL engine debug logs",
"config.deepl_use_free_api_entry": "Use DeepL Free API entry point",
Expand Down
2 changes: 2 additions & 0 deletions locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "Máximo número de caracteres que se mostrarán en las anotaciones en línea. Los restantes se mostrarán como elipses (...)",
"config.annotations": "Habilitar las anotaciones en línea.",
"config.auto_detection": "",
"config.baidu_app_secret": "",
"config.baidu_appid": "",
"config.deepl_api_key": "API key para el traductor DeepL",
"config.deepl_log": "Mostrar mensajes de depuración del motor DeepL",
"config.deepl_use_free_api_entry": "",
Expand Down
2 changes: 2 additions & 0 deletions locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "Nombre maximum de caractères à afficher dans les annotations. Le surplus sera affiché sous forme d'ellipse (...)",
"config.annotations": "Activer les annotations.",
"config.auto_detection": "Activer la détection automatique des langues pour les projets",
"config.baidu_app_secret": "",
"config.baidu_appid": "",
"config.deepl_api_key": "Clé API pour utiliser le moteur de traduction DeepL",
"config.deepl_log": "Montrer le journal de debug du moteur DeepL",
"config.deepl_use_free_api_entry": "Utiliser le point d'entrée gratuite de DeepL",
Expand Down
2 changes: 2 additions & 0 deletions locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "インラインアノテーションの最大文字数です。超過している部分は(...)と表示されます。",
"config.annotations": "インラインアノテーションを有効にする.",
"config.auto_detection": "",
"config.baidu_app_secret": "",
"config.baidu_appid": "",
"config.deepl_api_key": "",
"config.deepl_log": "",
"config.deepl_use_free_api_entry": "",
Expand Down
2 changes: 2 additions & 0 deletions locales/nb-NO.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "Maksimalt antall tegn vist i hjelpevindu. Resterende del vil bli vist som (...)",
"config.annotations": "Slå på hjelpevindu",
"config.auto_detection": "",
"config.baidu_app_secret": "",
"config.baidu_appid": "",
"config.deepl_api_key": "",
"config.deepl_log": "",
"config.deepl_use_free_api_entry": "",
Expand Down
2 changes: 2 additions & 0 deletions locales/nl-NL.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "Max. aantal karakters zichtbaar binnen inline annotaties. Overschreden delen worden getoond als ellipses (...)",
"config.annotations": "Inline annotaties inschakelen.",
"config.auto_detection": "",
"config.baidu_app_secret": "",
"config.baidu_appid": "",
"config.deepl_api_key": "",
"config.deepl_log": "",
"config.deepl_use_free_api_entry": "",
Expand Down
2 changes: 2 additions & 0 deletions locales/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "Número máximo de caracteres exibidos nas anotações inline. O restante será exibido em forma de reticências (...)",
"config.annotations": "Habilitar anotações inline",
"config.auto_detection": "",
"config.baidu_app_secret": "",
"config.baidu_appid": "",
"config.deepl_api_key": "",
"config.deepl_log": "",
"config.deepl_use_free_api_entry": "",
Expand Down
2 changes: 2 additions & 0 deletions locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "Максимальное количество символов, отображаемых во встроенных аннотациях. Лишние символы будут отображаться в виде многоточия (...)",
"config.annotations": "Включить встроенные аннотации.",
"config.auto_detection": "Включить автоматическое определение переводов для проектов",
"config.baidu_app_secret": "",
"config.baidu_appid": "",
"config.deepl_api_key": "API ключ для использования DeepL",
"config.deepl_log": "Отображать отладочные журналы DeepL",
"config.deepl_use_free_api_entry": "Использовать бесплатную версию DeepL API",
Expand Down
2 changes: 2 additions & 0 deletions locales/sv-SE.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "Max antal bokstävers som visas i insatta anteckningen. Överflödiga bokstäver kommer visas som en ellips (…)",
"config.annotations": "Aktivera insatta anteckningar.",
"config.auto_detection": "",
"config.baidu_app_secret": "",
"config.baidu_appid": "",
"config.deepl_api_key": "",
"config.deepl_log": "",
"config.deepl_use_free_api_entry": "",
Expand Down
2 changes: 2 additions & 0 deletions locales/tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "Satır aralığında gösterilen ek açıklamaların maksimum karakter uzunluğu. Fazladan karakterler noktalı şekilde görüntülenecektir (...)",
"config.annotations": "Satır aralığında ek açıklamaları etkinleştir",
"config.auto_detection": "Projeler için otomatik çeviri algılamasını etkinleştir",
"config.baidu_app_secret": "",
"config.baidu_appid": "",
"config.deepl_api_key": "DeepL çeviri sistemini kullanmak için API anahtarı",
"config.deepl_log": "DeepL sisteminin hata ayıklama kayıtlarını göster",
"config.deepl_use_free_api_entry": "DeepL'in Bedava API giriş noktasını kullanın",
Expand Down
2 changes: 2 additions & 0 deletions locales/uk-UA.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "Максимальна кількість символів, що відображаються у вбудованих анотаціях. Зайві символи будуть відображатися у вигляді трьох крапок (...)",
"config.annotations": "Включити вбудовані анотації.",
"config.auto_detection": "",
"config.baidu_app_secret": "",
"config.baidu_appid": "",
"config.deepl_api_key": "Ключ для використання API DeepL",
"config.deepl_log": "Відображати налагоджувальні журнали DeepL",
"config.deepl_use_free_api_entry": "",
Expand Down
2 changes: 2 additions & 0 deletions locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "内联注释显示的最大字符数。超出的部分将显示为省略号(...)",
"config.annotations": "启用内联注释",
"config.auto_detection": "",
"config.baidu_app_secret": "百度平台分配的密钥",
"config.baidu_appid": "百度平台APPID",
"config.deepl_api_key": "",
"config.deepl_log": "",
"config.deepl_use_free_api_entry": "",
Expand Down
2 changes: 2 additions & 0 deletions locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"config.annotation_max_length": "內聯註釋顯示的最大文字數。超出的部分將顯示為省略號(...)",
"config.annotations": "啟用內聯註釋",
"config.auto_detection": "",
"config.baidu_app_secret": "",
"config.baidu_appid": "",
"config.deepl_api_key": "",
"config.deepl_log": "",
"config.deepl_use_free_api_entry": "",
Expand Down
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@
"google-cn",
"deepl",
"libretranslate",
"baidu",
"openai"
]
},
Expand Down Expand Up @@ -1059,6 +1060,16 @@
"default": null,
"description": "%config.deepl_api_key%"
},
"i18n-ally.translate.baidu.appid": {
"type": "string",
"default": null,
"description": "%config.baidu_appid%"
},
"i18n-ally.translate.baidu.apiSecret": {
"type": "string",
"default": null,
"description": "%config.baidu_app_secret%"
},
"i18n-ally.translate.deepl.enableLog": {
"type": "boolean",
"default": false,
Expand Down
8 changes: 8 additions & 0 deletions src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,14 @@ export class Config {
.update(key, value, isGlobal)
}

static get baiduApiSecret() {
return this.getConfig<string | null | undefined>('translate.baidu.apiSecret')
}

static get baiduAppid() {
return this.getConfig<string | null | undefined>('translate.baidu.appid')
}

static get googleApiKey() {
return this.getConfig<string | null | undefined>('translate.google.apiKey')
}
Expand Down
84 changes: 84 additions & 0 deletions src/translators/engines/baidu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import crypto from 'crypto'
import axios from 'axios'
import qs from 'qs'
import TranslateEngine, { TranslateOptions, TranslateResult } from './base'
import { Config } from '~/core'

interface BaiduSignOptions {
appid: string | null | undefined
salt: string
secret: string | null | undefined
query: string
}

export default class BaiduTranslate extends TranslateEngine {
apiLink = 'https://fanyi.baidu.com'
apiRoot = 'https://fanyi-api.baidu.com'

async translate(options: TranslateOptions) {
let { from = 'auto', to = 'auto' } = options

from = this.convertToSupportedLocalesForGoogleCloud(from)
to = this.convertToSupportedLocalesForGoogleCloud(to)

const appid = Config.baiduAppid
const secret = Config.baiduApiSecret
const salt = Date.now().toString()
const sign = this.getSign({ appid, secret, query: options.text, salt })

const form = {
q: options.text,
appid,
salt,
from,
to,
sign,
}

const { data } = await axios({
method: 'GET',
url: `${this.apiRoot}/api/trans/vip/translate?${qs.stringify(form)}`,
})

return this.transform(data, options)
}

convertToSupportedLocalesForGoogleCloud(locale: string): string {
return locale.replace(/-/g, '_').split('_')[0]
}

getSign({ appid, salt, query, secret }: BaiduSignOptions): string {
if (appid && salt) {
const string = appid + query + salt + secret
const md5 = crypto.createHash('md5')
md5.update(string)
return md5.digest('hex')
}
return ''
}

transform(response: any, options: TranslateOptions): TranslateResult {
const { text } = options

const r: TranslateResult = {
text,
to: response.to,
from: response.from,
response,
linkToResult: `${this.apiLink}/#${response.from}/${response.to}/${text}`,
}

try {
const result: string[] = []
response.trans_result.forEach((v: any) => {
result.push(v.dst)
})
r.result = result
}
catch (e) {}

if (!r.result) r.error = new Error((`[${response.error_code}] ${response.error_msg}`) || 'No result')

return r
}
}
3 changes: 3 additions & 0 deletions src/translators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import GoogleTranslateEngine from './engines/google'
import GoogleTranslateCnEngine from './engines/google-cn'
import DeepLTranslateEngine from './engines/deepl'
import LibreTranslateEngine from './engines/libretranslate'
import BaiduTranslate from './engines/baidu'
import OpenAITranslateEngine from './engines/openai'

export class Translator {
Expand All @@ -11,6 +12,7 @@ export class Translator {
'google-cn': new GoogleTranslateCnEngine(),
'deepl': new DeepLTranslateEngine(),
'libretranslate': new LibreTranslateEngine(),
'baidu': new BaiduTranslate(),
'openai': new OpenAITranslateEngine(),
}

Expand All @@ -26,6 +28,7 @@ export {
GoogleTranslateCnEngine,
DeepLTranslateEngine,
LibreTranslateEngine,
BaiduTranslate,
OpenAITranslateEngine,
}

Expand Down

0 comments on commit fd4af07

Please sign in to comment.