Conditions | 3 |
Paths | 3 |
Total Lines | 24 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
23 | public function translate(array $texts): array |
||
24 | { |
||
25 | $newTexts = []; |
||
26 | |||
27 | foreach (array_chunk($texts, $this->chunkSize) as $strings) { |
||
28 | |||
29 | $guzzleClient = new Client(['headers' => ['content-type' => 'application/x-www-form-urlencoded']]); |
||
30 | $options = [ |
||
31 | 'form_params' => [ |
||
32 | 'sl' => $this->from, |
||
33 | 'tl' => $this->to, |
||
34 | 'q' => implode('|', $strings), |
||
35 | ] |
||
36 | |||
37 | ]; |
||
38 | $request = $guzzleClient->request('POST', 'https://clients5.google.com/translate_a/t?client=dict-chrome-ex', $options); |
||
39 | |||
40 | $response = json_decode((string) $request->getBody(), true); |
||
41 | |||
42 | if ($request->getStatusCode() === 200) { |
||
43 | $newTexts = array_merge($newTexts, array_map('trim', explode('|', $response['sentences'][0]['trans']))); |
||
44 | } |
||
45 | } |
||
46 | return array_combine($texts, $newTexts); |
||
47 | } |
||
49 |