1 | <?php |
||
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 | 5 | public function __construct(Client $http, array $config = []) |
|
36 | { |
||
37 | 5 | $this->http = $http; |
|
38 | 5 | $this->config = $config; |
|
39 | 5 | } |
|
40 | |||
41 | /** |
||
42 | * @param $text |
||
43 | * |
||
44 | * @return mixed |
||
45 | */ |
||
46 | 3 | public function translate($text) |
|
47 | { |
||
48 | 3 | return $this->getTranslatedText($text); |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param $text |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | 1 | public function translug($text) |
|
57 | { |
||
58 | 1 | return str_slug($this->getTranslatedText($text)); |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param $text |
||
63 | * |
||
64 | * @return mixed |
||
65 | */ |
||
66 | 4 | private function getTranslatedText($text) |
|
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 | throw new TranslationErrorException('Translate error, error_code : '.$translateResponse['errorCode'].'. Refer url: http://ai.youdao.com/docs/api.s'); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param array $translateResponse |
||
93 | * |
||
94 | * @return mixed |
||
95 | */ |
||
96 | private function getTranslatedTextFromResponse(array $translateResponse) |
||
97 | { |
||
98 | return $translateResponse['translation'][0]; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param $text |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | private function getTranslateUrl($text) |
||
107 | { |
||
108 | $salt = md5(time()); |
||
109 | $query = [ |
||
110 | 'sign' => md5($this->config['appKey'].$text.$salt.$this->config['appSecret']), |
||
111 | 'appKey' => $this->config['appKey'], |
||
112 | 'salt' => $salt, |
||
113 | ]; |
||
114 | |||
115 | return $this->api.http_build_query($query).'&q='.urlencode($text); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param $text |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | 4 | private function isEnglish($text) |
|
131 | |||
132 | /** |
||
133 | * Remove segment #. |
||
134 | * |
||
135 | * @param $text |
||
136 | * |
||
137 | * @return mixed |
||
138 | */ |
||
139 | private function removeSegment($text) |
||
143 | } |
||
144 |