/[drupal]/contributions/modules/translation_framework/contrib/google_translation/google_translation.module
ViewVC logotype

Diff of /contributions/modules/translation_framework/contrib/google_translation/google_translation.module

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

revision 1.1.2.4, Tue Apr 14 17:39:34 2009 UTC revision 1.1.2.5, Thu Apr 23 22:18:03 2009 UTC
# Line 167  function google_translation_translate($t Line 167  function google_translation_translate($t
167          }          }
168        }        }
169        // setting the language pairs that google is looking for        // setting the language pairs that google is looking for
170        $lp = $translate->src .'|'. $translate->dst;        $maxsize = variable_get('google_translation_max_string_size', 500);
171        $url = GOOGLE_TRANSLATION_URL . $lp .'&text='. urlencode($translate->data);        if (!is_numeric($maxsize)) {
172        $result = drupal_http_request($url, array(), GOOGLE_TRANSLATION_HTTP_METHOD, NULL, GOOGLE_TRANSLATION_HTTP_RETRY);          $maxsize = 500;
173          }
174          if (strlen($translate->data) > $maxsize) {
175            $text = _google_translation_split_string($translate);
176            if (!is_null($text)) {
177              $translate->translation = $text;
178            }
179            else {
180              $translate->translation = $translate->data;
181              $translate->error_code = 801;
182              $translate->error_msg = t('Google Translation service could not translate the information that was received');
183            }
184          }
185          else {
186            $lp = $translate->src .'|'. $translate->dst;
187            $url = GOOGLE_TRANSLATION_URL . $lp .'&text='. urlencode($translate->data);
188            $result = drupal_http_request($url, array(), GOOGLE_TRANSLATION_HTTP_METHOD, NULL, GOOGLE_TRANSLATION_HTTP_RETRY);
189            if ($result->code == 200) {
190              preg_match('/<div id=result_box dir="ltr">(.*?)<\/div>/', $result->data, $out);
191              $translate->translation = utf8_encode($out[1]);
192            }
193            else {
194              $translate->translation = $translate->data;
195              $translate->error_code = $result->code;
196              $translate->error_msg = $result->error;
197              watchdog('google_translation', t('Google Translation Failed: Code: !code, Error: !error'), array('!code' => $result->code, '!error' => $result->error), WATCHDOG_ERROR);
198            }
199          }
200        }
201      }
202      return $translate;
203    }
204    
205    /**
206     * Function splits the string into more managable chunks so google will not complain
207     */
208    function _google_translation_split_string($translate) {
209      $translated_string = '';
210      $maxsize = variable_get('google_translation_max_string_size', 500);
211      if (!is_numeric($maxsize)) {
212        $maxsize = 500;
213      }
214      $str = wordwrap($translate->data, $maxsize, "<BREAKONME>");
215      $pieces = explode("<BREAKONME>", $str);
216      $lp = $translate->src .'|'. $translate->dst;
217      $url = GOOGLE_TRANSLATION_URL . $lp .'&text=';
218      if (is_array($pieces) && !empty($pieces)) {
219        foreach ($pieces as $piece) {
220          $result = drupal_http_request($url . urlencode($piece), array(), GOOGLE_TRANSLATION_HTTP_METHOD, NULL, GOOGLE_TRANSLATION_HTTP_RETRY);
221        if ($result->code == 200) {        if ($result->code == 200) {
222          preg_match('/<div id=result_box dir="ltr">(.*?)<\/div>/', $result->data, $out);          preg_match('/<div id=result_box dir="ltr">(.*?)<\/div>/', $result->data, $out);
223          $translate->translation = utf8_encode($out[1]);          $translated_string .= utf8_encode($out[1]);
224        }        }
225        else {        else {
226          $translate->translation = $translate->data;          $translate->translation = $translate->data;
227          $translate->error_code = $result->code;          $translate->error_code = $result->code;
228          $translate->error_msg = $result->error;          $translate->error_msg = $result->error;
229          watchdog('google_translation', t('Google Translation Failed: Code: !code, Error: !error'), array('!code' => $result->code, '!error' => $result->error), WATCHDOG_ERROR);          watchdog('google_translation', t('Google Translation Failed: Code: !code, Error: !error'), array('!code' => $result->code, '!error' => $result->error), WATCHDOG_ERROR);
230            $translated_string = '';
231            break;
232        }        }
233      }      }
234    }    }
235    return $translate;    if (drupal_strlen($translated_string)) {
236        return $translated_string;
237      }
238      return NULL;
239  }  }
240    
241  /**  /**

Legend:
Removed from v.1.1.2.4  
changed lines
  Added in v.1.1.2.5

  ViewVC Help
Powered by ViewVC 1.1.2