1 | <?php |
||
11 | class Translation |
||
12 | { |
||
13 | /** |
||
14 | * Youdao api url. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $api = 'http://fanyi.youdao.com/openapi.do?type=data&doctype=json&version=1.1&'; |
||
19 | |||
20 | /** |
||
21 | * ai.youdao.com api url |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $api_v2 = 'http://openapi.youdao.com/api?from=zh-CHS&to=EN&'; |
||
25 | |||
26 | /** |
||
27 | * @var Client |
||
28 | */ |
||
29 | protected $http; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $config = []; |
||
35 | |||
36 | /** |
||
37 | * Translation constructor. |
||
38 | * |
||
39 | * @param Client $http |
||
40 | * @param array $config |
||
41 | */ |
||
42 | 5 | public function __construct(Client $http, array $config = []) |
|
47 | |||
48 | /** |
||
49 | * @param $text |
||
50 | * |
||
51 | * @return mixed |
||
52 | */ |
||
53 | 3 | public function translate($text) |
|
57 | |||
58 | /** |
||
59 | * @param $text |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | 1 | public function translug($text) |
|
67 | |||
68 | /** |
||
69 | * @param $text |
||
70 | * |
||
71 | * @return mixed |
||
72 | */ |
||
73 | 4 | private function getTranslatedText($text) |
|
87 | |||
88 | /** |
||
89 | * @param array $translateResponse |
||
90 | * |
||
91 | * @return mixed |
||
92 | */ |
||
93 | private function getTranslation(array $translateResponse) |
||
101 | |||
102 | /** |
||
103 | * @param array $translateResponse |
||
104 | * |
||
105 | * @return mixed |
||
106 | */ |
||
107 | private function getTranslatedTextFromResponse(array $translateResponse) |
||
111 | |||
112 | /** |
||
113 | * @param $text |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | private function getTranslateUrl($text) |
||
123 | |||
124 | /** |
||
125 | * @param $text |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | private function getTranslateUrlV2($text) |
||
141 | |||
142 | /** |
||
143 | * @param $text |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | 4 | private function isEnglish($text) |
|
155 | |||
156 | /** |
||
157 | * Remove segment #. |
||
158 | * |
||
159 | * @param $text |
||
160 | * |
||
161 | * @return mixed |
||
162 | */ |
||
163 | private function removeSegment($text) |
||
167 | } |
||
168 |