Archive for January, 2009

Google AJAX Language API php class

This class allows you to use the Google AJAX Translation API to translate arbitrary text to the many languages supported by the Google API.

Source versioning added to:
http://code.google.com/p/php-language-api/

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
< ?php /** * Translating language with Google API * @author gabe@fijiwebdesign.com * @version $id$ * @license - Share-Alike 3.0 (http://creativecommons.org/licenses/by-sa/3.0/) *  * Google requires attribution for their Language API, please see: http://code.google.com/apis/ajaxlanguage/documentation/#Branding *  */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, '', 'it');if ($trans_text !== false) {	echo $trans_text;}  ?>< ?php
 
/**
 * Translating language with Google API
 * @author gabe@fijiwebdesign.com
 * @version $id$
 * @license - Share-Alike 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
 * 
 * Google requires attribution for their Language API, please see: http://code.google.com/apis/ajaxlanguage/documentation/#Branding
 * 
 */
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, '', 'it');
if ($trans_text !== false) {
	echo $trans_text;
}
 
 
?>