TranslateLogic.php
17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
<?php
/**
* @remark :
* @name :TranslateLogic.php
* @author :lyh
* @method :post
* @time :2024/1/12 9:42
*/
namespace App\Http\Logic\Bside\Setting;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\RouteMap\RouteMap;
use App\Models\User\UserLog;
use App\Models\WebSetting\Translate as TranslateModel;
use App\Models\WebSetting\TranslateData;
use App\Models\WebSetting\WebLanguage;
use App\Helper\Translate;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class TranslateLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new TranslateModel();
$this->param = $this->requestAll;
}
/**
* @remark :
* @name :getTranslateList
* @author :lyh
* @method :post
* @time :2024/4/9 10:59
*/
public function getTranslateList(){
$data = [];
if($this->param['url'] == 'All' || $this->param['url'] == 'other_all_text'){
if($this->param['url'] == 'other_all_text'){
$this->param['language_id'] = 0;
}
$info = $this->model->with('translate_data')->where(['url'=>$this->param['url'],'language_id'=>$this->param['language_id'],'type'=>$this->param['type']])->first();
if(!empty($info) && !empty($info['translate_data'])){
$translateInfo = json_decode($info['translate_data']['data'],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
foreach ($translateInfo as $k => $v){
$data[] = [$k=>$v];
}
}
return $this->success($data);
}
// 需要校对路由
$url = $this->user['domain'] . (($this->param['url'] != '/') ? $this->param['url'] : '');
// 需要校对语种
$languageInfo = $this->getLanguage($this->param['language_id']);
// 原始校对内容
$param = $this->getRouteSource($this->param['url']);
$param['language_id'] = $this->param['language_id'];
$param['type'] = $this->param['type'];
$param['url'] = $this->param['url'];
$info = $this->model->with('translate_data')->where($param)->first();
//获取当前URl的所有文本内容
$text_array = $this->getUrlRead($url);
// 原始校对程序
$old_key = [];//key值组成数据
$data_read = json_decode($info && $info['translate_data'] ? $info['translate_data']['data'] : '',JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
if(!empty($data_read)){
foreach ($data_read as $k => $v){
$k = urldecode($k);
$old_key[] = $k;
$data[] = [$k => $v];
}
}
$arr2 = [];
foreach ($text_array as $val) {
if($val == ' ' || $val == ''){
continue;
}
$val = str_replace(' ','',urldecode($val));//处理特殊字符
$val = trim(str_replace(' ','',$val));
if (FALSE == in_array($val, $old_key)){
$arr2[] = $val;
}
}
if(!empty($arr2)){
$i = 0;
TranslateText:
if($languageInfo['short'] == 'zh-ct'){
$languageInfo['short'] = 'zh-TW';
}
$translate_list = Translate::tran($arr2, $languageInfo['short']);
if(empty($translate_list)){
if ($i < 3) {
$i++;
goto TranslateText;
}
$this->fail('翻译失败,请稍后重试!');
}
if(count($arr2) == 1){
$data[] = [
$arr2[0]=>$translate_list
];
}else{
foreach ($arr2 as $k => $v){
$data[] = [
$v=>$translate_list[$k]
];
}
}
}
return $this->success($data);
}
/**
* @remark :获取图片列表
* @name :imageList
* @author :lyh
* @method :post
* @time :2023/11/23 17:29
*/
public function getTranslateImageList(){
if($this->param['url'] == 'All'){
$info = $this->model->with('translate_data')->where(['url'=>$this->param['url'],'language_id'=>$this->param['language_id'],'type'=>$this->param['type']])->first();
$data = [];
if(!empty($info) && !empty($info['translate_data'])){
$translateInfo = json_decode($info['translate_data']['data'],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
foreach ($translateInfo as $k => $v){
$data[] = [$k=>$v];
}
}
return $this->success($data);
}
$url = $this->user['domain'].(($this->param['url'] != '/') ? $this->param['url'] : '');
$param = $this->getRouteSource($this->param['url']);
$param['language_id'] = $this->param['language_id'];
$param['type'] = $this->param['type'];
$info = $this->model->with('translate_data')->where($param)->first();
$data = [];
if(empty($info)){
$new_list = $this->getUrlImageRead($url);
foreach ($new_list as $v){
$data[] = [
$v=>$v,
];
}
return $this->success($data);
}
$new_list = $this->getUrlImageRead($url);
$old_list = [];
$data_read = json_decode($info && $info['translate_data'] ? $info['translate_data']['data'] : '',JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
foreach ($data_read as $k=>$v){
$old_list[] = $k;
$data[] = [
$k=>$v,
];
}
$arr2 = array_values(array_diff($new_list, $old_list));
if(!empty($arr2)){
foreach ($arr2 as $v1){
$data[] = [
$v1=>$v1
];
}
}
return $this->success($data);
}
/**
* @remark :获取语种信息
* @name :getLanguage
* @author :lyh
* @method :post
* @time :2024/1/12 9:45
*/
public function getLanguage($language_id){
//获取语种信息
$languageModel = new WebLanguage();
$languageInfo = $languageModel->read(['id'=>$language_id]);
if($languageInfo === false){
$this->fail('请选择语种');
}
return $this->success($languageInfo);
}
/**
* 获取url HTML需要翻译的文本
* @param $url
* @return array
*/
public function getUrlRead($url){
$dom = file_get_html($url);
$texts = $dom->find("text");
$description = $dom->find("meta[name=description]",0);
$keywords = $dom->find("meta[name=keywords]",0);
// 组装需要翻译的内容 HTML内文案、meta description、meta keywords
$need_tran = [];
foreach ($texts as $k=>$text) {
$tag= $text->parent()->tag;
if (in_array($tag, ['script', 'style', 'root'])){
continue;
}
$string = trim($text->text());
if (empty($string)){
continue;
}
$country_class = '';
if (method_exists($text->parent()->parent(),"find") && $text->parent()->parent()->find("b")) {
$country_class = $text->parent()->parent()->find("b",0)->class;
}
if(FALSE !== strpos($country_class, 'country-flag')) {
continue;
}
if(!in_array(htmlspecialchars_decode(html_entity_decode($string)),$need_tran)){
$need_tran[] = htmlspecialchars_decode(html_entity_decode($string));
}
}
$need_tran[] = $description ? $description->attr['content'] : '';
$need_tran[] = $keywords ? $keywords->attr['content'] : '';
$need_tran = array_values(array_unique($need_tran));
return $need_tran;
}
/**
* @remark :获取Url内容
* @name :getUrlRead
* @author :lyh
* @method :post
* @time :2023/11/22 10:02
*/
public function getUrlImageRead($url){
$contextOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
];
$pattern = '/<img.*?src="(.*?)".*?>/i';
$matches = array();
$context = stream_context_create($contextOptions);
$sourceCode = file_get_contents($url, false, $context);
preg_match_all($pattern, $sourceCode, $matches);
$textContentArray = $matches[1];
$data = [];
foreach ($textContentArray as $v){
if(!empty($v)){
$data[] = $v;
}
}
$uniqueArray = array_unique($data);
$data = array_values($uniqueArray);
return $data;
}
/**
* @name :(新增/更新多语言)save
* @author :lyh
* @method :post
* @time :2023/6/12 10:52
*/
public function translateSave(){
$data = [];
if(!empty($this->param['data'])){
//处理传递的data
foreach ($this->param['data'] as $k => $v){
if(!empty($v) && is_array($v)){
foreach ($v as $text => $translate){
$text = str_replace(['%22', '%', '+'], ['"', '%', '+'], $text);
$translate = str_replace(['%22', '%', '+'], ['"', '%', '+'], $translate);
$data[$text] = $translate;
}
}
}
$this->param['data'] = $data;
}
//解析路由
$sendData = $this->handleRoute($this->param['url']);
if($this->param['url'] == 'other_all_text'){
$this->param['language_id'] = 0;
}
DB::beginTransaction();
try {
$info = $this->model->read(['language_id'=>$this->param['language_id'],'url'=>$this->param['url'],'project_id'=>$this->user['project_id'],'type'=>$this->param['type']]);
if($info === false){
$sourceInfo = $this->getRouteSource($sendData['new_route']);
$param = [
'type'=>$this->param['type'] ?? 1,
'project_id'=>$this->user['project_id'],
'url'=>str_replace_url($this->param['url']),
'language_id'=>$this->param['language_id'],
'alias'=>$this->param['alias'],
'source'=>$sourceInfo['source'],
'source_id'=>$sourceInfo['source_id'],
'is_list'=>$sourceInfo['is_list'],
'is_custom'=>$sourceInfo['is_custom'],
'page'=>$sendData['page'] ?? 0
];
$id = $this->model->addReturnId($param);
TranslateData::insert(['trans_id'=>$id,'data'=>json_encode($data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)]);
}else{
TranslateData::where(['trans_id'=>$info['id']])->update(['data'=>json_encode($data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)]);
}
//写日志
$userLogModel = new UserLog();
$userLogModel->add(['model'=>'translate/save','remark'=>json_encode($this->param,true),'type'=>0,'operator_id'=>$this->user['id'],'project_id'=>$this->user['project_id']]);
DB::commit();
}catch (\Exception $e){
DB::rollback();
$this->fail('系统错误请联系管理员');
}
$this->sendMessage($sendData);
return $this->success();
}
/**
* @remark :根据路由获取source+source_id
* @name :getRouteSource
* @author :lyh
* @method :post
* @time :2024/5/17 15:11
*/
public function getRouteSource($route){
$routes = $route;
$data = ['source'=>0,'source_id'=>0,'is_list'=>0,'is_custom'=>0,'page'=>0];
if(strtolower($route) == 'all'){
return $this->success($data);
}
if($route == 'index' || $route == '/'){
$data['source'] = 1;
return $this->success($data);
}
$route = basename($route);
$page = 0;
if (is_numeric($route)) {
$arr = explode('/',$routes);
$page = $route;
$route = $arr[0];
}
$routeModel = new RouteMap();
$routeInfo = $routeModel->read(['route'=>$route]);
if($routeInfo === false){
if(in_array($route,['products','news','blog'])){
//固定路由
$data['page'] = $page;
$data['is_list'] = 1;
if($route == 'products'){
$data['source'] = 2;
}elseif ($route == 'news'){
$data['source'] = 4;
}else{
$data['source'] = 3;
}
return $this->success($data);
}
return $data;
}
$data = $this->resultData($routeInfo,$data);
$data['page'] = $page;
return $data;
}
/**
* @remark :返回数据
* @name :resultData
* @author :lyh
* @method :post
* @time :2024/5/20 11:54
*/
public function resultData($routeInfo,$data){
if($routeInfo['source'] == RouteMap::SOURCE_PAGE){
if($routeInfo['source_id']){
$data = ['source'=>9,'source_id'=>$routeInfo['source_id'],'is_list'=>0,'is_custom'=>0];
}
}
if($routeInfo['source'] == RouteMap::SOURCE_PRODUCT){
if($routeInfo['source_id']){
$data = ['source'=>2,'source_id'=>$routeInfo['source_id'],'is_list'=>0,'is_custom'=>0];
}
}
if($routeInfo['source'] == RouteMap::SOURCE_PRODUCT_CATE){
if($routeInfo['source_id']){
$data = ['source'=>2,'source_id'=>$routeInfo['source_id'],'is_list'=>1,'is_custom'=>0];
}
}
if($routeInfo['source'] == RouteMap::SOURCE_BLOG){
if($routeInfo['source_id']){
$data = ['source'=>3,'source_id'=>$routeInfo['source_id'],'is_list'=>0,'is_custom'=>0];
}
}
if($routeInfo['source'] == RouteMap::SOURCE_BLOG_CATE){
if($routeInfo['source_id']){
$data = ['source'=>3,'source_id'=>$routeInfo['source_id'],'is_list'=>1,'is_custom'=>0];
}
}
if($routeInfo['source'] == RouteMap::SOURCE_NEWS){
if($routeInfo['source_id']){
$data = ['source'=>4,'source_id'=>$routeInfo['source_id'],'is_list'=>0,'is_custom'=>0];
}
}
if($routeInfo['source'] == RouteMap::SOURCE_NEWS_CATE){
if($routeInfo['source_id']){
$data = ['source'=>4,'source_id'=>$routeInfo['source_id'],'is_list'=>1,'is_custom'=>0];
}
}
if($routeInfo['source'] == RouteMap::SOURCE_PRODUCT_KEYWORD){
if($routeInfo['source_id']){
$data = ['source'=>8,'source_id'=>$routeInfo['source_id'],'is_list'=>0,'is_custom'=>0];
}
}
if($routeInfo['source'] == RouteMap::SOURCE_MODULE){
if($routeInfo['source_id']){
$data = ['source'=>7,'source_id'=>$routeInfo['source_id'],'is_list'=>0,'is_custom'=>1];
}
}
if($routeInfo['source'] == RouteMap::SOURCE_MODULE_CATE){
if($routeInfo['source_id']){
$data = ['source'=>7,'source_id'=>$routeInfo['source_id'],'is_list'=>1,'is_custom'=>1];
}
}
return $data;
}
/**
* @remark :处理路由
* @name :handleRoute
* @author :lyh
* @method :post
* @time :2024/1/18 17:25
*/
public function handleRoute($url){
$lang = $this->getLanguage($this->param['language_id'])['short'];
$str = trim($url,'/');
$page = 0;
$route = 'index';
if(!empty($str)){
$arr = explode('/',$str);
$num = count($arr);
if($num == 1){
$route = $arr[0];
}elseif ($num == 2){
if(ctype_digit($arr[1])){//是数字的情况
$page = $arr[1];
$route = $arr[0];
}else{
$route = $arr[1];
}
}elseif($num == 3){
if(ctype_digit($arr[2])){//是数字的情况
$page = $arr[2];
$route = $arr[0];
}else{
if($arr[2] == 'page'){
$route = $arr[1];
}else{
$route = $arr[0];
}
}
}elseif ($num == 4){
if(ctype_digit($arr[3])){//是数字的情况
$page = $arr[3];
$route = $arr[1];
}
}
}
if($page != 0){
$data['page'] = $page;
}
$data['new_route'] = $route;
$data['lang'] = $lang;
$data['project_id']= $this->user['project_id'];
return $this->success($data);
}
/**
* @remark :发送通知
* @name :send
* @author :lyh
* @method :post
* @time :2024/5/21 11:13
*/
public function sendMessage($data){
return $this->curlDelRoute($data);
}
}