Example #1
0
<?php

require "GTranslate.php";
/**
* Example using RequestHTTP
*/
$translate_string = "Das ist wunderschön";
try {
    $gt = new Gtranslate();
    echo "[HTTP] Translating [{$translate_string}] German to English => " . $gt->german_to_english($translate_string) . "<br/>";
    /**
     * Lets switch the request type to CURL
     */
    $gt->setRequestType('curl');
    echo "[CURL] Translating [{$translate_string}] German to English => " . $gt->german_to_english($translate_string) . "<br/>";
} catch (GTranslateException $ge) {
    echo $ge->getMessage();
}
Example #2
0
 /**
  * Translate the content of a file with Google Translate API.
  *
  * @return The automatic translation.
  */
 public function translate()
 {
     // We must check if the file exist.
     // For example, when we start a translation, save it, and then open it from work in progress module, the path don't exist and we must use the fallback path for translation
     $originalContent = is_file($this->full_path) ? file_get_contents($this->full_path) : file_get_contents($this->full_path_fallback);
     // We search for new line caracters and mark it ! (Google API delete new line)
     $originalContent = str_replace("\n", "[@]", $originalContent);
     $lang = AccountManager::getInstance()->vcsLang;
     $translation = false;
     $gt = new Gtranslate();
     $gt->setRequestType('curl');
     $translation = $gt->translate('en', $lang, $originalContent);
     // Replace new line mark
     $translation = str_replace("[@]", "\n", $translation);
     // Few substitutions
     $translation = str_replace("&amp;", "&", $translation);
     $translation = str_replace("&amp;  ", "&", $translation);
     $translation = str_replace("&#39;", "'", $translation);
     $translation = str_replace("&quot;", '"', $translation);
     $translation = str_replace("&lt;", '<', $translation);
     $translation = str_replace("&gt;", '>', $translation);
     // CLeanUp entities. Google return it like this : & Reftitle.parameters;. We convert it like this : &reftitle.parameters;
     $translation = preg_replace_callback("/(&)\\s(.)(.[^;]*?)(;)/s", create_function('$matches', 'return $matches[1].strtolower($matches[2]).$matches[3].$matches[4];'), $translation);
     // We remove extra space after :: operator
     $translation = preg_replace("/(\\w+)(::)(\\s)(\\w+)/s", "\$1\$2\$4", $translation);
     // We delete space into tab like this </ b>
     $translation = preg_replace("/(<\\/\\s(\\w+[^>]*)>)/s", "</\$2>", $translation);
     // We delete space just after an open tag, and just before a close tag, like this <b> foo </b>
     $translation = preg_replace("/(<(\\w+[^>]*)>)(\\s?)(.[^>]*?)(\\s?)(<\\/(\\2)>)/s", "<\$2>\$4</\$7>", $translation);
     return $translation;
 }
Example #3
0
 protected function traduction_google_v1($mot_a_traduire)
 {
     $translate_string = "Das ist wunderschön";
     try {
         $gt = new Gtranslate();
         $gt->setRequestType('curl');
         $function = "en_to_" . substr($this->language->getSelectedValue(), -2, 2);
         return $gt->{$function}($mot_a_traduire);
     } catch (GTranslateException $ge) {
         return "";
     }
 }
 /**
  * Get the translation from a given string using Google Translate API
  */
 public function getGGTranslation()
 {
     if (!AccountManager::getInstance()->isLogged()) {
         return JsonResponseBuilder::failure();
     }
     $str = $this->getRequestVariable('str');
     $lang = AccountManager::getInstance()->vcsLang;
     $translation = false;
     $str = str_replace("\n", "[@]", $str);
     $gt = new Gtranslate();
     $gt->setRequestType('curl');
     $translation = $gt->translate('en', $lang, $str);
     // Replace new line mark
     $translation = str_replace("[@]", "<br>", $translation);
     // Few substitutions
     $translation = str_replace("&amp;", "&", $translation);
     $translation = str_replace("&amp;  ", "&", $translation);
     $translation = str_replace("&#39;", "'", $translation);
     $translation = str_replace("&quot;", '"', $translation);
     $translation = str_replace("&lt;", '<', $translation);
     $translation = str_replace("&gt;", '>', $translation);
     return JsonResponseBuilder::success(array('translation' => $translation));
 }
