创建时翻译
This commit is contained in:
@@ -27,6 +27,9 @@ class LocationAr 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.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ class LocationEn 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.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ class LocationRu 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.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ class VehicleAr extends Model
|
|||||||
*/
|
*/
|
||||||
public $timestamps = false;
|
public $timestamps = false;
|
||||||
|
|
||||||
|
//批量赋值黑名单
|
||||||
|
protected $guarded = ['created_at', 'updated_at'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the location that owns the vehicle.
|
* Get the location that owns the vehicle.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ class VehicleEn extends Model
|
|||||||
*/
|
*/
|
||||||
public $timestamps = false;
|
public $timestamps = false;
|
||||||
|
|
||||||
|
//批量赋值黑名单
|
||||||
|
protected $guarded = ['created_at', 'updated_at'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the location that owns the vehicle.
|
* Get the location that owns the vehicle.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ class VehicleRu extends Model
|
|||||||
*/
|
*/
|
||||||
public $timestamps = false;
|
public $timestamps = false;
|
||||||
|
|
||||||
|
//批量赋值黑名单
|
||||||
|
protected $guarded = ['created_at', 'updated_at'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the location that owns the vehicle.
|
* Get the location that owns the vehicle.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\service;
|
||||||
|
|
||||||
|
use app\util\Xunfei;
|
||||||
|
|
||||||
|
class TranslatorService
|
||||||
|
{
|
||||||
|
private Xunfei $xunfei;
|
||||||
|
|
||||||
|
private $lang = ['zh'=>'cn', 'en'=>'en','ru'=>'ru','ar'=>'ar'];
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
$this->xunfei = new Xunfei();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function vehicle($vehicle, string $targetLang = 'en') {
|
||||||
|
$fields = ['brand', 'model', 'purchase_location', 'procedure_location', 'fbo_destination'];
|
||||||
|
foreach ($vehicle as $k => $var) {
|
||||||
|
if (in_array($k, $fields)) {
|
||||||
|
$rst_dst = $this->xunfei->fanyi($var, 'cn', $targetLang);
|
||||||
|
if ($rst_dst) {
|
||||||
|
$vehicle[$k] = $rst_dst["trans_result"]["dst"];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $vehicle;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function location($location, string $targetLang = 'en') {
|
||||||
|
$fields = ['name'];
|
||||||
|
foreach ($location as $k => $var) {
|
||||||
|
if (in_array($k, $fields)) {
|
||||||
|
$rst_dst = $this->xunfei->fanyi($var, 'cn', $targetLang);
|
||||||
|
if ($rst_dst) {
|
||||||
|
$location[$k] = $rst_dst["trans_result"]["dst"];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $location;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -62,9 +62,6 @@ class LocationController extends Crud
|
|||||||
*/
|
*/
|
||||||
public function insert(Request $request): Response
|
public function insert(Request $request): Response
|
||||||
{
|
{
|
||||||
if ($request->method() === 'POST') {
|
|
||||||
return parent::insert($request);
|
|
||||||
}
|
|
||||||
// 获取当前语言
|
// 获取当前语言
|
||||||
$lang = session('lang', 'zh-cn');
|
$lang = session('lang', 'zh-cn');
|
||||||
|
|
||||||
@@ -75,6 +72,48 @@ class LocationController extends Crud
|
|||||||
// 合并翻译
|
// 合并翻译
|
||||||
$translations = array_merge($location_translations, $common_translations);
|
$translations = array_merge($location_translations, $common_translations);
|
||||||
|
|
||||||
|
// 如果是 POST 请求,处理数据保存和翻译
|
||||||
|
if ($request->method() === 'POST') {
|
||||||
|
// 获取表单数据
|
||||||
|
$data = $request->all();
|
||||||
|
Log::info('location',$data);
|
||||||
|
|
||||||
|
// 保存主表数据
|
||||||
|
$location = new Location();
|
||||||
|
$location->fill($data);
|
||||||
|
$location->save();
|
||||||
|
|
||||||
|
// 处理多语言翻译
|
||||||
|
$languages = ['en', 'ar', 'ru'];
|
||||||
|
$translationModels = [
|
||||||
|
'en' => \app\model\LocationEn::class,
|
||||||
|
'ar' => \app\model\LocationAr::class,
|
||||||
|
'ru' => \app\model\LocationRu::class
|
||||||
|
];
|
||||||
|
|
||||||
|
// 创建翻译服务实例
|
||||||
|
$translatorService = new \app\service\TranslatorService();
|
||||||
|
|
||||||
|
foreach ($languages as $lang) {
|
||||||
|
$translationModel = $translationModels[$lang];
|
||||||
|
$translation = new $translationModel();
|
||||||
|
|
||||||
|
// 复制主表数据
|
||||||
|
$translation->fill($data);
|
||||||
|
$translation->id = $location->id;
|
||||||
|
|
||||||
|
// 使用翻译服务处理需要翻译的字段
|
||||||
|
$translatedData = $translatorService->location($data, $lang);
|
||||||
|
|
||||||
|
// 更新翻译后的字段
|
||||||
|
$translation->name = $translatedData['name'] ?? $data['name'];
|
||||||
|
// $translation->description = $translatedData['description'] ?? $data['description'];
|
||||||
|
|
||||||
|
Log::info('location:'.$lang.':translatedData',$translatedData);
|
||||||
|
$translation->save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return view('location/insert', [
|
return view('location/insert', [
|
||||||
'lang' => $lang,
|
'lang' => $lang,
|
||||||
'translations' => $translations
|
'translations' => $translations
|
||||||
|
|||||||
@@ -158,6 +158,54 @@ class VehicleController extends Crud
|
|||||||
$translations = array_merge($required_translations, $translations);
|
$translations = array_merge($required_translations, $translations);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果是 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return view('vehicle/insert', [
|
return view('vehicle/insert', [
|
||||||
'lang' => $lang,
|
'lang' => $lang,
|
||||||
'translations' => $translations
|
'translations' => $translations
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ class Location extends Base
|
|||||||
*/
|
*/
|
||||||
protected $primaryKey = 'id';
|
protected $primaryKey = 'id';
|
||||||
|
|
||||||
|
//批量赋值黑名单
|
||||||
|
protected $guarded = ['created_at', 'updated_at'];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,9 @@ class Vehicle extends Base
|
|||||||
*/
|
*/
|
||||||
protected $primaryKey = 'id';
|
protected $primaryKey = 'id';
|
||||||
|
|
||||||
|
//批量赋值黑名单
|
||||||
|
protected $guarded = ['created_at', 'updated_at'];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,23 +66,43 @@
|
|||||||
let $ = layui.jquery;
|
let $ = layui.jquery;
|
||||||
let popup = layui.popup;
|
let popup = layui.popup;
|
||||||
|
|
||||||
form.on('submit(save)', function(data) {
|
// form.on('submit(save)', function(data) {
|
||||||
$.ajax({
|
// console.log('submit');
|
||||||
|
// $.ajax({
|
||||||
|
// url: '/app/admin/location/insert',
|
||||||
|
// data: JSON.stringify(data.field),
|
||||||
|
// dataType: 'json',
|
||||||
|
// contentType: 'application/json',
|
||||||
|
// type: 'post',
|
||||||
|
// success: function(result) {
|
||||||
|
// console.log('操作成功',result);
|
||||||
|
// if (result.code) {
|
||||||
|
// popup.failure(result.msg);
|
||||||
|
// } else {
|
||||||
|
// popup.success("<?= $translations['operation_success'] ?? '操作成功' ?>", function() {
|
||||||
|
// parent.layer.close(parent.layer.getFrameIndex(window.name));
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
// return false;
|
||||||
|
// });
|
||||||
|
layui.form.on("submit(save)", function (data) {
|
||||||
|
layui.$.ajax({
|
||||||
url: '/app/admin/location/insert',
|
url: '/app/admin/location/insert',
|
||||||
data: JSON.stringify(data.field),
|
type: "POST",
|
||||||
dataType: 'json',
|
dateType: "json",
|
||||||
contentType: 'application/json',
|
data: data.field,
|
||||||
type: 'post',
|
success: function (res) {
|
||||||
success: function(result) {
|
if (res.code) {
|
||||||
if (result.code) {
|
return layui.popup.failure(res.msg);
|
||||||
popup.failure(result.msg);
|
}
|
||||||
} else {
|
return layui.popup.success("操作成功", function () {
|
||||||
popup.success("<?= $translations['operation_success'] ?? '操作成功' ?>", function() {
|
parent.refreshTable();
|
||||||
parent.layer.close(parent.layer.getFrameIndex(window.name));
|
parent.layer.close(parent.layer.getFrameIndex(window.name));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
})
|
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -76,18 +76,18 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="layui-form-item">
|
<div class="layui-form-item">
|
||||||
<label class="layui-form-label"><?= $translations['fbo_location'] ?? 'fbo目的地' ?></label>
|
<label class="layui-form-label required"><?= $translations['fbo_location'] ?? 'fbo目的地' ?></label>
|
||||||
<div class="layui-input-block">
|
<div class="layui-input-block">
|
||||||
<select name="fbo_locations_id" lay-filter="category" id="fbo_locations_id" required>
|
<select name="fbo_locations_id" lay-filter="category" required lay-verify="required" id="fbo_locations_id" required>
|
||||||
<option value=""><?= $translations['please_select'] ?? '请选择分类' ?></option>
|
<option value=""><?= $translations['please_select'] ?? '请选择分类' ?></option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="layui-form-item">
|
<div class="layui-form-item">
|
||||||
<label class="layui-form-label"><?= $translations['current_location'] ?? '车辆所在地' ?></label>
|
<label class="layui-form-label required"><?= $translations['current_location'] ?? '车辆所在地' ?></label>
|
||||||
<div class="layui-input-block">
|
<div class="layui-input-block">
|
||||||
<select name="locations_id" lay-filter="category" id="locations_id" required>
|
<select name="locations_id" lay-filter="category" required lay-verify="required" id="locations_id" required>
|
||||||
<option value=""><?= $translations['please_select'] ?? '请选择分类' ?></option>
|
<option value=""><?= $translations['please_select'] ?? '请选择分类' ?></option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user