-
Notifications
You must be signed in to change notification settings - Fork 3
/
Google_Translate_adapter.php
153 lines (131 loc) · 4.42 KB
/
Google_Translate_adapter.php
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/** Zend_Http_Client */
require_once 'Zend/Http/Client.php';
/** Zend_Locale */
require_once 'Zend/Locale.php';
/** Zend_Translate_Adapter */
require_once 'Zend/Translate/Adapter.php';
/**
* Google translate adapter
*
* @package Quino_Translate
* @subpackage Adapter
*/
class Quino_Translate_Adapter_Google extends Zend_Translate_Adapter
{
/**
* Translate language api url
*
* @var string
*/
private $_uri = 'http://ajax.googleapis.com/ajax/services/language/translate';
/**
* Api version string
*
* @var string
*/
private $_apiversion = '1.0';
/**
* Generates the adapter
*
* @param array $data Translation data
* @param string|Zend_Locale $locale OPTIONAL Locale/Language to set, identical with locale identifier,
* see Zend_Locale for more information
* @param array $options OPTIONAL Options to set
*/
public function __construct($data, $locale = null, array $options = array())
{
parent::__construct($data, $locale, $options);
}
/**
* Load translation data
*
* @param string|array $data
* @param string $locale Locale/Language to add data for, identical with locale identifier,
* see Zend_Locale for more information
* @param array $options OPTIONAL Options to use
* @return array
*/
protected function _loadTranslationData($data, $locale, array $options = array())
{
$options = $options + $this->_options;
if (($options['clear'] === true) || !isset($this->_translate[$locale])) {
$this->_translate[$locale] = array();
}
$this->_translate[$locale] = (array) $data + $this->_translate[$locale] + array($locale);
return array();
}
/**
* Translates the given string
* returns the translation
*
* @see Zend_Locale
* @param string|array $messageId Translation string, or Array for plural translations
* @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with
* locale identifier, @see Zend_Locale for more information
* @return string
*/
public function translate($messageId, $locale = null)
{
if (is_null($locale)) {
$locale = $this->_options['content']['locale'];
}
try {
$locale = Zend_Locale::findLocale($locale);
} catch (Zend_Locale_Exception $e) {
// language does not exist, return original string
return $messageId;
}
$locale = new Zend_Locale($locale);
$locale = $locale->getLanguage();
$source = null;
if (isset($this->_options['content']['source'])) {
try {
$source = Zend_Locale::findLocale($this->_options['content']['source']);
} catch (Zend_Locale_Exception $e) {
$source = null;
}
if (!is_null($source)) {
$source = new Zend_Locale($source);
$source = $source->getLanguage();
}
}
if ($source == $locale) {
// return original string
return $messageId;
}
$client = new Zend_Http_Client();
$client->setUri($this->_uri);
$client->setConfig(array(
'maxredirects' => 0,
'timeout' => 30,
'useragent' => null,
));
$client->setParameterGet(array(
'v' => $this->_apiversion,
'q' => $messageId,
'langpair' => $source . '|' . $locale,
));
$response = $client->request();
if ($response->getStatus() !== 200) {
require_once 'Zend/Translate/Exception.php';
throw new Zend_Translate_Exception('Translate failed to load, got response code ' . $response->getStatus());
}
$data = json_decode($response->getBody());
if ($data->responseStatus !== 200) {
require_once 'Zend/Translate/Exception.php';
throw new Zend_Translate_Exception('Google translate api error message is "' . $data->responseDetails . '"');
}
return $data->responseData->translatedText;
}
/**
* returns the adapters name
*
* @return string
*/
public function toString()
{
return 'Google';
}
}
?>