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

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