function eventTranslateLanguage(EventControler $evtcl)
 {
     $src_lang = explode("_", $this->src_lng);
     $dest_lng = explode("_", $this->dest_lng);
     $fields_content = MergeString::getField($evtcl->et_content_src);
     $arr_fields_content = array();
     foreach ($fields_content as $fields_cont) {
         $arr_fields_content[$fields_cont] = "AB" . rand(1, 9999) . "YZ";
     }
     $content = htmlspecialchars($evtcl->et_content_src, ENT_QUOTES);
     $content = MergeString::withArray($content, $arr_fields_content);
     $trans_content = parent::translate($content, $src_lang[0], $dest_lng[0]);
     $trans_content = htmlspecialchars_decode($trans_content);
     $content = $this->withField($trans_content, $arr_fields_content);
     $_SESSION["et_content_src"] = $content;
 }
Example #2
0
 public function translate($text)
 {
     if (empty($text) || empty($this->from) || empty($this->to)) {
         return $text;
     }
     $key = md5($text);
     // TODO: split string by \\n, \\t ...
     // get the dictionary file first
     if (!$this->dict) {
         $this->dict = $this->readDictFile();
     }
     if (isset($this->dict[$key])) {
         echo "hit dictionary for {$text}" . nl;
         return $this->dict[$key]['to_text'];
     }
     // call google translation api
     require_once 'google.translator.php';
     $toText = Google_Translate_API::translate($text, $this->from, $this->to);
     if ($toText !== false) {
         $this->dict[$key] = array('from_text' => $text, 'to_text' => $toText);
     }
     return $toText;
 }
Example #3
0
<?php

// utf-8 is required to display characters properly
header('Content-Type: text/html; charset=utf-8');
require_once 'google.translator.php';
// translate apple from english to portuguese
$str = Google_Translate_API::translate('Apple', 'en', 'pt');
// view the translated string
var_dump($str);
 function eventTranslateLanguage(EventControler $evtcl)
 {
     //$src_lang = explode("_",$evtcl->src_lang);
     //$dest_lng = explode("_",$evtcl->dest_lang);
     $src_lang = explode("_", $this->src_lng);
     $dest_lng = explode("_", $this->dest_lng);
     $fields_subject = MergeString::getField($evtcl->et_sub_src);
     $arr_fields_subject = array();
     foreach ($fields_subject as $fields_sub) {
         $arr_fields_subject[$fields_sub] = "AB" . rand(1, 9999) . "YZ";
     }
     $subject = htmlspecialchars($evtcl->et_sub_src, ENT_QUOTES);
     $subject = MergeString::withArray($subject, $arr_fields_subject);
     $fields_bodytext = MergeString::getField($evtcl->et_body_text_src);
     $arr_bodytext = array();
     foreach ($fields_bodytext as $fields_body_text) {
         $arr_bodytext[$fields_body_text] = "AB" . rand(1, 9999) . "YZ";
     }
     $bodytext = htmlspecialchars($evtcl->et_body_text_src, ENT_QUOTES);
     $bodytext = MergeString::withArray($bodytext, $arr_bodytext);
     $fields_bodyhtml = MergeString::getField($evtcl->et_body_html_src);
     $arr_bodyhtml = array();
     foreach ($fields_bodyhtml as $fields_body_html) {
         $arr_bodyhtml[$fields_body_html] = "AB" . rand(1, 9999) . "YZ";
     }
     $bodyhtml = htmlspecialchars($evtcl->et_body_html_src, ENT_QUOTES);
     $bodyhtml = MergeString::withArray($bodyhtml, $arr_bodyhtml);
     $trans_subject = parent::translate($subject, $src_lang[0], $dest_lng[0]);
     $trans_bodytext = parent::translate($bodytext, $src_lang[0], $dest_lng[0]);
     $trans_bodyhtml = parent::translate($bodyhtml, $src_lang[0], $dest_lng[0]);
     $trans_subject = htmlspecialchars_decode($trans_subject);
     $trans_bodytext = htmlspecialchars_decode($trans_bodytext);
     $trans_bodyhtml = htmlspecialchars_decode($trans_bodyhtml);
     $subject = $this->withField($trans_subject, $arr_fields_subject);
     $bodytext = $this->withField($trans_bodytext, $arr_bodytext);
     $bodyhtml = $this->withField($trans_bodyhtml, $arr_bodyhtml);
     $_SESSION["et_sub_src"] = $subject;
     $_SESSION["et_body_text_src"] = $bodytext;
     $_SESSION["et_body_html_src"] = $bodyhtml;
 }
Example #5
0
 */
if (!isset($PAGEDATA->vars['translate_page_id']) || !$PAGEDATA->vars['translate_page_id']) {
    $PAGEDATA->body = '<em>no page chosen to translate</em>';
} else {
    if (!isset($PAGEDATA->vars['translate_language']) || strlen($PAGEDATA->vars['translate_language']) != 2) {
        $PAGEDATA->body = '<em>invalid language chosen</em>';
    } else {
        $fp = Page::getInstance($PAGEDATA->vars['translate_page_id']);
        if (!$fp) {
            $PAGEDATA->body = '<em>page to translate was not found!</em>';
        } else {
            $edate = $PAGEDATA->edate;
            $fedate = $fp->edate;
            /* if original document has been updated, or translation page is empty,
             * then update the translation
             */
            if (strcmp($edate, $fedate) < 0 || strlen($PAGEDATA->body) < 10) {
                require_once 'google.translator.php';
                $text = $fp->body;
                $c = Google_Translate_API::translate($text, $PAGEDATA->vars['translate_language_from'], $PAGEDATA->vars['translate_language']);
                if ($c === false) {
                    $PAGEDATA->body = '<em>translation failed! Original document is <a href="' . $fp->getRelativeUrl() . '">here</a>. Failed to translate to "' . $PAGEDATA->vars['translate_language'] . '" using Google Translate.</em>';
                } else {
                    $PAGEDATA->body = $c;
                    dbQuery('update pages set body="' . addslashes($c) . '",edate=now() where id=' . $PAGEDATA->id);
                    Core_cacheClear('pages');
                }
            }
        }
    }
}