/[drupal]/contributions/modules/gtrans/gtrans.module
ViewVC logotype

Contents of /contributions/modules/gtrans/gtrans.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.13 - (show annotations) (download) (as text)
Tue May 8 23:19:03 2007 UTC (2 years, 6 months ago) by owahab
Branch: MAIN
CVS Tags: DRUPAL-5--1-3, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.12: +3 -1 lines
File MIME type: text/x-php
Issue http://drupal.org/node/138412, thanks ica
1 <?php
2 // $Id: gtrans.module,v 1.12 2007/05/08 23:09:47 owahab Exp $
3
4 /**
5 * @file
6 * Used to translate all pages on-the-fly using Google Translate service.
7 *
8 * @author
9 * Omar Abdel-Wahab http://owahab.com
10 */
11
12 /**
13 * Implementation of hook_help().
14 */
15 function gtrans_help($section){
16 $output = '';
17
18 switch ($section) {
19 case 'admin/modules#gtrans':
20 $output = 'gtrans';
21 break;
22 case 'admin/modules#description':
23 case 'admin/help#gtrans':
24 $output = t('Translates site using Google Translation.');
25 break;
26 case 'user/help#gtrans':
27 $output = t('Translates site using Google Translation service.');
28 break;
29 }
30 return $output;
31 }
32
33 /**
34 * Implementation of hook_menu().
35 */
36 function gtrans_menu($may_cache){
37 $items = array();
38
39 $items[] = array('path' => 'admin/settings/gtrans', 'title' => t('Google Translation'),
40 'description' => t('Control Google Translate service.'),
41 'callback' => 'drupal_get_form', 'callback arguments' => 'gtrans_settings');
42
43 return $items;
44 }
45
46 /**
47 * Settings form.
48 */
49 function gtrans_settings(){
50 $languages = gtrans_get_avail_langs();
51 $form['gtrans_enabled_languages']=array('#type'=>'checkboxes',
52 '#title'=>t('Enabled languages'),
53 '#options' => $languages,
54 '#default_value' => variable_get('gtrans_enabled_languages', array('en', 'fr', 'de')),
55 );
56 $form['gtrans_your_language']=array('#type'=>'radios',
57 '#title'=>t('Site language'),
58 '#options' => $languages,
59 '#default_value' => variable_get('gtrans_your_language', 'en'),
60 );
61 return system_settings_form($form);
62 }
63
64 /**
65 * Implementation of hook_block().
66 */
67 function gtrans_block($op = 'list', $delta = 0, $edit = array()) {
68 if ($op == 'list') {
69 $blocks[0]['info'] = t('Change Language');
70 return $blocks;
71 }
72 elseif ($op == 'view') {
73 $block = array();
74 switch ($delta) {
75 case 0:
76 $block['subject'] = t('Change Language');
77 $block['content'] = drupal_get_form('gtrans_change_language_form');
78 return $block;
79 }
80 }
81 }
82
83 function gtrans_change_language_form(){
84 $languages = gtrans_get_avail_langs();
85 // get enabled languages, assume french and spanish by default
86 $lang = variable_get('gtrans_enabled_languages', array('fr' => 'fr', 'es' => 'es'));
87 $hls = array();
88 // show only enabled language
89 foreach($lang as $key => $val){
90 if(!is_null($languages[$val])){
91 $hls[$val] = $languages[$val];
92 }
93 }
94 $form['hl'] = array('#type' => 'select',
95 '#default_value' => ($_SESSION['hl']? $_SESSION['hl'] : 'fr'),
96 '#options' =>$hls,
97 '#prefix' => '<div class="container-inline">',
98 );
99 $form['submit'] = array('#type' => 'submit',
100 '#value' => t('Change'),
101 '#suffix' => '</div>',
102 );
103
104 return $form;
105 }
106 /**
107 * Implementation of hook_submit().
108 */
109 function gtrans_change_language_form_submit($form_id, $edit){
110 $_SESSION['drupal_hl'] = $edit['hl'];
111 }
112
113 /**
114 * Implementation of hook_init().
115 */
116 function gtrans_init(){
117 /*
118 avoid calling translation for POSTed pages
119 and for pages called by Google Translate service,
120 otherwise we'll end up going in a chain of calls
121 of the current drupal page
122 */
123 if(!$_GET['gtrans'] && !$_POST && _garg(0)!='admin'){
124 if($_SESSION['drupal_hl']){
125 gtrans_translate_page($_SESSION['drupal_hl']);
126 }
127 }
128 }
129
130 /**
131 * The real stuf: translate the whole drupal page and send it to the browser.
132 */
133 function gtrans_translate_page($hl='fr'){
134 $languages = gtrans_get_avail_langs();
135 // make sure we're not translating from and to the
136 // same language
137 if($hl != variable_get('gtrans_your_language', 'en')){
138 global $base_url;
139 // Google Translate IP
140 $url = '64.233.179.104';
141 // prepare the request, add a parameter to prevent
142 // recursively attempting to translate
143 $req_uri = str_replace('destination=', '', drupal_get_destination());
144 $uri = 'translate_c?hl=en&ie=UTF-8&oe=UTF-8&langpair='.variable_get('gtrans_your_language', 'en').'%7C'.$hl.'&u='.$base_url.'/'.$req_uri.'%3Fgtrans%3Dtrue';
145 // connecting...
146 $fp=fsockopen($url, 80, $errno, $errstr, 60);
147 if(!$fp){
148 watchdog('user', t('Attempt failed to translate page').' '.$_SERVER['REQUEST_URI'].' '.t('to').' '.$languages[$hl].': Couldn\'t contact Google Translate server');
149 break;
150 }
151 else{
152 // connected!
153 // prepare headers to send
154 $out = "GET /".$uri." HTTP/1.1\r\n";
155 $out .= "Host: ".$url."\r\n";
156 $out .= "Connection: Close\r\n\r\n";
157 // send header
158 fwrite($fp, $out);
159 // receive data
160 $body = false;
161 while (!feof($fp)) {
162 $s = fgets($fp, 1024);
163 if ( $body )
164 $content .= $s;
165 // ignore HTTP headers
166 if ( $s == "\r\n" )
167 $body = true;
168 }
169 // close the connection
170 fclose($fp);
171 // check the data we got:
172 if(!strlen($content)){
173 watchdog('user', t('Attempt failed to translate page').' '.$_SERVER['REQUEST_URI'].' '.t('to').' '.$languages[$hl].': No content from Google Translate server');
174 }
175 else{
176 // now prepare content
177 $content_array = explode("\r\n", $content);
178 $content = $content_array[1];
179 // Google Translate modifes all links to point to it,
180 // we will reverse this action and take the links back
181 $content = preg_replace('/<a href="(.*?)u=(.*?)">/', '<a href="$2">', $content);
182 // now translate HTML entites
183 $content = str_replace(str_replace('%7C', '&#037;7C', htmlentities($uri)), '', $content);
184 // set language in session
185 $_SESSION['drupal_hl'] = $hl;
186 // shoot the translated page and end script run
187 if(strlen($content)>30){
188 die($content);
189 }
190 else{
191 watchdog('user', t('Attempt failed to translate page').' '.$_SERVER['REQUEST_URI'].' '.t('to').' '.$languages[$hl].': Content I got was: '.$content);
192 }
193 }
194 }
195 }
196 }
197
198 /**
199 * A duplicate of arg()
200 *
201 */
202 function _garg($index) {
203 static $arguments, $q;
204
205 if (empty($arguments) || $q != $_GET['q']) {
206 $arguments = explode('/', $_GET['q']);
207 $q = $_GET['q'];
208 }
209
210 if (isset($arguments[$index])) {
211 return $arguments[$index];
212 }
213 }
214
215 function gtrans_get_avail_langs(){
216 return array(
217 'ar'=> 'Arabic',
218 'en'=> 'English',
219 'fr'=> 'French',
220 'de'=> 'Deutsch',
221 'it'=> 'Italian',
222 'es'=> 'Spanish',
223 'pt'=> 'Portuguese',
224 'ja'=> 'Japanese',
225 'zh-CN'=> 'Chinese',
226 'ko'=> 'Korean',
227 );
228 }
229 ?>

  ViewVC Help
Powered by ViewVC 1.1.2