/** * @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; } }
/** * @brief Translates a string of HTML using the job's translation memory * if it contains a match - or with the @p $existingStrings dictionary * if it contains a match. * * @param string $html The HTML string to be translated. * @param array $existingStrings Dictionary of existing strings. This is * meant to be the "previous_translations" content from the job_translatable * table which is generated when the job is compiled based on the contents * of the translation memory at the time. These strings should be in a form * that can be matched without normalization or encoding. * @param int $minStatus The minimum approval status to accept for a translation. * @param int $maxStatus The maximum approval status to accept for a translation. * * @returns string The translated HTML */ public function translateHtml($html, $existingStrings = array(), $minStatus = 3, $maxStatus = 5) { $mem = $this->getTranslationMemory(); 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; } $translations = $mem->getTranslations($strings, $minStatus, $maxStatus); $misses = $matches = 0; foreach ($translations as $k => $v) { if (isset($v)) { $strings[$k] = $v; $matches++; } else { if (isset($existingStrings[$strings[$k]])) { $strings[$k] = $existingStrings[$strings[$k]]; $matches++; } else { $misses++; } } } 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; } }