仓库多语言编辑

This commit is contained in:
2025-05-13 15:29:56 +08:00
parent 21b19109d0
commit 302acbc5ab
6 changed files with 236 additions and 97 deletions
+3
View File
@@ -27,6 +27,9 @@ class Location extends Model
*/ */
public $timestamps = false; public $timestamps = false;
//批量赋值黑名单
protected $guarded = ['created_at', 'updated_at'];
/** /**
* Get the vehicles for the location. * Get the vehicles for the location.
*/ */
+4 -4
View File
@@ -14,11 +14,11 @@ class TranslatorService
$this->xunfei = new Xunfei(); $this->xunfei = new Xunfei();
} }
public function vehicle($vehicle, string $targetLang = 'en') { public function vehicle($vehicle, string $targetLang = 'en', string $fromLang = 'cn') {
$fields = ['brand', 'model', 'purchase_location', 'procedure_location', 'fbo_destination']; $fields = ['brand', 'model', 'purchase_location', 'procedure_location', 'fbo_destination'];
foreach ($vehicle as $k => $var) { foreach ($vehicle as $k => $var) {
if (in_array($k, $fields)) { if (in_array($k, $fields)) {
$rst_dst = $this->xunfei->fanyi($var, 'cn', $targetLang); $rst_dst = $this->xunfei->fanyi($var, $fromLang, $targetLang);
if ($rst_dst) { if ($rst_dst) {
$vehicle[$k] = $rst_dst["trans_result"]["dst"]; $vehicle[$k] = $rst_dst["trans_result"]["dst"];
} }
@@ -27,11 +27,11 @@ class TranslatorService
return $vehicle; return $vehicle;
} }
public function location($location, string $targetLang = 'en') { public function location($location, string $targetLang = 'en', string $fromLang = 'cn') {
$fields = ['name']; $fields = ['name'];
foreach ($location as $k => $var) { foreach ($location as $k => $var) {
if (in_array($k, $fields)) { if (in_array($k, $fields)) {
$rst_dst = $this->xunfei->fanyi($var, 'cn', $targetLang); $rst_dst = $this->xunfei->fanyi($var, $fromLang, $targetLang);
if ($rst_dst) { if ($rst_dst) {
$location[$k] = $rst_dst["trans_result"]["dst"]; $location[$k] = $rst_dst["trans_result"]["dst"];
} }
@@ -54,6 +54,38 @@ class LocationController extends Crud
]); ]);
} }
/**
* 查询后置处理,添加位置信息
* @param mixed $items
* @return mixed
*/
protected function afterQuery($items)
{
$lang = session('lang', 'zh-cn');
switch ($lang) {
case 'zh-cn':
$location = new \app\model\Location();
break;
case 'en':
$location = new \app\model\LocationEn();
break;
case 'ru':
$location = new \app\model\LocationRu();
break;
case 'ar':
$location = new \app\model\LocationAr();
break;
}
foreach ($items as &$item) {
$item = $location::find($item['id']);
}
return $items;
}
/** /**
* 插入 * 插入
* @param Request $request * @param Request $request
@@ -74,44 +106,45 @@ class LocationController extends Crud
// 如果是 POST 请求,处理数据保存和翻译 // 如果是 POST 请求,处理数据保存和翻译
if ($request->method() === 'POST') { if ($request->method() === 'POST') {
// 获取表单数据 $this->save($request);
$data = $request->all(); // // 获取表单数据
Log::info('location',$data); // $data = $request->all();
// Log::info('location',$data);
// 保存主表数据 //
$location = new Location(); // // 保存主表数据
$location->fill($data); // $location = new Location();
$location->save(); // $location->fill($data);
// $location->save();
// 处理多语言翻译 //
$languages = ['en', 'ar', 'ru']; // // 处理多语言翻译
$translationModels = [ // $languages = ['en', 'ar', 'ru'];
'en' => \app\model\LocationEn::class, // $translationModels = [
'ar' => \app\model\LocationAr::class, // 'en' => \app\model\LocationEn::class,
'ru' => \app\model\LocationRu::class // 'ar' => \app\model\LocationAr::class,
]; // 'ru' => \app\model\LocationRu::class
// ];
// 创建翻译服务实例 //
$translatorService = new \app\service\TranslatorService(); // // 创建翻译服务实例
// $translatorService = new \app\service\TranslatorService();
foreach ($languages as $lang) { //
$translationModel = $translationModels[$lang]; // foreach ($languages as $lang) {
$translation = new $translationModel(); // $translationModel = $translationModels[$lang];
// $translation = new $translationModel();
// 复制主表数据 //
$translation->fill($data); // // 复制主表数据
$translation->id = $location->id; // $translation->fill($data);
// $translation->id = $location->id;
// 使用翻译服务处理需要翻译的字段 //
$translatedData = $translatorService->location($data, $lang); // // 使用翻译服务处理需要翻译的字段
// $translatedData = $translatorService->location($data, $lang);
// 更新翻译后的字段 //
$translation->name = $translatedData['name'] ?? $data['name']; // // 更新翻译后的字段
// $translation->description = $translatedData['description'] ?? $data['description']; // $translation->name = $translatedData['name'] ?? $data['name'];
//// $translation->description = $translatedData['description'] ?? $data['description'];
Log::info('location:'.$lang.':translatedData',$translatedData); //
$translation->save(); // Log::info('location:'.$lang.':translatedData',$translatedData);
} // $translation->save();
// }
} }
return view('location/insert', [ return view('location/insert', [
@@ -120,6 +153,66 @@ class LocationController extends Crud
]); ]);
} }
/**
* 保存
*/
public function save(Request $request)
{
// 获取表单数据
$data = $request->all();
$lang = session('lang', 'zh-cn');
$locationModel = $this->getModel($lang);
// 保存主表数据
if(!empty($data['id'])){
$location = $locationModel::find($data['id']);
}else{
$location = $locationModel;
}
$location->fill($data);
$location->save();
// 处理多语言翻译
$languages = ['zh-cn' => 'cn', 'en' => 'en', 'ar' => 'ar', 'ru' => 'ru'];
$translationModels = [
'cn' => \app\model\Location::class,
'en' => \app\model\LocationEn::class,
'ar' => \app\model\LocationAr::class,
'ru' => \app\model\LocationRu::class
];
//删除已保存的模型
unset($translationModels[$lang]);
unset($languages[$lang]);
// 创建翻译服务实例
$translatorService = new \app\service\TranslatorService();
foreach ($languages as $language) {
$translationModel = $translationModels[$language];
if(!empty($data['id'])){
$translation = $translationModel::find($data['id']);
}else{
$translation = new $translationModel();
}
// 复制主表数据
$translation->fill($data);
$translation->id = $location->id;
// 使用翻译服务处理需要翻译的字段
$translatedData = $translatorService->location($data, $language,$lang);
log::info('$lang',[$language,$lang,$translatedData]);
// 更新翻译后的字段
$translation->name = $translatedData['name'] ?? $data['name'];
$translation->save();
}
}
/** /**
* 更新 * 更新
* @param Request $request * @param Request $request
@@ -129,7 +222,8 @@ class LocationController extends Crud
public function update(Request $request): Response public function update(Request $request): Response
{ {
if ($request->method() === 'POST') { if ($request->method() === 'POST') {
return parent::update($request); // return parent::update($request);
$this->save($request);
} }
// 获取当前语言 // 获取当前语言
$lang = session('lang', 'zh-cn'); $lang = session('lang', 'zh-cn');
@@ -153,24 +247,32 @@ class LocationController extends Crud
public function getAll() public function getAll()
{ {
$lang = session('lang', 'zh-cn'); $lang = session('lang', 'zh-cn');
switch ($lang) { $hz = $this->getModel($lang);
case 'zh-cn': $locations = $hz::get();
$hz = new \app\model\Location(); return Util::success($locations);
break; }
/**
* @param $lang
* @return \app\model\Location|\app\model\LocationAr|\app\model\LocationEn|\app\model\LocationRu
*/
protected function getModel($lang){
switch ($lang) {
case 'en': case 'en':
$hz = new \app\model\LocationEn(); $hz = new \app\model\LocationEn();
break; break;
case 'ru': case 'ru':
$hz = new \app\model\LocationRu(); $hz = new \app\model\LocationRu();
break; break;
case 'ar': case 'ar':
$hz = new \app\model\LocationAr(); $hz = new \app\model\LocationAr();
break; break;
default:
$hz = new \app\model\Location();
break;
} }
$locations = $hz::get(); return $hz;
return Util::success($locations);
} }
} }
@@ -192,32 +192,36 @@ class VehicleController extends Crud
$data = $request->all(); $data = $request->all();
$data['shipping_cycle'] = !empty($data['shipping_cycle'])?$data['shipping_cycle']:0; $data['shipping_cycle'] = !empty($data['shipping_cycle'])?$data['shipping_cycle']:0;
$lang = session('lang', 'zh-cn');
list($vehicleModel,$local) = $this->getModel($lang);
// 保存主表数据 // 保存主表数据
if(!empty($data['id'])){ if(!empty($data['id'])){
$vehicle = Vehicle::find($data['id']); $vehicle = $vehicleModel::find($data['id']);
}else{ }else{
$vehicle = new Vehicle(); $vehicle =$vehicleModel;
} }
$vehicle->fill($data); $vehicle->fill($data);
// return json($vehicle);
$vehicle->save(); $vehicle->save();
Log::info('$request',$data);
// 处理多语言翻译 // 处理多语言翻译
$languages = ['en', 'ar', 'ru']; $languages = ['zh-cn' => 'cn', 'en' => 'en', 'ar' => 'ar', 'ru' => 'ru'];
$translationModels = [ $translationModels = [
'cn' => Vehicle::class,
'en' => VehicleEn::class, 'en' => VehicleEn::class,
'ar' => VehicleAr::class, 'ar' => VehicleAr::class,
'ru' => VehicleRu::class 'ru' => VehicleRu::class
]; ];
//删除已保存的模型
unset($translationModels[$lang]);
unset($languages[$lang]);
// 创建翻译服务实例 // 创建翻译服务实例
$translatorService = new \app\service\TranslatorService(); $translatorService = new \app\service\TranslatorService();
foreach ($languages as $lang) { foreach ($languages as $language) {
$translationModel = $translationModels[$lang]; $translationModel = $translationModels[$language];
if(!empty($data['id'])){ if(!empty($data['id'])){
// $vehicle = Vehicle::find($data['id']); // $vehicle = Vehicle::find($data['id']);
$translation = $translationModel::find($data['id']); $translation = $translationModel::find($data['id']);
@@ -230,8 +234,8 @@ class VehicleController extends Crud
$translation->id = $vehicle->id; $translation->id = $vehicle->id;
// 使用翻译服务处理需要翻译的字段 // 使用翻译服务处理需要翻译的字段
$translatedData = $translatorService->vehicle($data, $lang); log::info('lang:',[$language,$lang]);
Log::info($lang.':'.$vehicle->id,$translatedData); $translatedData = $translatorService->vehicle($data, $language,$lang);
// 更新翻译后的字段 // 更新翻译后的字段
$translation->brand = $translatedData['brand'] ?? $data['brand']; $translation->brand = $translatedData['brand'] ?? $data['brand'];
@@ -407,6 +411,7 @@ class VehicleController extends Crud
// $item->status = "已到达迪拜仓库"; // $item->status = "已到达迪拜仓库";
$item->location_name = $location ? $location->name : ''; $item->location_name = $location ? $location->name : '';
$final_location = $hz::find($item->final_locations_id); $final_location = $hz::find($item->final_locations_id);
// log::info('final_location_name:',[$item->final_locations_id]);
$item->final_location_name = $final_location ? $final_location->name : ''; $item->final_location_name = $final_location ? $final_location->name : '';
$fbo_location = $hz::find($item->fbo_locations_id); $fbo_location = $hz::find($item->fbo_locations_id);
$item->fbo_location_name = $fbo_location ? $fbo_location->name : ''; $item->fbo_location_name = $fbo_location ? $fbo_location->name : '';
@@ -415,8 +420,8 @@ class VehicleController extends Crud
$item->location_name = $location ? $location->name : ''; $item->location_name = $location ? $location->name : '';
// $car = $vehicle::find($item->id); // $car = $vehicle::find($item->id);
// $item->status = $car ? $car->status : $item->status; // $item->status = $car ? $car->status : $item->status;
$location = $hz::find($item->final_location_id); // $location = $hz::find($item->final_location_id);
$item->final_location_name = $location ? $location->name : ''; // $item->final_location_name = $location ? $location->name : '';
$location = $hz::find($item->fbo_locations_id); $location = $hz::find($item->fbo_locations_id);
$item->fbo_location_name = $location ? $location->name : ''; $item->fbo_location_name = $location ? $location->name : '';
@@ -447,6 +452,35 @@ class VehicleController extends Crud
return $items; return $items;
} }
/**
* @param $lang
* @return array
*/
protected function getModel($lang){
switch ($lang) {
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;
default:
$vehicle = new \app\model\Vehicle();
$hz = new \app\model\Location();
break;
}
return array($vehicle, $hz);
}
/** /**
* 获取翻译 * 获取翻译
* @param Request $request * @param Request $request
+16 -16
View File
@@ -79,18 +79,18 @@
align: "center", align: "center",
field: "name", field: "name",
},{ },{
title: translations.name_en, // title: translations.name_en,
align: "center", // align: "center",
field: "name_en", // field: "name_en",
},{ // },{
title: translations.address, // title: translations.address,
align: "center", // align: "center",
field: "address", // field: "address",
},{ // },{
title: translations.area, // title: translations.area,
align: "center", // align: "center",
field: "area", // field: "area",
},{ // },{
title: translations.type, title: translations.type,
align: "center", align: "center",
field: "type", field: "type",
@@ -104,10 +104,10 @@
return util.escape(items.join(",")); return util.escape(items.join(","));
} }
},{ },{
title: translations.num, // title: translations.num,
align: "center", // align: "center",
field: "num", // field: "num",
},{ // },{
title: translations.created_at, title: translations.created_at,
align: "center", align: "center",
field: "created_at", field: "created_at",
+20 -20
View File
@@ -51,26 +51,26 @@
</div> </div>
</div> </div>
<div class="layui-form-item"> <!-- <div class="layui-form-item">-->
<label class="layui-form-label">车辆数</label> <!-- <label class="layui-form-label">车辆数</label>-->
<div class="layui-input-block"> <!-- <div class="layui-input-block">-->
<input type="number" name="num" value="" class="layui-input"> <!-- <input type="number" name="num" value="" class="layui-input">-->
</div> <!-- </div>-->
</div> <!-- </div>-->
<!-- -->
<div class="layui-form-item"> <!-- <div class="layui-form-item">-->
<label class="layui-form-label">创建时间</label> <!-- <label class="layui-form-label">创建时间</label>-->
<div class="layui-input-block"> <!-- <div class="layui-input-block">-->
<input type="text" name="created_at" id="created_at" autocomplete="off" class="layui-input"> <!-- <input type="text" name="created_at" id="created_at" autocomplete="off" class="layui-input">-->
</div> <!-- </div>-->
</div> <!-- </div>-->
<!-- -->
<div class="layui-form-item"> <!-- <div class="layui-form-item">-->
<label class="layui-form-label">更新时间</label> <!-- <label class="layui-form-label">更新时间</label>-->
<div class="layui-input-block"> <!-- <div class="layui-input-block">-->
<input type="text" name="updated_at" id="updated_at" autocomplete="off" class="layui-input"> <!-- <input type="text" name="updated_at" id="updated_at" autocomplete="off" class="layui-input">-->
</div> <!-- </div>-->
</div> <!-- </div>-->
</div> </div>
</div> </div>