车辆多语言修复

This commit is contained in:
2025-05-13 15:29:30 +08:00
parent af0e8bc62f
commit 21b19109d0
8 changed files with 198 additions and 86 deletions
+115 -74
View File
@@ -10,6 +10,7 @@ use app\model\VehicleAr;
use app\model\VehicleEn;
use app\model\VehicleRu;
use app\service\OssService;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\Location;
use support\Log;
use support\Request;
@@ -33,9 +34,15 @@ class VehicleController extends Crud
* 不需要鉴权的方法
* @var string[]
*/
protected $noNeedAuth = ['detail'];
protected $noNeedAuth = ['detail', 'status'];
protected $status = ['未知','待发车','在途(发往fbo港口)','抵达发货港口','在途(发往目的地仓库)','抵达仓库','售出'];
protected $status = [
'zh-cn' => ['未知','待发车','在途(发往fbo港口)','抵达发货港口','在途(发往目的地仓库)','抵达目的地','售出'],
// 'zh' => ['未知','待发车','在途(发往fbo港口)','抵达发货港口','在途(发往目的地仓库)','抵达目的地','售出'],
'en' => ['unknown','Awaiting departure','In transit (to FBO port)','Arriving at the port of shipment','In transit (to destination warehouse)','Arriving at the destination','Sold'],
'ru' => ['Неизвестно','Ожидание отправления','В пути (в порт FBO)','Прибытие в порт отправления','В пути (отправлено на склад назначения)','Прибытие в пункт назначения','Продано'],
'ar' => ['غير معروف','في انتظار سيارة','على الطريق ( ميناء FBO )','الوصول إلى ميناء الشحن','في الطريق ( مستودع الوجهة )','الوصول إلى الوجهة','بيع'],
];
/**
* @var Vehicle
@@ -50,6 +57,13 @@ class VehicleController extends Crud
{
$this->model = new Vehicle;
}
public function status()
{
$lang = session('lang', 'zh-cn');
$status = $this->status[$lang];
return json(['code' => 0, 'msg' => 'success', 'data' => $status]);
}
/**
* 浏览
@@ -160,50 +174,7 @@ class VehicleController extends Crud
// 如果是 POST 请求,处理数据保存和翻译
if ($request->method() === 'POST') {
// 获取表单数据
$data = $request->all();
$data['shipping_cycle'] = !empty($data['shipping_cycle'])?$data['shipping_cycle']:0;
// 保存主表数据
$vehicle = new Vehicle();
$vehicle->fill($data);
$vehicle->save();
Log::info('$request',$data);
// 处理多语言翻译
$languages = ['en', 'ar', 'ru'];
$translationModels = [
'en' => VehicleEn::class,
'ar' => VehicleAr::class,
'ru' => VehicleRu::class
];
// 创建翻译服务实例
$translatorService = new \app\service\TranslatorService();
foreach ($languages as $lang) {
$translationModel = $translationModels[$lang];
$translation = new $translationModel();
// 复制主表数据
$translation->fill($data);
$translation->id = $vehicle->id;
// 使用翻译服务处理需要翻译的字段
$translatedData = $translatorService->vehicle($data, $lang);
Log::info($lang.':'.$vehicle->id,$translatedData);
// 更新翻译后的字段
$translation->brand = $translatedData['brand'] ?? $data['brand'];
$translation->model = $translatedData['model'] ?? $data['model'];
$translation->status = $translatedData['status'] ?? $data['status'];
$translation->purchase_location = $translatedData['purchase_location'] ?? $data['purchase_location'];
$translation->procedure_location = $translatedData['procedure_location'] ?? $data['procedure_location'];
// $translation->fbo_destination = $translatedData['fbo_destination'] ?? $data['fbo_destination'];
$translation->save();
}
$this->save($request);
}
return view('vehicle/insert', [
@@ -212,6 +183,70 @@ class VehicleController extends Crud
]);
}
/**
* 保存
*/
public function save(Request $request)
{
// 获取表单数据
$data = $request->all();
$data['shipping_cycle'] = !empty($data['shipping_cycle'])?$data['shipping_cycle']:0;
// 保存主表数据
if(!empty($data['id'])){
$vehicle = Vehicle::find($data['id']);
}else{
$vehicle = new Vehicle();
}
$vehicle->fill($data);
// return json($vehicle);
$vehicle->save();
Log::info('$request',$data);
// 处理多语言翻译
$languages = ['en', 'ar', 'ru'];
$translationModels = [
'en' => VehicleEn::class,
'ar' => VehicleAr::class,
'ru' => VehicleRu::class
];
// 创建翻译服务实例
$translatorService = new \app\service\TranslatorService();
foreach ($languages as $lang) {
$translationModel = $translationModels[$lang];
if(!empty($data['id'])){
// $vehicle = Vehicle::find($data['id']);
$translation = $translationModel::find($data['id']);
}else{
$translation = new $translationModel();
}
// 复制主表数据
$translation->fill($data);
$translation->id = $vehicle->id;
// 使用翻译服务处理需要翻译的字段
$translatedData = $translatorService->vehicle($data, $lang);
Log::info($lang.':'.$vehicle->id,$translatedData);
// 更新翻译后的字段
$translation->brand = $translatedData['brand'] ?? $data['brand'];
$translation->model = $translatedData['model'] ?? $data['model'];
// $translation->status = $translatedData['status'] ?? $data['status'];
$translation->purchase_location = $translatedData['purchase_location'] ?? $data['purchase_location'];
$translation->procedure_location = $translatedData['procedure_location'] ?? $data['procedure_location'];
// $translation->fbo_destination = $translatedData['fbo_destination'] ?? $data['fbo_destination'];
$translation->save();
}
}
/**
* 更新
* @param Request $request
@@ -258,6 +293,11 @@ class VehicleController extends Crud
];
$translations = array_merge($required_translations, $translations);
}
// 如果是 POST 请求,处理数据保存和翻译
if ($request->method() === 'POST') {
$this->save($request);
}
$data = Vehicle::where('id', $id)->first();
if (!$data) {
@@ -338,42 +378,43 @@ class VehicleController extends Crud
*/
protected function afterQuery($items)
{
$lang = session('lang', 'zh-cn');
switch ($lang) {
case 'zh-cn':
$vehicle = new \app\model\Vehicle();
$hz = new \app\model\Location();
break;
case 'en':
$vehicle = new \app\model\VehicleEn();
$hz = new \app\model\LocationEn();
break;
case 'ru':
$vehicle = new \app\model\VehicleRu();
$hz = new \app\model\LocationRu();
break;
case 'ar':
$vehicle = new \app\model\VehicleAr();
$hz = new \app\model\LocationAr();
break;
}
foreach ($items as &$item) {
$location = Location::find($item->locations_id);
// $item->status = $this->status[$item->status]??$this->status[0];
$item = $vehicle::find($item['id']);
$location = $hz::find($item->locations_id);
$item['status_txt'] = $this->status[$lang][$item->status]??$this->status[$lang][0];
// $item->status = "已到达迪拜仓库";
$item->location_name = $location ? $location->name : '';
$final_location = Location::find($item->final_locations_id);
$final_location = $hz::find($item->final_locations_id);
$item->final_location_name = $final_location ? $final_location->name : '';
$fbo_location = Location::find($item->fbo_locations_id);
$fbo_location = $hz::find($item->fbo_locations_id);
$item->fbo_location_name = $fbo_location ? $fbo_location->name : '';
$lang = session('lang', 'zh-cn');
switch ($lang) {
case 'zh-cn':
$vehicle = new \app\model\Vehicle();
$hz = new \app\model\Location();
break;
case 'en':
$vehicle = new \app\model\VehicleEn();
$hz = new \app\model\LocationEn();
break;
case 'ru':
$vehicle = new \app\model\VehicleRu();
$hz = new \app\model\LocationRu();
break;
case 'ar':
$vehicle = new \app\model\VehicleAr();
$hz = new \app\model\LocationAr();
break;
}
$location = $hz::find($item->locations_id);
$item->location_name = $location ? $location->name : '';
$car = $vehicle::find($item->id);
$item->status = $car ? $car->status : $item->status;
// $car = $vehicle::find($item->id);
// $item->status = $car ? $car->status : $item->status;
$location = $hz::find($item->final_location_id);
$item->final_location_name = $location ? $location->name : '';
$location = $hz::find($item->fbo_locations_id);