1 | <?php |
||
24 | class Translator |
||
25 | { |
||
26 | /** |
||
27 | * @var \GuzzleHttp\Client HTTP Client |
||
28 | */ |
||
29 | protected $httpClient; |
||
30 | |||
31 | /** |
||
32 | * @var string Source language - from where the string should be translated |
||
33 | */ |
||
34 | protected $sourceLanguage; |
||
35 | |||
36 | /** |
||
37 | * @var string Target language - to which language string should be translated |
||
38 | */ |
||
39 | protected $targetLanguage; |
||
40 | |||
41 | /** |
||
42 | * @var string|bool Last detected source language |
||
43 | */ |
||
44 | protected $lastDetectedSource = false; |
||
45 | |||
46 | /** |
||
47 | * @var string Google Translate URL base |
||
48 | */ |
||
49 | private $urlBase = 'http://translate.google.com/translate_a/single'; |
||
50 | |||
51 | /** |
||
52 | * @var array Dynamic guzzleHTTP client options |
||
53 | */ |
||
54 | protected $httpOptions = []; |
||
55 | |||
56 | /** |
||
57 | * @var array URL Parameters |
||
58 | */ |
||
59 | protected $urlParams = [ |
||
60 | 'client' => 't', |
||
61 | 'hl' => 'en', |
||
62 | 'dt' => 't', |
||
63 | 'sl' => null, // Source language |
||
64 | 'tl' => null, // Target language |
||
65 | 'q' => null, // String to translate |
||
66 | 'ie' => 'UTF-8', // Input encoding |
||
67 | 'oe' => 'UTF-8', // Output encoding |
||
68 | 'multires' => 1, |
||
69 | 'otf' => 0, |
||
70 | 'pc' => 1, |
||
71 | 'trs' => 1, |
||
72 | 'ssel' => 0, |
||
73 | 'tsel' => 0, |
||
74 | 'kc' => 1, |
||
75 | 'tk' => null, |
||
76 | ]; |
||
77 | |||
78 | /** |
||
79 | * @var array Regex key-value patterns to replace on response data |
||
80 | */ |
||
81 | protected $resultRegexes = [ |
||
82 | '/,+/' => ',', |
||
83 | '/\[,/' => '[', |
||
84 | ]; |
||
85 | |||
86 | /** |
||
87 | * @var TokenProviderInterface |
||
88 | */ |
||
89 | protected $tokenProvider; |
||
90 | |||
91 | /** |
||
92 | * Class constructor. |
||
93 | * |
||
94 | * For more information about HTTP client configuration options, visit |
||
95 | * "Creating a client" section of GuzzleHttp docs. |
||
96 | * 5.x - http://guzzle.readthedocs.org/en/5.3/clients.html#creating-a-client |
||
97 | * |
||
98 | * @param string $source Source language (Optional) |
||
99 | * @param string $target Target language (Optional) |
||
100 | * @param array $options Associative array of http client configuration options (Optional) |
||
101 | * |
||
102 | * @throws Exception If token provider does not implement TokenProviderInterface |
||
103 | */ |
||
104 | public function __construct($source = null, $target = 'en', $options = [], TokenProviderInterface $tokenProvider = null) |
||
115 | |||
116 | /** |
||
117 | * Set source language we are transleting from. |
||
118 | * |
||
119 | * @param string $source Language code |
||
120 | * |
||
121 | * @return Translator |
||
122 | */ |
||
123 | public function setSource($source = null) |
||
129 | |||
130 | /** |
||
131 | * Set translation language we are translating to. |
||
132 | * |
||
133 | * @param string $target Language code |
||
134 | * |
||
135 | * @return Translator |
||
136 | */ |
||
137 | public function setTarget($target) |
||
143 | |||
144 | /** |
||
145 | * Set guzzleHttp client options. |
||
146 | * |
||
147 | * @param array $options guzzleHttp client options. |
||
148 | * |
||
149 | * @return Translator |
||
150 | */ |
||
151 | public function setHttpOption(array $options) |
||
157 | |||
158 | /** |
||
159 | * Get response array. |
||
160 | * |
||
161 | * @param string|array $data String or array of strings to translate |
||
162 | * |
||
163 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
164 | * @throws ErrorException If the HTTP request fails |
||
165 | * @throws UnexpectedValueException If received data cannot be decoded |
||
166 | * |
||
167 | * @return array Response |
||
168 | */ |
||
169 | public function getResponse($data) |
||
212 | |||
213 | /** |
||
214 | * Translate text. |
||
215 | * |
||
216 | * This can be called from instance method translate() using __call() magic method. |
||
217 | * Use $instance->translate($string) instead. |
||
218 | * |
||
219 | * @param string|array $data Text or array of texts to translate |
||
220 | * |
||
221 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
222 | * @throws ErrorException If the HTTP request fails |
||
223 | * @throws UnexpectedValueException If received data cannot be decoded |
||
224 | * |
||
225 | * @return string|bool Translated text |
||
226 | */ |
||
227 | public function translate($data) |
||
241 | |||
242 | /** |
||
243 | * Translate text. |
||
244 | * |
||
245 | * This can be called from instance method translate() using __call() magic method. |
||
246 | * Use $instance->translate($string) instead. |
||
247 | * |
||
248 | * @param string $data Text or array of texts to translate |
||
|
|||
249 | * |
||
250 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
251 | * @throws ErrorException If the HTTP request fails |
||
252 | * @throws UnexpectedValueException If received data cannot be decoded |
||
253 | * |
||
254 | * @return string|bool Translated text |
||
255 | */ |
||
256 | protected function translateText($string) |
||
324 | |||
325 | /** |
||
326 | * Get last detected language. |
||
327 | * |
||
328 | * @return string|bool Last detected language or boolean FALSE |
||
329 | */ |
||
330 | public function getLastDetectedSource() |
||
334 | |||
335 | /** |
||
336 | * Check if given locale is valid. |
||
337 | * |
||
338 | * @param string $lang Language code to verify |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | public function isValidLocale($lang) |
||
346 | } |
||
347 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.