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