-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleTranslator.class.php
More file actions
138 lines (112 loc) · 4.27 KB
/
GoogleTranslator.class.php
File metadata and controls
138 lines (112 loc) · 4.27 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* GoogleTranslator for PHP - library for translating text via Google Translator API.
*
* @author David Grudl
* @author Will Skates
* @copyright Copyright (c) 2010 David Grudl
* @license New BSD License
* @link http://phpfashion.com/
* @link http://twitter.com/willskates
* @version 1.0 - Modified (Will Skates).
*/
abstract class GoogleTranslator
{
// An array to store the language params.
public static $langs = array(
'sourceLang'=>'',
'destLang'=>''
);
//An array to contain a list of translations which are already complete.
public static $translations = array();
// ------------------------------------------------------------------------
/**
* Translate.
*
* Translates text from a source to a destination language.
*
* Note: Source and destination language references must be in ISO format (e.g. en,de)
*
* @param String $text - The text we want to translate.
* @param String $sourceLang - The source language reference (optional).
* @param String $destLang - The destination language reference (optional).
*/
public static function translate($text,$sourceLang = false,$destLang = false)
{
//Store the variable names that we need to access (these are mimiced in self::$langs).
$vars = array('sourceLang','destLang');
foreach ($vars as $k=>$v) {
/*
* Check to see if the parameter was passed with the function.
* If not, use one that has already been set (if possible).
*/
if ( $$v === false ) { $$v = self::$langs[$v]; }
if ( empty($$v) ) {
throw new GoogleTranslatorException('No value was set for ' . $v . '.');
}
}
//Create an array reference string for this translation.
$ref = md5($text . $sourceLang . $destLang);
//If we've already done the work, don't bother doing it again.
if ( isset(self::$translations[$ref]) ) { return self::$translations[$ref]; }
//Get the translation from the api.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://ajax.googleapis.com/ajax/services/language/translate');
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // no echo, just return result
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'v' => '1.0',
'langpair' => $sourceLang . '|' . $destLang,
'q' => (string) $text
));
$result = curl_exec($curl);
if (curl_errno($curl)) {
throw new GoogleTranslatorException('Server error: ' . curl_error($curl));
}
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($code != 200) {
throw new GoogleTranslatorException("Server error #$code", $code);
}
$json = @json_decode($result); // intentionally @ - Why?
if (!isset($json->responseData->translatedText)) {
throw new GoogleTranslatorException('Invalid server response');
}
//Make sure we don't have to make the translation a second time.
self::$translations[$ref] = $json->responseData->translatedText;
return self::$translations[$ref];
}
// ------------------------------------------------------------------------
/**
* Set Languages
*
* In case the developer using this class wants to batch process a lot of
* translations from one to another, allow them to set the translation languages here.
*
* The function also allows either parameter to be optional so that they can be set
* individually.
*
* E.g.
* To set the source: GoogleTranslator::setLanguages('en');
* To set the destination: GoogleTranslator::setLanguages(false,'de');
* To set both: GoogleTranslator::setLanguages('en','de');
*
* @param String $sourceLang - The source language reference (optional).
* @param String $destLang - The destination language reference (optional).
*/
public static function setLanguages($sourceLang = false,$destLang = false)
{
if ($sourceLang === false && $destLang === false ) {
throw new GoogleTranslatorException('You must set either a source or destination language.');
}
$vars = array('sourceLang','destLang');
foreach ($vars as $k=>$v) { if ( $$v !== false ) { self::$langs[$v] = $$v; } }
}
// ------------------------------------------------------------------------
}
/**
* An exception generated by GoogleTranslator.
*/
class GoogleTranslatorException extends Exception
{
}