Files
car_info/app/service/TranslatorService.php
T

55 lines
1.7 KiB
PHP
Raw Normal View History

2025-05-09 16:04:16 +08:00
<?php
namespace app\service;
use app\util\Xunfei;
2025-05-28 09:30:01 +08:00
use support\Log;
use Webman\RedisQueue\Redis;
2025-05-09 16:04:16 +08:00
class TranslatorService
{
private Xunfei $xunfei;
private $lang = ['zh'=>'cn', 'en'=>'en','ru'=>'ru','ar'=>'ar'];
public function __construct() {
$this->xunfei = new Xunfei();
}
2025-05-13 15:29:56 +08:00
public function vehicle($vehicle, string $targetLang = 'en', string $fromLang = 'cn') {
2025-05-09 16:04:16 +08:00
$fields = ['brand', 'model', 'purchase_location', 'procedure_location', 'fbo_destination'];
foreach ($vehicle as $k => $var) {
if (in_array($k, $fields)) {
2025-05-13 15:29:56 +08:00
$rst_dst = $this->xunfei->fanyi($var, $fromLang, $targetLang);
2025-05-09 16:04:16 +08:00
if ($rst_dst) {
$vehicle[$k] = $rst_dst["trans_result"]["dst"];
}
}
}
2025-05-28 16:18:28 +08:00
log::info('fanyi',$vehicle);
2025-05-09 16:04:16 +08:00
return $vehicle;
}
2025-05-13 15:29:56 +08:00
public function location($location, string $targetLang = 'en', string $fromLang = 'cn') {
2025-05-09 16:04:16 +08:00
$fields = ['name'];
foreach ($location as $k => $var) {
if (in_array($k, $fields)) {
2025-05-13 15:29:56 +08:00
$rst_dst = $this->xunfei->fanyi($var, $fromLang, $targetLang);
2025-05-09 16:04:16 +08:00
if ($rst_dst) {
$location[$k] = $rst_dst["trans_result"]["dst"];
}
}
}
return $location;
}
2025-05-28 09:30:01 +08:00
2025-05-28 17:55:16 +08:00
public function queue($field,$sourceLang,$type,$id,$toLang) {
2025-05-28 09:30:01 +08:00
// 队列名
$queue = 'translator';
// 数据,可以直接传数组,无需序列化
2025-05-28 17:55:16 +08:00
$data = ['entityType' => $type, 'sourceFields' => $field, 'sourceLang' => $sourceLang, 'locale' => 'hello', 'id' => $id, 'toLang' => $toLang];
2025-05-28 09:30:01 +08:00
// 投递消息
Redis::send($queue, $data);
return true;
}
2025-05-09 16:04:16 +08:00
}