forked from Stichoza/google-translate-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleTranslate.php
146 lines (127 loc) · 3.74 KB
/
GoogleTranslate.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Google Translate PHP class
*
* @author Levan Velijanashvili <[email protected]>
* @link http://stichoza.com/
* @version 1.3.0
* @access public
*/
class GoogleTranslate {
/**
* Last translation
* @var string
* @access private
*/
public $lastResult = "";
/**
* Language translating from
* @var string
* @access private
*/
private $langFrom;
/**
* Language translating to
* @var string
* @access private
*/
private $langTo;
/**
* Google Translate URL format
* @var string
* @access private
*/
private static $urlFormat = "http://translate.google.com/translate_a/t?client=t&text=%s&hl=en&sl=%s&tl=%s&ie=UTF-8&oe=UTF-8&multires=1&otf=1&pc=1&trs=1&ssel=3&tsel=6&sc=1";
/**
* Class constructor
*
* @param string $from Language translating from (Optional)
* @param string $to Language translating to (Optional)
* @access public
*/
public function __construct($from = "en", $to = "ka") {
$this->setLangFrom($from)->setLangTo($to);
}
/**
* Set language we are transleting from
*
* @param string $from Language code
* @return GoogleTranslate
* @access public
*/
public function setLangFrom($lang) {
$this->langFrom = $lang;
return $this;
}
/**
* Set language we are transleting to
*
* @param string $to Language code
* @return GoogleTranslate
* @access public
*/
public function setLangTo($lang) {
$this->langTo = $lang;
return $this;
}
/**
* Simplified curl method
* @param string $url URL
* @param array $params Parameter array
* @param boolean $cookieSet
* @return string
* @access public
*/
public static final function makeCurl($url, array $params = array(), $cookieSet = false) {
if (!$cookieSet) {
$cookie = tempnam(sys_get_temp_dir(), "CURLCOOKIE");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
// Clean up temporary file
unset($ch);
unlink($cookie);
return $output;
}
$queryString = http_build_query($params);
$curl = curl_init($url . "?" . $queryString);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curl);
return $output;
}
/**
* Translate text
*
* @param string $string Text to translate
* @return string/boolean Translated text
* @access public
*/
public function translate($string) {
return $this->lastResult = self::staticTranslate($string, $this->langFrom, $this->langTo);
}
/**
* Static method for translating text
*
* @param string $string Text to translate
* @param string $from Language code
* @param string $to Language code
* @return string/boolean Translated text
* @access public
*/
public static function staticTranslate($string, $from, $to) {
$url = sprintf(self::$urlFormat, rawurlencode($string), $from, $to);
$result = preg_replace('!,+!', ',', self::makeCurl($url)); // remove repeated commas (causing JSON syntax error)
$resultArray = json_decode($result, true);
$finalResult = "";
if (!empty($resultArray[0])) {
foreach ($resultArray[0] as $results) {
$finalResult .= $results[0];
}
return $finalResult;
}
return false;
}
}
?>