文章 > [vue3+element-plus+webman]机器翻译聚合接口

17:31 2022/06/02 3622

[vue3+element-plus+webman]机器翻译聚合接口

依次看了阿里云,腾讯云,百度云的机器翻译接口,阿里云一个月有100w字符的免费额度,腾讯云一个月有500w字符免费额度,百度云一年500w字符免费额度,但是百度翻译有个开放平台,这上面最基础版的完全免费的但是限制QPS为1,对于轻度个人使用完全够了,高级版一个月也是有200w字符的免费额度,由此,腾讯云和百度感觉比较“良心啊”。

阿里云

他有php的sdk可供使用,支持composer包管理
composer require alibabacloud/sdk
调用也很方便

  1. use AlibabaCloud\Client\AlibabaCloud;
  2. use AlibabaCloud\Client\Exception\ClientException;
  3. use AlibabaCloud\Client\Exception\ServerException;
  4. // 创建一个全局客户端
  5. AlibabaCloud::accessKeyClient('<your-access-key-id>', '<your-access-key-secret>')
  6. ->regionId('cn-hangzhou')
  7. ->asGlobalClient();
  8. try {
  9. $result = AlibabaCloud::alimt()
  10. ->v20181012() //通用版本
  11. ->translateGeneral()
  12. ->method('POST') //设置请求POST
  13. ->withSourceLanguage('en') //源语言
  14. ->withSourceText('book') //原文
  15. ->withFormatType('text') //翻译文本的格式
  16. ->withTargetLanguage('zh') //目标语言
  17. ->request();
  18. \print_r($result->toArray());
  19. } catch (ServerException $e) {
  20. \print_r($e->getErrorMessage());
  21. \print_r($e->getRequestId());
  22. } catch (ClientException $e) {
  23. \print_r($e->getErrorMessage());
  24. }

也不需要自己去组装签名什么的了,很方便。

腾讯云

他也有php的sdk可供使用,支持composer包管理
composer require tencentcloud/tencentcloud-sdk-php
他的调用代码可以去腾讯云后台ApiExplorer自动生成

  1. require_once 'vendor/autoload.php';
  2. use TencentCloud\Common\Credential;
  3. use TencentCloud\Common\Profile\ClientProfile;
  4. use TencentCloud\Common\Profile\HttpProfile;
  5. use TencentCloud\Common\Exception\TencentCloudSDKException;
  6. use TencentCloud\Tmt\V20180321\TmtClient;
  7. use TencentCloud\Tmt\V20180321\Models\TextTranslateRequest;
  8. try {
  9. $cred = new Credential("SecretId", "SecretKey");
  10. $httpProfile = new HttpProfile();
  11. $httpProfile->setEndpoint("tmt.tencentcloudapi.com");
  12. $clientProfile = new ClientProfile();
  13. $clientProfile->setHttpProfile($httpProfile);
  14. $client = new TmtClient($cred, "ap-chongqing", $clientProfile);
  15. $req = new TextTranslateRequest();
  16. $params = array(
  17. "SourceText" => "hello world",
  18. "Source" => "auto",
  19. "Target" => "zh",
  20. "ProjectId" => 0
  21. );
  22. $req->fromJsonString(json_encode($params));
  23. $resp = $client->TextTranslate($req);
  24. print_r($resp->toJsonString());
  25. }
  26. catch(TencentCloudSDKException $e) {
  27. echo $e;
  28. }

这个也是一样,当然自己拼接签名这些也是可以的,腾讯云阿里云都有提供签名生成算法。

百度翻译开放平台(不是百度云)

这个就没有现成的php sdk使用了,不过他的签名方式很简单,下面是一个官方的完整请求的示例,php用curl就可以了,也支持post方式请求。
http://api.fanyi.baidu.com/api/trans/vip/translate?q=apple&from=en&to=zh&appid=2015063000000001&salt=1435660288&sign=f89f9594663708c1605f3d736d01d2d4

  1. $params = [
  2. 'q'=>$text,
  3. 'from'=>$source,
  4. 'to'=>$target,
  5. 'appid'=>'你的appid',
  6. 'salt'=>md5(time().rand(1000,9999))
  7. ];
  8. $params['sign'] = get_bd_sign($params,'你的key');
  9. $rst = curlPost('https://fanyi-api.baidu.com/api/trans/vip/translate',http_build_query($params),['Content-Type:application/x-www-form-urlencoded']);
  10. function get_bd_sign(array $arr,string $app_key)
  11. {
  12. return md5($arr['appid'].$arr['q'].$arr['salt'].$app_key);
  13. }

这样的话,就可以选择性使用接口了,但是腾讯云的接口重复翻译相同内容,也会计算字符数的,所以可以在本地数据库缓存一下翻译的内容。