39 lines
981 B
PHP
39 lines
981 B
PHP
|
|
<?php
|
||
|
|
namespace app\queue\translation\service;
|
||
|
|
|
||
|
|
use app\queue\utils\XunfeiFanyi;
|
||
|
|
use support\Log;
|
||
|
|
|
||
|
|
class TranslatorClient
|
||
|
|
{
|
||
|
|
|
||
|
|
// protected string $endpoint;
|
||
|
|
// protected string $apiKey;
|
||
|
|
|
||
|
|
// public function __construct()
|
||
|
|
// {
|
||
|
|
// $this->endpoint = env('TRANSLATION_API_URL');
|
||
|
|
// $this->apiKey = env('TRANSLATION_API_KEY');
|
||
|
|
// }
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 翻译文本
|
||
|
|
* @throws \RuntimeException
|
||
|
|
*/
|
||
|
|
public function translate(string $text, string $from = 'cn', string $to = 'ru'): string
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$xunfei = new XunfeiFanyi();
|
||
|
|
$rst_dst = $xunfei->fanyi($text, $from, $to);
|
||
|
|
if ($rst_dst) {
|
||
|
|
return $rst_dst["trans_result"]["dst"];
|
||
|
|
} else {
|
||
|
|
return '';
|
||
|
|
}
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
Log::error("TranslatorClient 错误: ".$e->getMessage());
|
||
|
|
throw new \RuntimeException('翻译服务调用失败');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|