cvs: pear /HTML_Template_Flexy/Flexy Translator.php

From: Date: Mon, 14 Jun 2004 04:31:26 +0000
Subject: cvs: pear /HTML_Template_Flexy/Flexy Translator.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-20623@lists.php.net to get a copy of this message
alan_k Mon Jun 14 00:31:26 2004 EDT Modified files: /pear/HTML_Template_Flexy/Flexy Translator.php Log: 1000% speed increase on translator http://cvs.php.net/diff.php/pear/HTML_Template_Flexy/Flexy/Translator.php?r1=1.2&r2=1.3&ty=u Index: pear/HTML_Template_Flexy/Flexy/Translator.php diff -u pear/HTML_Template_Flexy/Flexy/Translator.php:1.2 pear/HTML_Template_Flexy/Flexy/Translator.php:1.3 --- pear/HTML_Template_Flexy/Flexy/Translator.php:1.2 Tue Jun 1 23:04:22 2004 +++ pear/HTML_Template_Flexy/Flexy/Translator.php Mon Jun 14 00:31:26 2004 @@ -16,11 +16,11 @@ // | Authors: nobody <nobody@localhost> | // +----------------------------------------------------------------------+ // -// $Id: Translator.php,v 1.2 2004/06/02 03:04:22 alan_k Exp $ +// $Id: Translator.php,v 1.3 2004/06/14 04:31:26 alan_k Exp $ // // Controller Type Class providing translation faciliites // - + /* usage : @@ -131,6 +131,7 @@ function process($get,$post) { //DB_DataObject::debugLevel(1); + $displayLang = isset($get['translate']) ? $get['translate'] : (isset($post['translate']) ? $post['translate'] : false); @@ -159,20 +160,25 @@ $this->clearTemplateCache($displayLang); } - + $t = explode(' ',microtime()); $start= $t[0] + $t[1]; + require_once 'Translation2.php'; $tr = &new Translation2('dataobjectsimple','translations'); $tr->setLang($displayLang); - $suggestions = &new Translation2('dataobjectsimple','translations'); - $suggestions->setLang($displayLang); + //$suggestions = &new Translation2('dataobjectsimple','translations'); + //$suggestions->setLang($displayLang); + $this->compileAll(); + //$tr->setPageID('test.html'); // delete them after we have compiled them!! if (isset($post['_apply'])) { $this->clearTemplateCache($displayLang); } - + //DB_DataObject::debugLevel(1); + $this->loadTranslations(); + $this->loadTranslations($displayLang); $all = array(); foreach($this->words as $page=>$words) { @@ -188,7 +194,8 @@ $md5 = md5($page.':'.$word); - $value = $tr->get($word); + //$value = $tr->get($word); + $value = $this->getTranslation($page,$word,$displayLang); // we posted something.. if (isset($post[$displayLang][$md5])) { $nval = get_magic_quotes_gpc() ? stripslashes($post[$displayLang][$md5]) : $post[$displayLang][$md5]; @@ -216,10 +223,11 @@ $add->to = $value; if (!$add->to || ($add->from == $add->to)) { $add->untranslated = true; - $suggest = $suggestions->get($word); - if ($suggest && ($suggest != $word)) { - $add->suggest = $suggestions->get($word); - } + $add->suggest = implode(', ', $this->getSuggestions($word, $displayLang)); + //$suggest = $suggestions->get($word); + //if ($suggest && ($suggest != $word)) { + // $add->suggest = $suggestions->get($word); + //} } $add->md5 = $md5; @@ -230,17 +238,76 @@ } } - + $t = explode(' ',microtime()); $total= $t[0] + $t[1] - $start; + //printf("Built All in %0.2fs<BR>",$total); $this->status = $status; } - + var $translations = array(); + var $translationMap = array(); + /** + * LoadTranslations - load all the translations from the database + * into $this->translations[{lang}][{id}] = $translation; + * + * + * @param string Language + * @access public + */ + function loadTranslations ($lang= false) { + $d = DB_DataObject::factory('translations'); + $d->lang = ($lang == false) ? '-' : $lang; + $d->find(); + $this->translations[$d->lang] = array(); + while ($d->fetch()) { + $this->translations[$d->lang][$d->string_id] = $d->translation; + if ($lang == false) { + $this->translationMap[$d->page][$d->translation] = $d->string_id; + } + // suggestions:? + + } + } + function getSuggestions($string,$lang) { + $ids = array(); + //echo '<PRE>';print_r($this->translationMap); + foreach($this->translationMap as $page=>$map) { + if (isset($map[$string])) { + $ids[] = $map[$string]; + } + } + //echo '<PRE>';print_r(array($string,$lang,$ids,$this->translations[$lang])); + + //exit; + if (!$ids) { + return array(); + } + $ret = array(); + foreach($ids as $id) { + if (isset($this->translations[$lang][$id])) { + $ret[] = $this->translations[$lang][$id]; + } + } + // echo '<PRE>';print_r($ret); + return $ret; + } - + function getTranslation($page,$word,$lang) + { + + if (!isset($this->translationMap[$page][$word])) { + //echo "No string id for $page : $word\n"; + return false; + } + if (!isset($this->translations[$lang][$this->translationMap[$page][$word]])) { + + return false; + } + return $this->translations[$lang][$this->translationMap[$page][$word]]; + } /** * compile all the templates in a specified folder. * @@ -277,9 +344,12 @@ if (!preg_match('/\.html$/',$name)) { continue; } - - $x = new HTML_Template_Flexy( $o ); + + $oo = $o; $oo['debug'] = 1; + $x = new HTML_Template_Flexy( $oo ); $r = $x->compile($fname); + + //printf(" %0.3fs : $fname<BR>", $time); if (is_a($r,'PEAR_Error')) { echo "compile failed on $fname<BR>"; echo $r->toString();

« previous php.pear.cvs (#20623) next »