-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Baidu translation engine support (#768)
* feat: Added Baidu translation support * fix: Baidu translation results are not displayed correctly * Fix merge error in Turkish translation --------- Co-authored-by: Alex Terehov <[email protected]>
- Loading branch information
1 parent
6815d55
commit fd4af07
Showing
18 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters