谷歌翻译 google translation api

长孙兴德
2023-12-01

以下任何一个实例均可实现谷歌翻译功能:

 

 

实例 1:

 

<?php

function translate( $text, $destLang = 'zh-cn', $srcLang = 'en' ) {

$text = urlencode( $text );
$destLang = urlencode( $destLang );
$srcLang = urlencode( $srcLang );

$trans = @file_get_contents( "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q={$text}&langpair={$srcLang}|{$destLang}" );
$json = json_decode( $trans, true );

if( $json['responseStatus'] != '200' ) return false; else return $json['responseData']['translatedText'];

}

echo translate('Google Translate is probably one of the best online translators around. Here’s how to integrate it into your scripts.');
?>

 

 

实例 2:

 

<?php

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"
					)
				)
			)
		);
		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
	 * @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);
	}
}

// example usage
$text = 'Welcome to my website.';
$trans_text = Google_Translate_API::translate($text, 'en', 'zh-cn');
if ($trans_text !== false) {
	echo $trans_text;
}


?>
 

 

 

 

更多查看:http://code.google.com/apis/ajaxlanguage/documentation/

 

 

JavaScript版

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>google翻译</title>
</head>
<body>
内容:<input type="text" id="source" οnchange="submitChange()"><br><br>
语言选择:<select id="langpair">
	<option value="zh-CN|en">汉译英</option>
	<option value="en|zh-CN">英译汉</option>

</select>
<br><br>
翻译结果:<div id="results_body" style="color: red"></div>

<div class=line></div>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
		google.load("language", "1"); 
		google.setOnLoadCallback(submitChange); 
		function submitChange() { 
			var value = document.getElementById('source').value; 
			var langpair = document.getElementById('langpair'); 
			var pair = langpair.options[langpair.selectedIndex].value.split('|'); 
			var src = pair[0]; var dest = pair[1]; 
			google.language.translate(value, src, dest, translateResult); 
			return false; 
		} 
		function translateResult(result) { 
			var resultBody = document.getElementById("results_body"); 
			if (result.translation) { 
				resultBody.innerHTML = result.translation; 
			} else { 
				resultBody.innerHTML = '';
			}
		}
</script>
</body>
</html>
 

 

 

 

 

 

 类似资料: