|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Mirza Yandex Translator For Laravel |
|
4
|
|
|
* Mirza makes it easy to translate and manipulate text using the Yandex.Translate API. |
|
5
|
|
|
* |
|
6
|
|
|
* @version 1.0.0 |
|
7
|
|
|
* |
|
8
|
|
|
* @author yak0d3 <[email protected]> |
|
9
|
|
|
* @license MIT |
|
10
|
|
|
* |
|
11
|
|
|
* @link https://github.com/yak0d3/Mirza_Yandex_Translator |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2018 Yak0d3 |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace yak0d3\mirza_yandex_translator; |
|
17
|
|
|
|
|
18
|
|
|
use Exception; |
|
19
|
|
|
use yak0d3\mirza_yandex_translator\MirzaClient as MirzaClient; |
|
20
|
|
|
|
|
21
|
|
|
class Mirza |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The variable that will contain the MirzaClient instance. |
|
25
|
|
|
* |
|
26
|
|
|
* @var MirzaClient |
|
27
|
|
|
*/ |
|
28
|
|
|
private $client; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Construct. |
|
32
|
|
|
* |
|
33
|
|
|
* @param MirzaClient $MirzaClient |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(MirzaClient $MirzaClient) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->client = $MirzaClient; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Translates string to the given language. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $text |
|
44
|
|
|
* @param string $lang |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function translate(string $text, string $lang) |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->client->translate($text, $lang); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Translates an array of text to the given language |
|
55
|
|
|
* The array can be associative or sequential |
|
56
|
|
|
* If the array is associative, an array with the same index names will be returned in a json encoded string |
|
57
|
|
|
* If the $assoc param is set to `true` and the given array is sequential an exception will be thrown. |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $textArray |
|
60
|
|
|
* @param string $lang |
|
61
|
|
|
* @param bool $assoc |
|
62
|
|
|
* |
|
63
|
|
|
* @throws Exception if the target language is not supported |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function translateArray(array $textArray, string $lang, $assoc = false) |
|
68
|
|
|
{ |
|
69
|
|
|
if (!$this->isSupportedLang($lang)) { |
|
70
|
|
|
throw new Exception('The target language is not supported. Run getSupportedLanguages() to get the list of supported languages.'); |
|
71
|
|
|
} |
|
72
|
|
|
$keys = array_keys($textArray); |
|
|
|
|
|
|
73
|
|
|
$translated = []; |
|
74
|
|
|
|
|
75
|
|
|
$index = 0; |
|
76
|
|
|
if ($assoc) { |
|
77
|
|
|
$this->isAssoc($textArray); |
|
78
|
|
|
} |
|
79
|
|
|
foreach ($textArray as $key => $text) { |
|
80
|
|
|
$translated[$key] = [ |
|
81
|
|
|
'originalText' => $text, |
|
82
|
|
|
'translatedText' => $this->client->translate($text, $lang), |
|
83
|
|
|
]; |
|
84
|
|
|
$index++; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return json_encode($translated); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Translate a string to multiple languages. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $text |
|
94
|
|
|
* @param array $langs |
|
95
|
|
|
* |
|
96
|
|
|
* @throws Exception if one or more target languages are not supported |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
public function translateTo(string $text, array $langs) |
|
101
|
|
|
{ |
|
102
|
|
|
$notSupported = []; |
|
103
|
|
|
foreach ($langs as $lang) { |
|
104
|
|
|
if (!$this->isSupportedLang($lang)) { |
|
105
|
|
|
array_push($notSupported, $lang); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
if (count($notSupported) > 0) { |
|
109
|
|
|
throw new Exception('The following languages are not supported: '.implode("\n", $notSupported)); |
|
110
|
|
|
} |
|
111
|
|
|
$textLang = $this->detectLanguage($text); |
|
112
|
|
|
$translatedText = ['originalText' => $text, 'originalLanguage' => $textLang, 'text' => []]; |
|
113
|
|
|
foreach ($langs as $lang) { |
|
114
|
|
|
try { |
|
115
|
|
|
$translatedText['text'][$lang] = $this->client->translate($text, $lang); |
|
116
|
|
|
} catch (Exception $e) { |
|
117
|
|
|
$translatedText['text'][$lang] = ''; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
unset($translatedText['text'][$textLang]); |
|
121
|
|
|
|
|
122
|
|
|
return json_encode($translatedText); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Detects the language of the provided string |
|
127
|
|
|
* An exception will be thrown if the language could not be found. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $text |
|
130
|
|
|
* @param bool $langName |
|
131
|
|
|
* |
|
132
|
|
|
* @throws Exception if the language name is not found |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function detectLanguage(string $text, $langName = false) |
|
137
|
|
|
{ |
|
138
|
|
|
$langCode = $this->client->detectLanguage($text); |
|
139
|
|
|
if ($langName) { |
|
140
|
|
|
$allLanguages = $this->client->getLanguages(); |
|
141
|
|
|
|
|
142
|
|
|
if (array_key_exists($langCode, $allLanguages)) { |
|
|
|
|
|
|
143
|
|
|
return $allLanguages->$langCode; |
|
144
|
|
|
} else { |
|
145
|
|
|
throw new Exception('Language name could not be found.'); |
|
146
|
|
|
} |
|
147
|
|
|
} else { |
|
148
|
|
|
return $langCode; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Returns the list of all supported languages. |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getSupportedLanguages() |
|
158
|
|
|
{ |
|
159
|
|
|
return json_encode($this->client->getLanguages()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Translates a string to all supported languages |
|
164
|
|
|
* This may take some time and cause a timeout exception. |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $text |
|
167
|
|
|
* |
|
168
|
|
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
public function translateToAll(string $text) |
|
171
|
|
|
{ |
|
172
|
|
|
$langs = $this->client->getLanguages(true); |
|
173
|
|
|
$textLang = $this->detectLanguage($text); |
|
174
|
|
|
$translatedText = ['originalText' => $text, 'originalLanguage' => $textLang, 'text' => []]; |
|
175
|
|
|
unset($langs[array_search($textLang, $langs)]); |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
foreach ($langs as $lang) { |
|
|
|
|
|
|
178
|
|
|
try { |
|
179
|
|
|
$translatedText['text'][$lang] = $this->client->translate($text, $lang); |
|
180
|
|
|
} catch (Exception $e) { |
|
181
|
|
|
$translatedText['text'][$lang] = ''; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return json_encode($translatedText); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Generates the "Powered by Yandex.Translate" link. |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $color |
|
192
|
|
|
* @param string $fontsize |
|
193
|
|
|
* |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
|
|
public function yandex_rights(string $color = '#fff', string $fontsize = '14px') |
|
197
|
|
|
{ |
|
198
|
|
|
$copyrights = 'Powered by Yandex.Translate'; |
|
199
|
|
|
|
|
200
|
|
|
return "<a href='https://translate.yandex.com/' target='_blank' style='font-size:$fontsize;color:$color;'>".$copyrights.'</a>'; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Generates an HTML `<select>` with the list of all supported languages. |
|
205
|
|
|
* |
|
206
|
|
|
* @return string |
|
207
|
|
|
*/ |
|
208
|
|
|
public function languages_select() |
|
209
|
|
|
{ |
|
210
|
|
|
$select = '<select>'; |
|
211
|
|
|
$option = '<option value="_lang_code_">_lang_name_</option>'; |
|
212
|
|
|
foreach ($this->client->supportedLanguages as $langCode => $langName) { |
|
|
|
|
|
|
213
|
|
|
$optionTemp = str_replace('_lang_code_', $langCode, $option); |
|
214
|
|
|
$optionTemp = str_replace('_lang_name_', $langName, $optionTemp); |
|
215
|
|
|
$select .= $optionTemp; |
|
216
|
|
|
} |
|
217
|
|
|
$select .= '</select>'; |
|
218
|
|
|
|
|
219
|
|
|
return $select; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Checks if the array given is associative. |
|
224
|
|
|
* |
|
225
|
|
|
* @param array $array |
|
226
|
|
|
* |
|
227
|
|
|
* @throws Exception if the array given is not associative |
|
228
|
|
|
* |
|
229
|
|
|
* @return boolean |
|
230
|
|
|
*/ |
|
231
|
|
|
private function isAssoc(array $array) |
|
232
|
|
|
{ |
|
233
|
|
|
if ([] == $array || array_keys($array) === range(0, count($array) - 1)) { |
|
234
|
|
|
throw new Exception('Argument 1 given to translateArray is a sequential array, an associative array is expected.'); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return true; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Checks if the language given is supported. |
|
242
|
|
|
* |
|
243
|
|
|
* @param string $lang |
|
244
|
|
|
* |
|
245
|
|
|
* @return boolean |
|
246
|
|
|
*/ |
|
247
|
|
|
private function isSupportedLang(string $lang) |
|
248
|
|
|
{ |
|
249
|
|
|
if (!in_array($lang, json_decode(json_encode($this->client->supportedLanguages), true))) { |
|
250
|
|
|
return false; |
|
251
|
|
|
} else { |
|
252
|
|
|
return true; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|