<?php
class Google_Translate_API {
function translate($text, $from = '', $to = 'en') {
$url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0'.''.'&q='.rawurlencode($text).'&langpair='.rawurlencode($from.'|'.$to);
$response = file_get_contents(
$url,
null,
stream_context_create(
array(
'http'=>array(
'method'=>"GET",
'header'=>"Referer: http://".$_SERVER['HTTP_HOST']."/\r\n"
)
)
)
);
if (preg_match("/{\"translatedText\":\"([^\"]+)\"/i", $response, $matches)) {
return self::_unescapeUTF8EscapeSeq($matches[1]);
}
return false;
}
function _unescapeUTF8EscapeSeq($str) {
return preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return html_entity_decode(\'\'.$matches[1].\';\', ENT_NOQUOTES, \'UTF-8\');'), $str);
}
}
// kullanımı..
$text = $_GET['q'];
$lf = $_GET['from'];
$lt = $_GET['to'];
if($text != ''){
$trans_text = Google_Translate_API::translate($text, $lf, $lt);
if ($trans_text !== false) {
echo $trans_text;
}
}
?>
şu şekilde kullanıcaz..
(tugrulalpdogan.com/cevir.php?q=apple&from=en&to=tr)
sayfada görünen değer "elma" olacaktır..
0 yorum:
Yorum Gönder