1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JellyBool\Translug; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use JellyBool\Translug\Exceptions\TranslationErrorException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Translation. |
10
|
|
|
*/ |
11
|
|
|
class Translation |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Youdao new API url. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $api = 'https://openapi.youdao.com/api?from=zh-CHS&to=EN&'; |
19
|
|
|
/** |
20
|
|
|
* @var Client |
21
|
|
|
*/ |
22
|
|
|
protected $http; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $config = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Translation constructor. |
31
|
|
|
* |
32
|
|
|
* @param Client $http |
33
|
|
|
* @param array $config |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Client $http, array $config = []) |
36
|
|
|
{ |
37
|
|
|
$this->http = $http; |
38
|
|
|
$this->config = $config; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $text |
43
|
|
|
* |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function translate($text) |
47
|
|
|
{ |
48
|
|
|
return $this->getTranslatedText($text); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $text |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function translug($text) |
57
|
|
|
{ |
58
|
|
|
return str_slug($this->getTranslatedText($text)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $text |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
private function getTranslatedText($text) |
67
|
|
|
{ |
68
|
|
|
if ($this->isEnglish($text)) { |
69
|
|
|
return $text; |
70
|
|
|
} |
71
|
|
|
$text = $this->removeSegment($text); |
72
|
|
|
$url = $this->getTranslateUrl($text); |
73
|
|
|
$response = $this->http->get($url); |
74
|
|
|
|
75
|
|
|
return $this->getTranslation(json_decode($response->getBody(), true)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $translateResponse |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
private function getTranslation(array $translateResponse) |
84
|
|
|
{ |
85
|
|
|
if ((int) $translateResponse['errorCode'] === 0) { |
86
|
|
|
return $this->getTranslatedTextFromResponse($translateResponse); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
throw new TranslationErrorException('Translate error, error_code : '.$translateResponse['errorCode'].'. Refer url: http://ai.youdao.com/docs/api.s'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $translateResponse |
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
private function getTranslatedTextFromResponse(array $translateResponse) |
98
|
|
|
{ |
99
|
|
|
return $translateResponse['translation'][0]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $text |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
private function getTranslateUrl($text) |
108
|
|
|
{ |
109
|
|
|
$salt = md5(time()); |
110
|
|
|
$query = [ |
111
|
|
|
'sign' => md5($this->config['appKey'].$text.$salt.$this->config['appSecret']), |
112
|
|
|
'appKey' => $this->config['appKey'], |
113
|
|
|
'salt' => $salt, |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
return $this->api.http_build_query($query).'&q='.urlencode($text); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $text |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
private function isEnglish($text) |
125
|
|
|
{ |
126
|
|
|
if (preg_match("/\p{Han}+/u", $text)) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Remove segment #. |
135
|
|
|
* |
136
|
|
|
* @param $text |
137
|
|
|
* |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
|
|
private function removeSegment($text) |
141
|
|
|
{ |
142
|
|
|
return str_replace('#', '', ltrim($text)); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|