예제 #1
0
 /**
  * @brief Translates HTML using the specified translation memory and settings.
  *
  * @param string $html The HTML to translate.
  * @param array &$out An out parameter that passes out the following info:
  *		- misses : The number of strings that did not find a match in the TM.
  * 		- matches : The number of strings that found a match in the TM.
  * @return string The translated HTML.
  */
 public function translateHtml($html, &$stats, $logMisses = false)
 {
     $mem = $this->translationMemory;
     $minStatus = $this->minStatus;
     $maxStatus = $this->maxStatus;
     require_once 'inc/WebLite_Translate.class.php';
     $translator = new Weblite_HTML_Translator();
     $html2 = $translator->extractStrings($html);
     $strings = $translator->strings;
     if (isset($this->lastStrings)) {
         unset($this->lastStrings);
     }
     // Store the string output so that we can use it from outside
     // after the function returns
     $this->lastStrings =& $strings;
     $paramsArr = array();
     foreach ($strings as $k => $v) {
         unset($params);
         $strings[$k] = TMTools::encode($v, $params);
         $paramsArr[$k] = $params;
     }
     $translations = $mem->getTranslations($strings, $minStatus, $maxStatus);
     $matches = 0;
     $misses = 0;
     $log = array();
     if (!$logMisses) {
         foreach ($translations as $k => $v) {
             if (isset($v)) {
                 $strings[$k] = $v;
                 $matches++;
             } else {
                 $misses++;
             }
         }
     } else {
         foreach ($translations as $k => $v) {
             if (isset($v)) {
                 $strings[$k] = $v;
                 $matches++;
             } else {
                 $log[$k] = $strings[$k];
                 $misses++;
             }
         }
     }
     $stats = array('matches' => $matches, 'misses' => $misses);
     //$out = $this->getTranslation($mem->getDestinationLanguage());
     if ($logMisses) {
         foreach ($log as $k => $v) {
             try {
                 //print_r($paramsArr[$k]);
                 $log[$k] = TMTools::decode($v, $paramsArr[$k]);
             } catch (Exception $ex) {
                 echo $ex->getMessage();
                 exit;
             }
         }
         $stats['log'] = $log;
     }
     if ($matches == 0) {
         return $html;
     } else {
         foreach ($strings as $k => $v) {
             $translator->strings[$k] = TMTools::decode($v, $paramsArr[$k]);
         }
         $html = $translator->replaceStrings($html2);
         return $html;
     }
 }
예제 #2
0
 public function extractDictionaryFromHtml($mem, $html)
 {
     //1. extract all the strings from the html content
     //and normalize them
     require_once 'inc/WebLite_Translate.class.php';
     $translator = new Weblite_HTML_Translator();
     $html2 = $translator->extractStrings($html);
     $strings = $translator->strings;
     $paramsArr = array();
     foreach ($strings as $k => $v) {
         unset($params);
         $strings[$k] = TMTools::encode($v, $params);
         $paramsArr[$k] = $params;
     }
     //2. get existing translations for the strings
     //from translation memory
     return $mem->getTranslations($strings);
 }