创建时翻译
This commit is contained in:
@@ -62,9 +62,6 @@ class LocationController extends Crud
|
||||
*/
|
||||
public function insert(Request $request): Response
|
||||
{
|
||||
if ($request->method() === 'POST') {
|
||||
return parent::insert($request);
|
||||
}
|
||||
// 获取当前语言
|
||||
$lang = session('lang', 'zh-cn');
|
||||
|
||||
@@ -74,6 +71,48 @@ class LocationController extends Crud
|
||||
|
||||
// 合并翻译
|
||||
$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', [
|
||||
'lang' => $lang,
|
||||
|
||||
@@ -158,6 +158,54 @@ class VehicleController extends Crud
|
||||
$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', [
|
||||
'lang' => $lang,
|
||||
'translations' => $translations
|
||||
|
||||
@@ -30,7 +30,8 @@ class Location extends Base
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
|
||||
|
||||
//批量赋值黑名单
|
||||
protected $guarded = ['created_at', 'updated_at'];
|
||||
|
||||
}
|
||||
|
||||
@@ -39,6 +39,9 @@ class Vehicle extends Base
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
//批量赋值黑名单
|
||||
protected $guarded = ['created_at', 'updated_at'];
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -66,23 +66,43 @@
|
||||
let $ = layui.jquery;
|
||||
let popup = layui.popup;
|
||||
|
||||
form.on('submit(save)', function(data) {
|
||||
$.ajax({
|
||||
// form.on('submit(save)', function(data) {
|
||||
// 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',
|
||||
data: JSON.stringify(data.field),
|
||||
dataType: 'json',
|
||||
contentType: 'application/json',
|
||||
type: 'post',
|
||||
success: function(result) {
|
||||
if (result.code) {
|
||||
popup.failure(result.msg);
|
||||
} else {
|
||||
popup.success("<?= $translations['operation_success'] ?? '操作成功' ?>", function() {
|
||||
parent.layer.close(parent.layer.getFrameIndex(window.name));
|
||||
});
|
||||
type: "POST",
|
||||
dateType: "json",
|
||||
data: data.field,
|
||||
success: function (res) {
|
||||
if (res.code) {
|
||||
return layui.popup.failure(res.msg);
|
||||
}
|
||||
return layui.popup.success("操作成功", function () {
|
||||
parent.refreshTable();
|
||||
parent.layer.close(parent.layer.getFrameIndex(window.name));
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
return false;
|
||||
});
|
||||
})
|
||||
|
||||
@@ -76,18 +76,18 @@
|
||||
</div>
|
||||
|
||||
<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">
|
||||
<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>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<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">
|
||||
<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>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user