工单分类
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace app\queue\translation\service;
|
||||
|
||||
// use Webman\RedisQueue\Client;
|
||||
use Webman\RedisQueue\Redis;
|
||||
|
||||
class TranslationService
|
||||
{
|
||||
|
||||
// 配置
|
||||
protected $config = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = getConfig('/queue/translation/config/trans.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步排队所有语种的翻译任务
|
||||
*
|
||||
* @param string $entityType 实体名,如 'vehicle'
|
||||
* @param int $entityId 主表 ID
|
||||
* @param string $sourceLocale 源语种,如 'zh'
|
||||
* @param array $sourceFields 源语种字段 ['name'=>…,'condition_desc'=>…]
|
||||
* @param array $clientFields 前端已传翻译 ['en'=>[...], …]
|
||||
*/
|
||||
public function queueTranslations(string $entityType, int $entityId, string $sourceLocale, array $sourceFields, array $clientFields = []): void
|
||||
{
|
||||
foreach ($this->config['locales'] as $locale) {
|
||||
$payload = [
|
||||
'entityType' => $entityType,
|
||||
'entityId' => $entityId,
|
||||
'locale' => $locale,
|
||||
'sourceLocale' => $sourceLocale,
|
||||
'sourceFields' => $sourceFields,
|
||||
'clientFields' => $clientFields[$locale] ?? [],
|
||||
];
|
||||
Redis::send('transTask', $payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?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('翻译服务调用失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user