Example #5
0
 function google_translage($code1, $code2, $content)
 {
     require_once dirname(__FILE__) . '/../includes/lib/GTranslate/GTranslate.php';
     try {
         $code1 = strtolower($code1);
         $code2 = strtolower($code2);
         if ($code1 == 'cn') {
             $code1 = 'zh-cn';
         }
         if ($code2 == 'cn') {
             $code2 = 'zh-cn';
         }
         $language_list = parse_ini_file(dirname(__FILE__) . '/../includes/lib/GTranslate/languages.ini');
         $language_list = array_map("strtolower", $language_list);
         $language_list2 = array_map("strtolower", array_flip($language_list));
         //
         $language_list_v = array_map("strtolower", array_values($language_list));
         $language_list_k = array_map("strtolower", array_keys($language_list));
         if (!isset($language_list2[$code1])) {
             return 'Error language code:' . $code1;
         }
         if (!isset($language_list2[$code2])) {
             return 'Error language code:' . $code2;
         }
         $fromlanguage = $language_list2[$code1];
         $targetlanguage = $language_list2[$code2];
         //print_r($targetlanguage);
         $gt = new Gtranslate();
         $gt->setRequestType('curl');
         $function = $fromlanguage . '_to_' . $targetlanguage;
         $text = $gt->{$function}($content);
         echo $function . "\r\n";
         $text = iconv('UTF-8', 'GBK', $text);
         return $text;
     } catch (GTranslateException $ge) {
         return $ge->getMessage();
     }
 }
function gtranslate_text_translate($text, $lang_from = 'en', $lang_to = 'ru')
{
    global $gt, $l_func, $settings, $seftranslate_error;
    //DBQuery
    $database =& JFactory::getDBO();
    $ret_value = $text;
    if (isset($GLOBALS['gt'])) {
        try {
            $ret_value = $gt->{$l_func}($text);
            $ret_value = html_entity_decode($ret_value[0]->translatedText, ENT_NOQUOTES, 'UTF-8');
        } catch (Exception $e) {
            if ($settings['debug']) {
                throw $e;
                exit;
            } else {
                $seftranslate_error = true;
                return $ret_value;
            }
        }
    } else {
        $gt = new Gtranslate();
        if (!empty($settings['api_google_translate_key'])) {
            //$gt->setApiKey('ABQIAAAARBki4JWk0Cwz-v6GlQ90wBRTxlZd-7gfuqQumodWhI9M82S_fRQ5OJFZp4v4oNXri4eZjoah2n4--w');
            $gt->setApiKey($settings['api_google_translate_key']);
        }
        if ($settings['userip']) {
            $gt->setUserIp($_SERVER['REMOTE_ADDR']);
        }
        $gt->setRequestType('curl');
        $gt->setApiVersion(2);
        $gt->setUrl('https://www.googleapis.com/language/translate/v2');
        $lang_list = parse_ini_file(JPATH_SITE . '/components/com_seftranslate/languages.ini');
        $lang_from2 = array_search($lang_from, $lang_list);
        $lang_to2 = array_search($lang_to, $lang_list);
        $l_func = $lang_from2 . "_to_" . $lang_to2;
        $GLOBALS['gt'] = $gt;
        $GLOBALS['l_func'] = $l_func;
        try {
            $ret_value = $gt->{$l_func}($text);
            $ret_value = html_entity_decode($ret_value[0]->translatedText, ENT_NOQUOTES, 'UTF-8');
        } catch (Exception $e) {
            if ($settings['debug']) {
                throw $e;
                exit;
            } else {
                $seftranslate_error = true;
                return $ret_value;
            }
        }
    }
    if (trim($ret_value) == "") {
        return $ret_value;
    }
    //save to database
    $efentity = new mosSefentity($database);
    $efentity->hash = md5($text);
    $efentity->entity_text = $ret_value;
    $efentity->lang_from = $lang_from;
    $efentity->lang_to = $lang_to;
    $efentity->hits = 1;
    $efentity->date = date("Y-m-d H:i:s");
    $efentity->checkin();
    $efentity->store();
    return $ret_value;
}