HTML_Template_Flexy
[ class tree: HTML_Template_Flexy ] [ index: HTML_Template_Flexy ] [ all elements ]

Source for file Translator.php

Documentation is available at Translator.php

  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.5 2004/07/26 04:39:24 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.     );
  56.     /**
  57.     * app URL (copied from above)
  58.     *
  59.     * @var string 
  60.     * @access public
  61.     */
  62.     var $appURL;
  63.     var $languages = array();
  64.     /**
  65.     * Array of templates and the words found in each one.
  66.     *
  67.     * @var array 
  68.     * @access public
  69.     */
  70.     var $words= array();   
  71.     /**
  72.     * Array of objects with name, md5's, has it been set, the translation etc.
  73.     *
  74.     * @var array 
  75.     * @access public
  76.     */
  77.     var $status = array();
  78.     /**
  79.     * The current language
  80.     *
  81.     * @var array 
  82.     * @access public
  83.     */
  84.     var $translate ''// language being displayed /edited.
  85.     
  86.     
  87.     /**
  88.     * constructor
  89.     *
  90.     * Just set options (no checking done)
  91.     * 
  92.     * 
  93.     * @param   array   see options array in file.
  94.     * @return   none 
  95.     * @access   public
  96.     */
  97.   
  98.     function HTML_Template_Flexy_Translator($options= array()) {
  99.         foreach($options as $k=>$v{
  100.             $this->options[$k]  $v;
  101.         }
  102.         if (!in_array($this->options['baseLang']$this->options['targetLangs'])) {
  103.             $this->options['targetLangs'][$this->options['baseLang'];
  104.         }
  105.         $o = PEAR::getStaticProperty('HTML_Template_Flexy','options');
  106.         if (!strlen($this->options['templateDir'])) {
  107.             $this->options['templateDir'$o['templateDir'];
  108.         }
  109.         if (!strlen($this->options['compileDir'])) {
  110.             $this->options['compileDir'$o['compileDir'];
  111.         }
  112.         if (!strlen($this->options['url_rewrite'])) {
  113.             $this->options['url_rewrite'$o['url_rewrite'];
  114.         }
  115.         $this->appURL $this->options['appURL'];
  116.         $this->languages $this->options['targetLangs'];
  117.     }
  118.     
  119.     
  120.     /**
  121.     * process the input
  122.     *
  123.     * 
  124.     * @param   array   $_GET; (translate = en)
  125.     * @param   array   $_POST; (translate = en, en[{md5}] = translation)
  126.     
  127.     * @return   none 
  128.     * @access   public
  129.     */
  130.     
  131.     
  132.     function process($get,$post{
  133.         //DB_DataObject::debugLevel(1);
  134.         
  135.         $displayLang = isset($get['translate']$get['translate'
  136.             (isset($post['translate']$post['translate': false);
  137.             
  138.         if ($displayLang === false{
  139.           
  140.             return;
  141.         }
  142.         require_once 'Translation2/Admin.php';
  143.         $trd &new Translation2_Admin('dataobjectsimple''translations' );
  144.         //$trd->setDecoratedLang('en');
  145.         foreach($this->options['targetLangs'as $l{
  146.             $trd->createNewLang(array('lang_id'=>$l));
  147.         }
  148.         
  149.         // back to parent if no language selected..
  150.         
  151.         if (!in_array($displayLang$this->options['targetLangs')) {
  152.             require_once 'PEAR.php';
  153.             return PEAR::raiseError('Unknown Language :' .$displayLang);
  154.         }
  155.         
  156.         $this->translate $displayLang;
  157.         
  158.         
  159.         if (isset($post['_apply'])) {
  160.             $this->clearTemplateCache($displayLang);
  161.              
  162.         }
  163.         $t explode(' ',microtime())$start$t[0$t[1];
  164.      
  165.         require_once 'Translation2.php';
  166.         $tr &new Translation2('dataobjectsimple','translations');
  167.         $tr->setLang($displayLang);
  168.         
  169.         //$suggestions = &new Translation2('dataobjectsimple','translations');
  170.         //$suggestions->setLang($displayLang);
  171.         
  172.         $this->compileAll();
  173.         
  174.         //$tr->setPageID('test.html');
  175.         // delete them after we have compiled them!!
  176.         if (isset($post['_apply'])) {
  177.             $this->clearTemplateCache($displayLang);
  178.         }
  179.         //DB_DataObject::debugLevel(1);
  180.         $this->loadTranslations();
  181.         $this->loadTranslations($displayLang);
  182.         
  183.         $all = array();
  184.         foreach($this->words as $page=>$words{
  185.             $status[$page= array();
  186.             $tr->setPageID($page);
  187.             // pages....
  188.             
  189.             foreach ($words as $word{
  190.             
  191.                 if (!trim(strlen($word))) 
  192.                     continue;
  193.                 }
  194.                 
  195.                 $md5 md5($page.':'.$word);
  196.                 
  197.                 //$value = $tr->get($word);
  198.                 $value $this->getTranslation($page,$word,$displayLang);
  199.                 // we posted something..
  200.                 if (isset($post[$displayLang][$md5])) {
  201.                     $nval get_magic_quotes_gpc(stripslashes($post[$displayLang][$md5]$post[$displayLang][$md5];
  202.                     
  203.                     if ($value != $nval{
  204.                     
  205.                         $trd->add($word,$page,array($displayLang=>$nval));
  206.                         $value $nval;
  207.                     }
  208.                 }
  209.                 
  210.                 if ($value == ''{
  211.                     // try the old gettext...
  212.                     if (isset($old[addslashes($word)])) {
  213.                         $trd->add($word,$page,array($displayLang=>$old[addslashes($word)]));
  214.                         $value $old[addslashes($word)];
  215.                     }
  216.                 
  217.                 
  218.                 }
  219.                 
  220.                 $add = new StdClass;
  221.                  
  222.                 $add->from = $word;
  223.                 $add->to   = $value;
  224.                 if (!$add->to || ($add->from == $add->to)) {
  225.                     $add->untranslated = true;
  226.                     $add->suggest = implode(', '$this->getSuggestions($word$displayLang));
  227.                     //$suggest = $suggestions->get($word);
  228.                     //if ($suggest && ($suggest  != $word)) {
  229.                     //    $add->suggest = $suggestions->get($word);
  230.                     //}
  231.                 }
  232.  
  233.                 $add->md5 = $md5;
  234.                 $add->short = (bool) (strlen($add->from< 30);
  235.                 $status[$page][$add;
  236.             
  237.                  
  238.             }
  239.             
  240.         }
  241.         $t explode(' ',microtime())$total$t[0$t[1-  $start;
  242.         //printf("Built All in %0.2fs<BR>",$total);
  243.         $this->status $status;
  244.           
  245.              
  246.     
  247.     }
  248.     var $translations = array();
  249.     var $translationMap = array();
  250.    
  251.     /**
  252.     * LoadTranslations - load all the translations from the database
  253.     * into $this->translations[{lang}][{id}] = $translation;
  254.     *
  255.     * 
  256.     * @param   string       Language
  257.     * @access   public
  258.     */
  259.     function loadTranslations ($lang= false{
  260.         $d = DB_DataObject::factory('translations');
  261.         $d->lang = ($lang == false'-' $lang;
  262.         $d->find();
  263.         $this->translations[$d->lang= array();
  264.         while ($d->fetch()) {
  265.             $this->translations[$d->lang][$d->string_id$d->translation;
  266.             if ($lang == false{
  267.                 $this->translationMap[$d->page][$d->translation$d->string_id;
  268.             }
  269.             // suggestions:?
  270.             
  271.         }
  272.     }
  273.     
  274.     function getSuggestions($string,$lang{
  275.         $ids = array();
  276.         //echo '<PRE>';print_r($this->translationMap);
  277.         foreach($this->translationMap as $page=>$map{
  278.             if (isset($map[$string])) {
  279.                 $ids[$map[$string];
  280.             }
  281.         }
  282.         //echo '<PRE>';print_r(array($string,$lang,$ids,$this->translations[$lang]));
  283.         
  284.         //exit;
  285.         if (!$ids{
  286.             return array();
  287.         }
  288.         $ret = array();
  289.         foreach($ids as $id{
  290.             if (isset($this->translations[$lang][$id])) {
  291.                 $ret[$this->translations[$lang][$id];
  292.             }
  293.         }
  294.        // echo '<PRE>';print_r($ret);
  295.         return $ret;
  296.     }
  297.     
  298.     function getTranslation($page,$word,$lang)
  299.     {
  300.         
  301.         if (!isset($this->translationMap[$page][$word])) {
  302.             //echo "No string id for $page : $word\n";
  303.             return false;
  304.         }
  305.         if (!isset($this->translations[$lang][$this->translationMap[$page][$word]])) {
  306.         
  307.             return false;
  308.         }
  309.         return $this->translations[$lang][$this->translationMap[$page][$word]];
  310.     }
  311.     /**
  312.     * compile all the templates in a specified folder.
  313.     *
  314.     * 
  315.     * @param   string   subdirectory of templateDir or empty
  316.     * @return   none 
  317.     * @access   public
  318.     */
  319.  
  320.     function compileAll($d=''{
  321.         set_time_limit(0)// this could take quite a while!!!
  322.         
  323.         $words = array();
  324.         $dname $d $this->options['templateDir'.'/'.$d  $this->options['templateDir'];
  325.         //echo "Open $dname<BR>";
  326.         $dh opendir$dname);
  327.         require_once 'HTML/Template/Flexy.php';
  328.         $o $this->options;
  329.         $o['fatalError'= PEAR_ERROR_RETURN;
  330.         $o['locale''en';
  331.         while (($name readdir($dh)) !== false{
  332.             $fname $d $d .'/'$name $name;
  333.             
  334.             if ($name{0== '.'{
  335.                 continue;
  336.             }
  337.             
  338.             if (is_dir($this->options['templateDir''/'$fname)) {
  339.                 $this->compileAll($fname);
  340.                 continue;
  341.             }
  342.                 
  343.                 
  344.             if (!preg_match('/\.html$/',$name)) {
  345.                 continue;
  346.             }
  347.             
  348.             $oo $o;// $oo['debug'] = 1; 
  349.             $x = new HTML_Template_Flexy$oo );
  350.             $r $x->compile($fname);
  351.             
  352.             //printf(" %0.3fs : $fname<BR>", $time);
  353.             if (is_a($r,'PEAR_Error')) {
  354.                 echo "compile failed on $fname<BR>";
  355.                 echo $r->toString();
  356.                 continue;
  357.             }
  358.             $this->words[$fnamefile_exists($x->getTextStringsFile?
  359.                 unserialize(file_get_contents($x->getTextStringsFile)) :
  360.                 array();
  361.         }
  362.         //echo '<PRE>';print_R($words);exit;
  363.         
  364.         ksort($this->words);
  365.     }
  366.  
  367.  
  368.     /**
  369.     * delete all the compiled templates in  a specified language
  370.     *
  371.     * 
  372.     * @param   string   language
  373.     * @param   string   subdirectory of templateDir or empty
  374.     * @return   none 
  375.     * @access   public
  376.     */
  377.     function clearTemplateCache($lang='en',$d ''{
  378.         
  379.         $dname $d $this->options['templateDir'.'/'.$d  $this->options['templateDir'];
  380.        
  381.         $dh opendir($dname);
  382.         while (($name readdir($dh)) !== false{
  383.             $fname $d $d .'/'$name $name;
  384.             
  385.             if ($name{0== '.'{
  386.                 continue;
  387.             }
  388.             
  389.             if (is_dir($this->options['templateDir''/'$fname)) {
  390.                 $this->clearTemplateCache($lang,$fname);
  391.                 continue;
  392.             }
  393.             if (!preg_match('/\.html$/',$name)) {
  394.                 continue;
  395.             }
  396.       
  397.             $file = "{$this->options['compileDir']}/{$fname}.{$lang}.php";
  398.             
  399.             if (file_exists($file)) {
  400.                // echo "DELETE $file?";
  401.                 unlink($file);
  402.             }
  403.         }
  404.         clearstatcache();
  405.     }
  406.    /**
  407.     * output the default template with the editing facilities.
  408.     * 
  409.     * @return   none 
  410.     * @access   public
  411.     */
  412.     function outputDefaultTemplate({
  413.         $o = array(
  414.             'compileDir' => ini_get('session.save_path''/HTML_Template_Flexy_Translate',
  415.             'templateDir' => dirname(__FILE__).'/templates'
  416.         );
  417.         $x = new HTML_Template_Flexy$o );
  418.         $x->compile('translator.html');
  419.         $x->outputObject($this);
  420.     }
  421.         
  422.       
  423.  
  424. }

Documentation generated on Mon, 11 Mar 2019 14:17:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.