php class GoogleTranslate static function translate text source dest t

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
class GoogleTranslate {
static function translate($text, $source, $dest) {
$text=urlencode($text);
$ch=curl_init('http://translate.google.ru/translate_t');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS, "client=t&text=$text&sl=$source&tl=$dest");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.3) Gecko/2008092417');
$r=curl_exec($ch);
$arr=array();
preg_match('|<div id=result_box dir="ltr">(.*)</div>|sU', $r, $arr);
return strip_tags(str_replace('<br>', "\n", $arr[1]));
}
}
echo GoogleTranslate::translate('Read this fucking manual', 'eng', 'ru');
?>