diff --git a/i18n_auto.module b/i18n_auto.module index 1020ac2..e340444 100644 --- a/i18n_auto.module +++ b/i18n_auto.module @@ -109,6 +109,12 @@ function i18n_auto_action_node_translate($node) { // create a translation of the node for each enabled language // other than the source node language $languages = i18n_language_list(); + + // check if submitted node languages is set + if(!empty($_REQUEST[language])){ + $node->language = $_REQUEST[language]; + } + unset($languages[$node->language]); // exclude the language of the new node foreach ($languages as $langcode => $language) { @@ -167,20 +173,29 @@ function i18n_auto_action_node_translate($node) { } // get a translation from Google - require_once("gtranslate-api-php/GTranslate.php"); - $func = $node->language . '_to_' . $langcode; - + + require_once 'google-api-php-client/src/Google_Client.php'; + require_once 'google-api-php-client/src/contrib/Google_TranslateService.php'; + + + $client = new Google_Client(); + + function t_func($t_str, $langcode, $client){ + $gt = new Google_TranslateService($client); + $t_ar = $gt->translations->listTranslations($t_str, $langcode); + $t = $t_ar[translations][0][translatedText]; + return ($t); + } + if ($body_split) { // define variables $tbody_array = array(); $chunk_failed = FALSE; - foreach ($body_array as $key => $chunk) { - try { - $gt = new Gtranslate; - $tbody_array[$key] = $gt->$func($chunk); + try { + $tbody_array[$key] = t_func($chunk, $langcode, $client); } - catch (GTranslateException $ge) { + catch (Google_Exception $ge) { // if there was a problem, pass the error to watchdog. $message = t('From GTranslate: ') . $ge->getMessage(); watchdog(t('auto translate'), $message, array(), WATCHDOG_ERROR); @@ -188,12 +203,12 @@ function i18n_auto_action_node_translate($node) { $chunk_failed = TRUE; } } - if ($chunk_failed) { - $new_node->body = $node->body; - } + $new_node->body = $node->body; + } else { - foreach ($tbody_array as $tchunk) { + + foreach ($tbody_array as $tchunk) { $tbody .= $tchunk; } // attempt to turn mark-up back in to real line breaks @@ -203,16 +218,15 @@ function i18n_auto_action_node_translate($node) { $tbody = str_replace('
', "\n", $tbody); $tbody = str_replace('
', "\n", $tbody); $tbody = str_replace('
', "\n", $tbody); - $new_node->body = $tbody; + $new_node->body = $tbody; } } else { try { - $gt = new Gtranslate; // we need to send HTML to Google to not lose formatting $body_markup = check_markup($node->body); - $tbody = $gt->$func($body_markup); + $tbody = t_func($body_markup, $langcode, $client); // attempt to turn mark-up back in to real line breaks // TODO: I'm sure this can be nicer! See http://drupal.org/node/684340 $tbody = str_replace('

', '', $tbody); @@ -222,7 +236,7 @@ function i18n_auto_action_node_translate($node) { $tbody = str_replace('
', "\n", $tbody); $new_node->body = $tbody; } - catch (GTranslateException $ge) { + catch (Google_Exception $ge) { // if there was a problem, pass the error to watchdog. $message = t('From GTranslate: ') . $ge->getMessage(); watchdog(t('auto translate'), $message, array(), WATCHDOG_ERROR); @@ -233,12 +247,11 @@ function i18n_auto_action_node_translate($node) { try { - $gt = new Gtranslate; - $ttitle = $gt->$func($node->title); + $ttitle = t_func($node->title, $langcode, $client); // get rid of any HTML encoded data Google might send $new_node->title = html_entity_decode($ttitle, ENT_QUOTES); } - catch (GTranslateException $ge) { + catch (Google_Exception $ge) { // if there was a problem, pass the error to watchdog. $message = t('From GTranslate: ') . $ge->getMessage(); watchdog(t('auto translate'), $message, array(), WATCHDOG_ERROR);