home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / Template / Flexy / Translator.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  15.1 KB  |  475 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors:  nobody <nobody@localhost>                                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Translator.php,v 1.10 2006/11/24 07:03:59 alan_k Exp $
  20. //
  21. //  Controller Type Class providing translation faciliites
  22. //
  23.    
  24. /*
  25.  
  26. usage : 
  27.  
  28. $t = new HTML_Template_Flexy_Translator(array(
  29.     'baseLang'      => 'en',
  30.     'targetLangs'   => array('es','fr','zh'),
  31.     'appURL'       => '/admin/translate.php',
  32.  
  33. ));
  34. $t->process(isset($_GET ? $_GET : array(),isset($_POST ? $_POST : array()); // read data.. etc.
  35. // you can replace this pretty easily with your own templates..
  36. $t->outputDefautTemplate();
  37.  
  38. */
  39.  
  40. class HTML_Template_Flexy_Translator {
  41.     
  42.     /**
  43.     * Options for Translator tool.
  44.     *
  45.     * @var array
  46.     * @access public 
  47.     */
  48.     var $options = array(
  49.         'baseLang'          => 'en',            // the language the templates are in.
  50.         'targetLangs'       => array('fr'),     // the language the templates are being translated to.
  51.         'templateDir'       => '',              // these are read from global config if not set.
  52.         'compileDir'        => '',        
  53.         'url_rewrite'       => '',              // for image rewriting.. -- needs better thinking through!
  54.         'appURL'            => '',              // url to translation too : eg. /admin/translator.php
  55.         'Translation2'      => array(
  56.                                 'driver' => 'dataobjectsimple', 
  57.                                 'options' => 'translations'
  58.                             ),
  59.  
  60.     );
  61.     /**
  62.     * app URL (copied from above)
  63.     *
  64.     * @var string
  65.     * @access public 
  66.     */
  67.     var $appURL;
  68.     var $languages = array();
  69.     /**
  70.     * Array of templates and the words found in each one.
  71.     *
  72.     * @var array
  73.     * @access public 
  74.     */
  75.     var $words= array();   
  76.     /**
  77.     * Array of objects with name, md5's, has it been set, the translation etc.
  78.     *
  79.     * @var array
  80.     * @access public 
  81.     */
  82.     var $status = array();
  83.     /**
  84.     * The current language
  85.     *
  86.     * @var array
  87.     * @access public 
  88.     */
  89.     var $translate = ''; // language being displayed /edited.
  90.     
  91.     
  92.     /**
  93.     * constructor
  94.     *
  95.     * Just set options (no checking done)
  96.     * 
  97.     * 
  98.     * @param   array   see options array in file.
  99.     * @return   none
  100.     * @access   public
  101.     */
  102.   
  103.     function HTML_Template_Flexy_Translator($options= array()) {
  104.         foreach($options as $k=>$v) {
  105.             $this->options[$k]  = $v;
  106.         }
  107.         if (!in_array($this->options['baseLang'], $this->options['targetLangs'])) {
  108.             $this->options['targetLangs'][] = $this->options['baseLang'];
  109.         }
  110.         $o = PEAR::getStaticProperty('HTML_Template_Flexy','options');
  111.         if (!strlen($this->options['templateDir'])) {
  112.             $this->options['templateDir'] = $o['templateDir'];
  113.         }
  114.         if (!strlen($this->options['compileDir'])) {
  115.             $this->options['compileDir'] = $o['compileDir'];
  116.         }
  117.         if (!strlen($this->options['url_rewrite'])) {
  118.             $this->options['url_rewrite'] = $o['url_rewrite'];
  119.         }
  120.         if (empty($this->options['Translation2'])) {
  121.             $this->options['Translation2'] = $o['Translation2'];
  122.         }
  123.         $this->appURL = $this->options['appURL'];
  124.         $this->languages = $this->options['targetLangs'];
  125.     }
  126.     
  127.     
  128.     /**
  129.     * process the input 
  130.     *
  131.     * 
  132.     * @param   array   $_GET; (translate = en)
  133.     * @param   array   $_POST; (translate = en, en[{md5}] = translation)
  134.     
  135.     * @return   none
  136.     * @access   public
  137.     */
  138.     
  139.     
  140.     function process($get,$post)
  141.     {
  142.         //DB_DataObject::debugLevel(1);
  143.         
  144.         $displayLang = isset($get['translate']) ? $get['translate'] : 
  145.             (isset($post['translate']) ? $post['translate'] : false);
  146.             
  147.         if ($displayLang === false) {          
  148.             return;
  149.         }
  150.         require_once 'Translation2/Admin.php';
  151.           
  152.         $driver = $this->options['Translation2']['driver'];
  153.         $options = $this->options['Translation2']['options'];
  154.         $usingGT = ($driver == 'gettext');
  155.         $usingDO = ($driver == 'dataobjectsimple');
  156.         $trd = &Translation2_Admin::factory($driver, $options);
  157.         
  158.         
  159.         
  160.         //$trd->setDecoratedLang('en');
  161.         foreach($this->options['targetLangs'] as $l) {
  162.             $trd->addLang(array(
  163.                 'lang_id' => $l
  164.             ));
  165.         }
  166.         
  167.         // back to parent if no language selected..
  168.         
  169.         if (!in_array($displayLang, $this->options['targetLangs'] )) {
  170.             require_once 'PEAR.php';
  171.             return PEAR::raiseError('Unknown Language :' .$displayLang);
  172.         }
  173.         
  174.         $this->translate = $displayLang;
  175.         
  176.         
  177.         if (isset($post['_apply'])) {
  178.             $this->clearTemplateCache($displayLang);
  179.              
  180.         }
  181.         $t = explode(' ',microtime()); $start= $t[0] + $t[1];
  182.      
  183.         require_once 'Translation2.php';
  184.         $tr = &Translation2::factory($driver, $options);
  185.         $tr->setLang($displayLang);
  186.         
  187.         if (!$usingDO) {
  188.             $suggestions = &Translation2::factory($driver, $options);
  189.             $suggestions->setLang($displayLang);
  190.         }
  191.         
  192.         $this->compileAll();
  193.         
  194.         //$tr->setPageID('test.html');
  195.         // delete them after we have compiled them!!
  196.         if (isset($post['_apply'])) {
  197.             $this->clearTemplateCache($displayLang);
  198.         }
  199.         //DB_DataObject::debugLevel(1);
  200.         if ($usingDO) {
  201.             $this->loadTranslations();
  202.             $this->loadTranslations($displayLang);
  203.         }
  204.         
  205.         $all = array();
  206.         
  207.         if ($usingGT) {
  208.             $trd->storage->begin();
  209.         }
  210.         $displayLangClean = str_replace('.', '_', $displayLang);
  211.                  
  212.         foreach($this->words as $page=>$words) {
  213.             $status[$page] = array();
  214.             $tr->setPageID($page);
  215.             // pages....
  216.             if (isset($post['_clear']) && !PEAR::isError($p = $trd->getPage($page, $displayLang))) {
  217.                 $diff = array_diff(array_keys($p), $words);
  218.                 if (count($diff)) {
  219.                     foreach ($diff as $string) {
  220.                         $trd->remove($string, $page);
  221.                     }
  222.                 }
  223.             }
  224.  
  225.             foreach ($words as $word) {
  226.             
  227.                 if (!strlen(trim($word))) { 
  228.                     continue;
  229.                 }
  230.                 
  231.                 $md5 = md5($page.':'.$word);
  232.                 
  233.                 $value = $usingDO ? $this->getTranslation($page,$word,$displayLang) : $tr->get($word);
  234.                 
  235.                 // we posted something..
  236.                 if (isset($post[$displayLangClean][$md5])) {
  237.                     // eak we shouldnt really deal with magic_quotes!!!
  238.                     $nval = str_replace("\r\n", "\n", 
  239.                         get_magic_quotes_gpc() ? 
  240.                             stripslashes($post[$displayLangClean][$md5]) : 
  241.                             $post[$displayLangClean][$md5]);
  242.                     
  243.                     if ($value != $nval) {
  244.                         $trd->add($word,$page,array($displayLang=>$nval));
  245.                         $value = $nval;
  246.                     }
  247.                 }
  248.                 
  249.                 if ($value == '') {
  250.                     // try the old gettext...
  251.                     if (isset($old[addslashes($word)])) {
  252.                         $trd->add($word,$page,array($displayLang=>$old[addslashes($word)]));
  253.                         $value = $old[addslashes($word)];
  254.                     }
  255.                 
  256.                 
  257.                 }
  258.                 
  259.                 $add = new StdClass;
  260.                  
  261.                 $add->from = $word;
  262.                 $add->to   = $value;
  263.                 if (!$add->to || ($add->from == $add->to)) {
  264.                     $add->untranslated = true;
  265.                     
  266.                     if ($usingDO) {
  267.                         $add->suggest = implode(', ', $this->getSuggestions($word, $displayLang));
  268.                     } else {
  269.                         $suggest = $suggestions->get($word);
  270.                         if ($suggest && ($suggest != $word)) {
  271.                             $add->suggest = $suggest;
  272.                         }
  273.                     }
  274.                     
  275.                     
  276.                 }
  277.  
  278.                 $add->md5 = $md5;
  279.                 // show big or small text entry..
  280.                 $add->short = (bool) (strlen($add->from) < 30 && strstr($add->from, "\n") === false);
  281.                 
  282.                 $status[$page][] = $add;
  283.             
  284.                  
  285.             }
  286.             
  287.         }
  288.         if ($usingGT) {
  289.             $trd->storage->commit();
  290.         }
  291.         $t = explode(' ',microtime()); $total= $t[0] + $t[1] -  $start;
  292.         //printf("Built All in %0.2fs<BR>",$total);
  293.         $this->status = $status;
  294.           
  295.              
  296.     
  297.     }
  298.     var $translations = array();
  299.     var $translationMap = array();
  300.    
  301.     /**
  302.     * LoadTranslations - load all the translations from the database
  303.     * into $this->translations[{lang}][{id}] = $translation;
  304.     *
  305.     * 
  306.     * @param   string       Language
  307.     * @access   public
  308.     */
  309.     function loadTranslations ($lang= false) {
  310.         $d = DB_DataObject::factory('translations');
  311.         $d->lang = ($lang == false) ? '-' : $lang;
  312.         $d->find();
  313.         $this->translations[$d->lang] = array();
  314.         while ($d->fetch()) {
  315.             $this->translations[$d->lang][$d->string_id] = $d->translation;
  316.             if ($lang == false) {
  317.                 $this->translationMap[$d->page][$d->translation] = $d->string_id;
  318.             }
  319.             // suggestions:?
  320.             
  321.         }
  322.     }
  323.     
  324.     function getSuggestions($string,$lang) {
  325.         $ids = array();
  326.         //echo '<PRE>';print_r($this->translationMap);
  327.         foreach($this->translationMap as $page=>$map) {
  328.             if (isset($map[$string])) {
  329.                 $ids[] = $map[$string];
  330.             }
  331.         }
  332.         //echo '<PRE>';print_r(array($string,$lang,$ids,$this->translations[$lang]));
  333.         
  334.         //exit;
  335.         if (!$ids) {
  336.             return array();
  337.         }
  338.         $ret = array();
  339.         foreach($ids as $id) {
  340.             if (isset($this->translations[$lang][$id])) {
  341.                 $ret[] = $this->translations[$lang][$id];
  342.             }
  343.         }
  344.        // echo '<PRE>';print_r($ret);
  345.         return $ret;
  346.     }
  347.     
  348.     function getTranslation($page,$word,$lang)
  349.     {
  350.         
  351.         if (!isset($this->translationMap[$page][$word])) {
  352.             //echo "No string id for $page : $word\n";
  353.             return false;
  354.         }
  355.         if (!isset($this->translations[$lang][$this->translationMap[$page][$word]])) {
  356.         
  357.             return false;
  358.         }
  359.         return $this->translations[$lang][$this->translationMap[$page][$word]];
  360.     }
  361.     /**
  362.     * compile all the templates in a specified folder.
  363.     *
  364.     * 
  365.     * @param   string   subdirectory of templateDir or empty
  366.     * @return   none
  367.     * @access   public
  368.     */
  369.  
  370.     function compileAll($d='') {
  371.         set_time_limit(0); // this could take quite a while!!!
  372.         
  373.         $words = array();
  374.         $dname = $d ? $this->options['templateDir'] .'/'.$d  : $this->options['templateDir'];
  375.         //echo "Open $dname<BR>";
  376.         $dh = opendir( $dname);
  377.         require_once 'HTML/Template/Flexy.php';
  378.         $o = $this->options;
  379.         $o['fatalError'] = PEAR_ERROR_RETURN;
  380.         $o['locale'] = 'en';
  381.         while (($name = readdir($dh)) !== false) {
  382.             $fname = $d ? $d .'/'. $name : $name;
  383.             
  384.             if ($name{0} == '.') {
  385.                 continue;
  386.             }
  387.             
  388.             if (is_dir($this->options['templateDir'] . '/'. $fname)) {
  389.                 $this->compileAll($fname);
  390.                 continue;
  391.             }
  392.                 
  393.                 
  394.             if (!preg_match('/\.html$/',$name)) {
  395.                 continue;
  396.             }
  397.             
  398.             $oo = $o;// $oo['debug'] = 1; 
  399.             $x = new HTML_Template_Flexy( $oo );
  400.             $r = $x->compile($fname);
  401.             
  402.             //printf(" %0.3fs : $fname<BR>", $time);
  403.             if (is_a($r,'PEAR_Error')) {
  404.                 echo "compile failed on $fname<BR>";
  405.                 echo $r->toString();
  406.                 continue;
  407.             }
  408.             $this->words[$fname] = file_exists($x->getTextStringsFile) ?
  409.                 unserialize(file_get_contents($x->getTextStringsFile)) :
  410.                 array();
  411.         }
  412.         //echo '<PRE>';print_R($words);exit;
  413.         
  414.         ksort($this->words);
  415.     }
  416.  
  417.  
  418.     /**
  419.     * delete all the compiled templates in  a specified language
  420.     *
  421.     * 
  422.     * @param   string   language
  423.     * @param   string   subdirectory of templateDir or empty
  424.     * @return   none
  425.     * @access   public
  426.     */
  427.     function clearTemplateCache($lang='en',$d = '') {
  428.         
  429.         $dname = $d ? $this->options['templateDir'] .'/'.$d  : $this->options['templateDir'];
  430.        
  431.         $dh = opendir($dname);
  432.         while (($name = readdir($dh)) !== false) {
  433.             $fname = $d ? $d .'/'. $name : $name;
  434.             
  435.             if ($name{0} == '.') {
  436.                 continue;
  437.             }
  438.             
  439.             if (is_dir($this->options['templateDir'] . '/'. $fname)) {
  440.                 $this->clearTemplateCache($lang,$fname);
  441.                 continue;
  442.             }
  443.             if (!preg_match('/\.html$/',$name)) {
  444.                 continue;
  445.             }
  446.       
  447.             $file = "{$this->options['compileDir']}/{$fname}.{$lang}.php";
  448.             
  449.             if (file_exists($file)) {
  450.                // echo "DELETE $file?";
  451.                 unlink($file);
  452.             }
  453.         }
  454.         clearstatcache();
  455.     }
  456.    /**
  457.     * output the default template with the editing facilities.
  458.     * 
  459.     * @return   none
  460.     * @access   public
  461.     */
  462.     function outputDefaultTemplate() {
  463.         $o = array(
  464.             'compileDir' => ini_get('session.save_path') . '/HTML_Template_Flexy_Translate',
  465.             'templateDir' => dirname(__FILE__).'/templates'
  466.         );
  467.         $x = new HTML_Template_Flexy( $o );
  468.         $x->compile('translator.html');
  469.         $x->outputObject($this);
  470.     }
  471.         
  472.       
  473.  
  474. }
  475.