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

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