My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?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. 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 >= 0x80 && $bin <= 0x7FF) {
return pack("C*", 0xC0 | $bin >> 6, 0x80 | $bin & 0x3F);
} else if ($bin >= 0x800 && $bin <= 0xFFF) {
return pack("C*", 0xE0 | $bin >> 11, 0x80 | $bin >> 6 & 0x3F, 0x80 | $bin & 0x3F);
} else if ($bin >= 0x10000 && $bin <= 0x10FFFF) {
return pack("C*", 0xE0 | $bin >> 17, 0x80 | $bin >> 12 & 0x3F, 0x80 | $bin >> 6& 0x3F, 0x80 | $bin & 0x3F);
}
}

}


?>


Change log

r7 by bucabay on Aug 20, 2009   Diff
Moved test to its own file
The decoding of UTF-8 escape sequences was
added to but not complete, still needs to
be tested
Go to: 
Project members, sign in to write a code review

Older revisions

r6 by bucabay on Jan 19, 2009   Diff
Added ID keyword
r5 by bucabay on Jan 19, 2009   Diff
Typo in version id
r4 by bucabay on Jan 19, 2009   Diff
Removed debugging.
All revisions of this file

File info

Size: 2616 bytes, 81 lines

File properties

svn:keywords
Id
Powered by Google Project Hosting