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 334846 2014-09-12 04:50:56Z alan_k $
  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.         if (class_exists('PEAR5',false)) {
  111.             $o = PEAR5::getStaticProperty('HTML_Template_Flexy','options');
  112.         }
  113.         if (empty($o)) {
  114.             $o = PEAR::getStaticProperty('HTML_Template_Flexy','options');
  115.         }
  116.         if (!strlen($this->options['templateDir'])) {
  117.             $this->options['templateDir'$o['templateDir'];
  118.         }
  119.         if (!strlen($this->options['compileDir'])) {
  120.             $this->options['compileDir'$o['compileDir'];
  121.         }
  122.         if (!strlen($this->options['url_rewrite'])) {
  123.             $this->options['url_rewrite'$o['url_rewrite'];
  124.         }
  125.         if (empty($this->options['Translation2'])) {
  126.             $this->options['Translation2'$o['Translation2'];
  127.         }
  128.         $this->appURL $this->options['appURL'];
  129.         $this->languages $this->options['targetLangs'];
  130.     }
  131.     
  132.     
  133.     /**
  134.     * process the input
  135.     *
  136.     * 
  137.     * @param   array   $_GET; (translate = en)
  138.     * @param   array   $_POST; (translate = en, en[{md5}] = translation)
  139.     
  140.     * @return   none 
  141.     * @access   public
  142.     */
  143.     
  144.     
  145.     function process($get,$post)
  146.     {
  147.         //DB_DataObject::debugLevel(1);
  148.         
  149.         $displayLang = isset($get['translate']$get['translate'
  150.             (isset($post['translate']$post['translate': false);
  151.             
  152.         if ($displayLang === false{          
  153.             return;
  154.         }
  155.         require_once 'Translation2/Admin.php';
  156.           
  157.         $driver $this->options['Translation2']['driver'];
  158.         $options $this->options['Translation2']['options'];
  159.         $usingGT ($driver == 'gettext');
  160.         $usingDO ($driver == 'dataobjectsimple');
  161.         $trd &Translation2_Admin::factory($driver$options);
  162.         
  163.         
  164.         
  165.         //$trd->setDecoratedLang('en');
  166.         foreach($this->options['targetLangs'as $l{
  167.             $trd->addLang(array(
  168.                 'lang_id' => $l
  169.             ));
  170.         }
  171.         
  172.         // back to parent if no language selected..
  173.         
  174.         if (!in_array($displayLang$this->options['targetLangs')) {
  175.             require_once 'PEAR.php';
  176.             $p = new PEAR();
  177.             return $p->raiseError('Unknown Language :' .$displayLang);
  178.         }
  179.         
  180.         $this->translate $displayLang;
  181.         
  182.         
  183.         if (isset($post['_apply'])) {
  184.             $this->clearTemplateCache($displayLang);
  185.              
  186.         }
  187.         $t explode(' ',microtime())$start$t[0$t[1];
  188.      
  189.         require_once 'Translation2.php';
  190.         $tr &Translation2::factory($driver$options);
  191.         $tr->setLang($displayLang);
  192.         
  193.         if (!$usingDO{
  194.             $suggestions &Translation2::factory($driver$options);
  195.             $suggestions->setLang($displayLang);
  196.         }
  197.         
  198.         $this->compileAll();
  199.         
  200.         //$tr->setPageID('test.html');
  201.         // delete them after we have compiled them!!
  202.         if (isset($post['_apply'])) {
  203.             $this->clearTemplateCache($displayLang);
  204.         }
  205.         //DB_DataObject::debugLevel(1);
  206.         if ($usingDO{
  207.             $this->loadTranslations();
  208.             $this->loadTranslations($displayLang);
  209.         }
  210.         
  211.         $all = array();
  212.         
  213.         if ($usingGT{
  214.             $trd->storage->begin();
  215.         }
  216.         $displayLangClean str_replace('.''_'$displayLang);
  217.                  
  218.         foreach($this->words as $page=>$words{
  219.             $status[$page= array();
  220.             $tr->setPageID($page);
  221.             // pages....
  222.             if (isset($post['_clear']&& !PEAR::isError($p $trd->getPage($page$displayLang))) {
  223.                 $diff array_diff(array_keys($p)$words);
  224.                 if (count($diff)) {
  225.                     foreach ($diff as $string{
  226.                         $trd->remove($string$page);
  227.                     }
  228.                 }
  229.             }
  230.  
  231.             foreach ($words as $word{
  232.             
  233.                 if (!strlen(trim($word))) 
  234.                     continue;
  235.                 }
  236.                 
  237.                 $md5 md5($page.':'.$word);
  238.                 
  239.                 $value $usingDO $this->getTranslation($page,$word,$displayLang$tr->get($word);
  240.                 
  241.                 // we posted something..
  242.                 if (isset($post[$displayLangClean][$md5])) {
  243.                     // eak we shouldnt really deal with magic_quotes!!!
  244.                     $nval str_replace("\r\n""\n"
  245.                         get_magic_quotes_gpc(
  246.                             stripslashes($post[$displayLangClean][$md5]
  247.                             $post[$displayLangClean][$md5]);
  248.                     
  249.                     if ($value != $nval{
  250.                         $trd->add($word,$page,array($displayLang=>$nval));
  251.                         $value $nval;
  252.                     }
  253.                 }
  254.                 
  255.                 if ($value == ''{
  256.                     // try the old gettext...
  257.                     if (isset($old[addslashes($word)])) {
  258.                         $trd->add($word,$page,array($displayLang=>$old[addslashes($word)]));
  259.                         $value $old[addslashes($word)];
  260.                     }
  261.                 
  262.                 
  263.                 }
  264.                 
  265.                 $add = new StdClass;
  266.                  
  267.                 $add->from = $word;
  268.                 $add->to   = $value;
  269.                 if (!$add->to || ($add->from == $add->to)) {
  270.                     $add->untranslated = true;
  271.                     
  272.                     if ($usingDO{
  273.                         $add->suggest = implode(', '$this->getSuggestions($word$displayLang));
  274.                     else {
  275.                         $suggest $suggestions->get($word);
  276.                         if ($suggest && ($suggest != $word)) {
  277.                             $add->suggest = $suggest;
  278.                         }
  279.                     }
  280.                     
  281.                     
  282.                 }
  283.  
  284.                 $add->md5 = $md5;
  285.                 // show big or small text entry..
  286.                 $add->short = (bool) (strlen($add->from< 30 && strstr($add->from"\n"=== false);
  287.                 
  288.                 $status[$page][$add;
  289.             
  290.                  
  291.             }
  292.             
  293.         }
  294.         if ($usingGT{
  295.             $trd->storage->commit();
  296.         }
  297.         $t explode(' ',microtime())$total$t[0$t[1-  $start;
  298.         //printf("Built All in %0.2fs<BR>",$total);
  299.         $this->status $status;
  300.           
  301.              
  302.     
  303.     }
  304.     var $translations = array();
  305.     var $translationMap = array();
  306.    
  307.     /**
  308.     * LoadTranslations - load all the translations from the database
  309.     * into $this->translations[{lang}][{id}] = $translation;
  310.     *
  311.     * 
  312.     * @param   string       Language
  313.     * @access   public
  314.     */
  315.     function loadTranslations ($lang= false{
  316.         $d = DB_DataObject::factory('translations');
  317.         $d->lang = ($lang == false'-' $lang;
  318.         $d->find();
  319.         $this->translations[$d->lang= array();
  320.         while ($d->fetch()) {
  321.             $this->translations[$d->lang][$d->string_id$d->translation;
  322.             if ($lang == false{
  323.                 $this->translationMap[$d->page][$d->translation$d->string_id;
  324.             }
  325.             // suggestions:?
  326.             
  327.         }
  328.     }
  329.     
  330.     function getSuggestions($string,$lang{
  331.         $ids = array();
  332.         //echo '<PRE>';print_r($this->translationMap);
  333.         foreach($this->translationMap as $page=>$map{
  334.             if (isset($map[$string])) {
  335.                 $ids[$map[$string];
  336.             }
  337.         }
  338.         //echo '<PRE>';print_r(array($string,$lang,$ids,$this->translations[$lang]));
  339.         
  340.         //exit;
  341.         if (!$ids{
  342.             return array();
  343.         }
  344.         $ret = array();
  345.         foreach($ids as $id{
  346.             if (isset($this->translations[$lang][$id])) {
  347.                 $ret[$this->translations[$lang][$id];
  348.             }
  349.         }
  350.        // echo '<PRE>';print_r($ret);
  351.         return $ret;
  352.     }
  353.     
  354.     function getTranslation($page,$word,$lang)
  355.     {
  356.         
  357.         if (!isset($this->translationMap[$page][$word])) {
  358.             //echo "No string id for $page : $word\n";
  359.             return false;
  360.         }
  361.         if (!isset($this->translations[$lang][$this->translationMap[$page][$word]])) {
  362.         
  363.             return false;
  364.         }
  365.         return $this->translations[$lang][$this->translationMap[$page][$word]];
  366.     }
  367.     /**
  368.     * compile all the templates in a specified folder.
  369.     *
  370.     * 
  371.     * @param   string   subdirectory of templateDir or empty
  372.     * @return   none 
  373.     * @access   public
  374.     */
  375.  
  376.     function compileAll($d='')
  377.     {
  378.         set_time_limit(0)// this could take quite a while!!!
  379.         
  380.         $words = array();
  381.         $dname $d $this->options['templateDir'.'/'.$d  $this->options['templateDir'];
  382.         //echo "Open $dname<BR>";
  383.         $dh opendir$dname);
  384.         require_once 'HTML/Template/Flexy.php';
  385.         $o $this->options;
  386.         $o['fatalError'= PEAR_ERROR_RETURN;
  387.         $o['locale''en';
  388.         while (($name readdir($dh)) !== false{
  389.             $fname $d $d .'/'$name $name;
  390.             
  391.             if ($name{0== '.'{
  392.                 continue;
  393.             }
  394.             
  395.             if (is_dir($this->options['templateDir''/'$fname)) {
  396.                 $this->compileAll($fname);
  397.                 continue;
  398.             }
  399.                 
  400.                 
  401.             if (!preg_match('/\.html$/',$name)) {
  402.                 continue;
  403.             }
  404.             
  405.             $oo $o;// $oo['debug'] = 1; 
  406.             $x = new HTML_Template_Flexy$oo );
  407.             $r $x->compile($fname);
  408.             
  409.             //printf(" %0.3fs : $fname<BR>", $time);
  410.             if (is_object($r&& is_a($r,'PEAR_Error')) {
  411.                 echo "compile failed on $fname<BR>";
  412.                 echo $r->toString();
  413.                 continue;
  414.             }
  415.             $this->words[$fnamefile_exists($x->getTextStringsFile?
  416.                 unserialize(file_get_contents($x->getTextStringsFile)) :
  417.                 array();
  418.         }
  419.         //echo '<PRE>';print_R($words);exit;
  420.         
  421.         ksort($this->words);
  422.     }
  423.  
  424.  
  425.     /**
  426.     * delete all the compiled templates in  a specified language
  427.     *
  428.     * 
  429.     * @param   string   language
  430.     * @param   string   subdirectory of templateDir or empty
  431.     * @return   none 
  432.     * @access   public
  433.     */
  434.     function clearTemplateCache($lang='en',$d ''{
  435.         
  436.         $dname $d $this->options['templateDir'.'/'.$d  $this->options['templateDir'];
  437.        
  438.         $dh opendir($dname);
  439.         while (($name readdir($dh)) !== false{
  440.             $fname $d $d .'/'$name $name;
  441.             
  442.             if ($name{0== '.'{
  443.                 continue;
  444.             }
  445.             
  446.             if (is_dir($this->options['templateDir''/'$fname)) {
  447.                 $this->clearTemplateCache($lang,$fname);
  448.                 continue;
  449.             }
  450.             if (!preg_match('/\.html$/',$name)) {
  451.                 continue;
  452.             }
  453.       
  454.             $file = "{$this->options['compileDir']}/{$fname}.{$lang}.php";
  455.             
  456.             if (file_exists($file)) {
  457.                // echo "DELETE $file?";
  458.                 unlink($file);
  459.             }
  460.         }
  461.         clearstatcache();
  462.     }
  463.    /**
  464.     * output the default template with the editing facilities.
  465.     * 
  466.     * @return   none 
  467.     * @access   public
  468.     */
  469.     function outputDefaultTemplate({
  470.         $o = array(
  471.             'compileDir' => ini_get('session.save_path''/HTML_Template_Flexy_Translate',
  472.             'templateDir' => dirname(__FILE__).'/templates'
  473.         );
  474.         $x = new HTML_Template_Flexy$o );
  475.         $x->compile('translator.html');
  476.         $x->outputObject($this);
  477.     }
  478.         
  479.       
  480.  
  481. }

Documentation generated on Mon, 11 Mar 2019 16:00:04 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.