-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
AbstractGoogleTranslateDirectiveResolver.php
executable file
·121 lines (109 loc) · 4 KB
/
AbstractGoogleTranslateDirectiveResolver.php
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
<?php
declare(strict_types=1);
namespace PoPSchema\GoogleTranslateDirective\DirectiveResolvers;
use PoP\ComponentModel\ErrorHandling\Error;
use PoPSchema\GoogleTranslateDirective\Environment;
use PoP\Translation\Facades\TranslationAPIFacade;
use PoPSchema\TranslateDirective\DirectiveResolvers\AbstractTranslateDirectiveResolver;
abstract class AbstractGoogleTranslateDirectiveResolver extends AbstractTranslateDirectiveResolver
{
const PROVIDERS_GOOGLE = 'google';
/**
* The name of the API's provider
*
* @return array
*/
public function getProvidersToResolve(): array
{
return [
self::PROVIDERS_GOOGLE,
];
}
protected function getClientFailureMessage(Error $error, string $provider): string
{
// Remove the API key from being printed
return str_replace(
$this->getEndpoint($provider),
$this->getEndpointURL($provider),
parent::getClientFailureMessage($error, $provider)
);
}
protected function getAPIKey(string $provider)
{
switch ($provider) {
case self::PROVIDERS_GOOGLE:
if ($apiKey = Environment::getGoogleTranslateAPIKey()) {
return $apiKey;
}
break;
}
return null;
}
protected function getEndpointURL(string $provider): ?string
{
switch ($provider) {
case self::PROVIDERS_GOOGLE:
return 'https://translation.googleapis.com/language/translate/v2';
}
return null;
}
protected function getEndpoint(string $provider): ?string
{
switch ($provider) {
case self::PROVIDERS_GOOGLE:
$endpointURL = $this->getEndpointURL($provider);
$apiKey = $this->getAPIKey($provider);
if ($endpointURL && $apiKey) {
return sprintf(
'%s?key=%s',
$endpointURL,
$apiKey
);
}
break;
}
return null;
}
protected function getQuery(string $provider, string $sourceLang, string $targetLang, array $contents): array
{
switch ($provider) {
case self::PROVIDERS_GOOGLE:
return [
'format' => 'text',
'source' => $sourceLang,
'target' => $targetLang,
'q' => $contents,
];
}
return parent::getQuery($provider, $sourceLang, $targetLang, $contents);
}
protected function getErrorMessageFromResponse(string $provider, array $response): ?string
{
switch ($provider) {
case self::PROVIDERS_GOOGLE:
// An array with the translations is found under data/translations
if (!$response['data']) {
$translationAPI = TranslationAPIFacade::getInstance();
return $translationAPI->__('There was an unidentified error when fetching data from the Google Translate API', 'google-translate-directive');
}
}
return parent::getErrorMessageFromResponse($provider, $response);
}
protected function extractTranslationsFromResponse(string $provider, array $response): array
{
switch ($provider) {
case self::PROVIDERS_GOOGLE:
// An array with the translations is found under data/translations
$translations = $response['data']['translations'];
// Each response comes after key "translatedText". Remove it from there
// Ref: https://cloud.google.com/translate/docs/reference/rest/v2/translate
return array_map(
function ($entry) {
return $entry['translatedText'];
},
$translations
);
}
return parent::extractTranslationsFromResponse($provider, $response);
}
}