Posted on 12-02-2010
Filed Under (css) by Elfin Ruler

class Google_Translate_API {

/**
* Translate a piece of text with the Google Translate API
* @return String
* @param $text String
* @param $from String[optional] Original language of $text. An empty String will let google decide the language of origin
* @param $to String[optional] Language to translate $text to
*/
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”
)
)
)
);*/
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST      ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS    ,”");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
curl_setopt($ch, CURLOPT_HEADER      ,0);  // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,0);  // RETURN THE CONTENTS OF THE CALL
curl_setopt($ch,CURLOPT_RETURNTRANSFER,    1);
$response = curl_exec($ch);
//var_dump($Rec_Data);
ob_start();

curl_close($ch);
if (preg_match(“/{\”translatedText\”:\”([^\"]+)\”/i”, $response, $matches)) {
return self::_unescapeUTF8EscapeSeq($matches[1]);
}
return false;
}

/**
* Convert UTF-8 Escape sequences in a string to UTF-8 Bytes. Old version.
* @return UTF-8 String
* @param $str String
*/
function __unescapeUTF8EscapeSeq($str) {
return preg_replace_callback(“/\\\u([0-9a-f]{4})/i”, create_function(‘$matches’, ‘return html_entity_decode(\’&#x\’.$matches[1].\’;\’, ENT_NOQUOTES, \’UTF-8\’);’), $str);
}

/**
* Convert UTF-8 Escape sequences in a string to UTF-8 Bytes
* @return UTF-8 String
* @param $str String
*/
function _unescapeUTF8EscapeSeq($str) {
return preg_replace_callback(“/\\\u([0-9a-f]{4})/i”, create_function(‘$matches’, ‘return Google_Translate_API::_bin2utf8(hexdec($matches[1]));’), $str);
}

/**
* Convert binary character code to UTF-8 byte sequence
* @return String
* @param $bin Mixed Interger or Hex code of character
*/
function _bin2utf8($bin) {
if ($bin <= 0x7F) {
return chr($bin);
} else if ($bin >= 0×80 && $bin <= 0x7FF) {
return pack(“C*”, 0xC0 | $bin >> 6, 0×80 | $bin & 0x3F);
} else if ($bin >= 0×800 && $bin <= 0xFFF) {
return pack(“C*”, 0xE0 | $bin >> 11, 0×80 | $bin >> 6 & 0x3F, 0×80 | $bin & 0x3F);
} else if ($bin >= 0×10000 && $bin <= 0x10FFFF) {
return pack(“C*”, 0xE0 | $bin >> 17, 0×80 | $bin >> 12 & 0x3F, 0×80 | $bin >> 6& 0x3F, 0×80 | $bin & 0x3F);
}
}

}

$lang=new Google_Translate_API();

$nom= $lang->translate(“welcome”,”en”,”fr”);

(0) Comments    Read More   
BACK-LINKS and PAGE-RANK WEB DIRECTORY

Free targeted website traffic - www.ad-traffic.net