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

Source for file Standard.php

Documentation is available at Standard.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: Alan Knowles <alan@akbkhome.com>                            |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Standard.php,v 1.35 2004/06/17 07:21:32 alan_k Exp $
  20. //
  21. //  Base Compiler Class
  22. //  Standard 'Original Flavour' Flexy compiler
  23.  
  24. // this does the main conversion, (eg. for {vars and methods}) 
  25. // it relays into Compiler/Tag & Compiler/Flexy for tags and namespace handling.
  26.  
  27.  
  28.  
  29. require_once 'HTML/Template/Flexy/Tokenizer.php';
  30.  
  31. // cache for po files..
  32. $GLOBALS['_html_template_flexy_compiler_standard']['PO'= array();
  33.  
  34.  
  35. class HTML_Template_Flexy_Compiler_Standard extends HTML_Template_Flexy_Compiler {
  36.     
  37.     
  38.     
  39.     /**
  40.     * The compile method.
  41.     * 
  42.     * @params   object HTML_Template_Flexy
  43.     * @params   string|false string to compile of false to use a file.
  44.     * @return   string   filename of template
  45.     * @access   public
  46.     */
  47.     function compile(&$flexy,$string=false
  48.     {
  49.         // read the entire file into one variable
  50.         
  51.         // note this should be moved to new HTML_Template_Flexy_Token
  52.         // and that can then manage all the tokens in one place..
  53.         global $_HTML_TEMPLATE_FLEXY_COMPILER;
  54.         
  55.         $gettextStrings &$_HTML_TEMPLATE_FLEXY_COMPILER['gettextStrings'];
  56.         $gettextStrings = array()// reset it.
  57.         
  58.         if (@$this->options['debug']{
  59.             echo "compiling template $flexy->currentTemplate<BR>";
  60.             
  61.         }
  62.          
  63.         // reset the elements.
  64.         $flexy->_elements = array();
  65.         
  66.         // replace this with a singleton??
  67.         
  68.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions'$this->options;
  69.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'= array();
  70.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['filename'$flexy->currentTemplate;
  71.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput']  '';
  72.         
  73.         if (is_array($this->options['Translation2'])) {
  74.             require_once 'Translation2.php';
  75.             $this->options['Translation2'= new Translation2(
  76.                 $this->options['Translation2']['driver'],
  77.                 @$this->options['Translation2']['options']
  78.             );
  79.         }
  80.         
  81.         
  82.         if (is_a($this->options['Translation2'],'Translation2')) {
  83.             $this->options['Translation2']->setLang($this->options['locale']);
  84.             // fixme - needs to be more specific to which template to use..
  85.             foreach ($this->options['templateDir'as $tt{
  86.                 $n basename($flexy->currentTemplate);
  87.                 if (substr($flexy->currentTemplate,0,strlen($tt)) == $tt{
  88.                     $n substr($flexy->currentTemplate,strlen($tt)+1);
  89.                 }
  90.                 //echo $n;
  91.             }
  92.             $this->options['Translation2']->setPageID($n);
  93.         else {
  94.             setlocale(LC_ALL$this->options['locale']);
  95.         }
  96.         
  97.         
  98.         
  99.         
  100.         
  101.         
  102.         
  103.         $data $string;
  104.         if ($string === false{
  105.             $data file_get_contents($flexy->currentTemplate);
  106.         }
  107.         
  108.         
  109.         
  110.         // PRE PROCESS {_(.....)} translation markers.
  111.         $got_gettext_markup = false;
  112.         
  113.         
  114.         
  115.         if (strpos($data,'{_('!== false{
  116.             $matches = array();
  117.             $lmatches explode ('{_('$data);
  118.             array_shift($lmatches);
  119.             // shift the first..
  120.             foreach ($lmatches as $k{
  121.                 if (false === strpos($k,')_}')) {
  122.                     continue;
  123.                 }
  124.                 $x explode(')_}',$k);
  125.                 $matches[$x[0];
  126.             }
  127.         
  128.         
  129.            //echo '<PRE>';print_r($matches);
  130.             // we may need to do some house cleaning here...
  131.             $gettextStrings $matches;
  132.             $got_gettext_markup = true;
  133.             
  134.             
  135.             // replace them now..  
  136.             // ** leaving in the tag (which should be ignored by the parser..
  137.             // we then get rid of the tags during the toString method in this class.
  138.             foreach($matches as $v{
  139.                 $data str_replace('{_('.$v.')_}''{_('.$this->translateString($v).')_}',$data);
  140.             }
  141.             
  142.         }
  143.             
  144.         
  145.         
  146.         
  147.         
  148.         
  149.         
  150.         $tokenizer = new HTML_Template_Flexy_Tokenizer($data);
  151.         $tokenizer->fileName = $flexy->currentTemplate;
  152.         
  153.         
  154.         //$tokenizer->debug=1;
  155.         if ($this->options['nonHTML']{
  156.             $tokenizer->ignoreHTML = true;
  157.         }
  158.         if ($this->options['allowPHP']{
  159.             $tokenizer->ignorePHP = false;
  160.         }
  161.         
  162.         $res = HTML_Template_Flexy_Token::buildTokens($tokenizer);
  163.          
  164.         
  165.         if (is_a($res,'PEAR_Error')) {
  166.             return $res;
  167.         }   
  168.         // turn tokens into Template..
  169.         
  170.         $data $res->compile($this);
  171.         
  172.         if (is_a($data,'PEAR_Error')) {
  173.             return $data;
  174.         }
  175.         
  176.         $data $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput'$data;
  177.         
  178.         if (   @$this->options['debug']{
  179.             echo "<B>Result: </B><PRE>".htmlspecialchars($data)."</PRE><BR>";
  180.             
  181.         }
  182.  
  183.         if ($this->options['nonHTML']{
  184.            $data =  str_replace("?>\n","?>\n\n",$data);
  185.         }
  186.         
  187.          
  188.         
  189.         
  190.         // at this point we are into writing stuff...
  191.         if ($this->options['compileToString']{
  192.             $flexy->elements =  $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'];
  193.             return $data;
  194.         }
  195.         
  196.         
  197.         
  198.         
  199.         // error checking?
  200.         if( ($cfp fopen$flexy->compiledTemplate 'w' )) ) {
  201.             if (@$this->options['debug']{
  202.                 echo "<B>Writing: </B>".htmlspecialchars($data)."<BR>";
  203.                 
  204.             }
  205.             fwrite($cfp,$data);
  206.             fclose($cfp);
  207.              chmod($flexy->compiledTemplate,0775);
  208.             // make the timestamp of the two items match.
  209.             clearstatcache();
  210.             touch($flexy->compiledTemplatefilemtime($flexy->currentTemplate));
  211.             
  212.         else {
  213.             return HTML_Template_Flexy::raiseError('HTML_Template_Flexy::failed to write to '.$flexy->compiledTemplate,
  214.         }
  215.         // gettext strings
  216.         if (file_exists($flexy->getTextStringsFile)) {
  217.             unlink($flexy->getTextStringsFile);
  218.         }
  219.         
  220.         if($gettextStrings && ($cfp fopen$flexy->getTextStringsFile'w') ) ) {
  221.             
  222.             fwrite($cfp,serialize(array_unique($gettextStrings)));
  223.             fclose($cfp);
  224.         }
  225.         
  226.         // elements
  227.         if (file_exists($flexy->elementsFile)) {
  228.             unlink($flexy->elementsFile);
  229.         }
  230.         
  231.         if$GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'&&
  232.             ($cfp fopen$flexy->elementsFile'w') ) ) {
  233.             fwrite($cfp,serialize$GLOBALS['_HTML_TEMPLATE_FLEXY']['elements']));
  234.             fclose($cfp);
  235.             // now clear it.
  236.         
  237.         }
  238.         
  239.         return true;
  240.     }
  241.     
  242.     /**
  243.     * Flag indicating compiler is inside {_( .... )_} block, and should not
  244.     * add to the gettextstrings array.
  245.     *
  246.     * @var boolean 
  247.     * @access public
  248.     */
  249.     var $inGetTextBlock = false;
  250.     
  251.     /**
  252.     * This is the base toString Method, it relays into toString{TokenName}
  253.     *
  254.     * @param    object    HTML_Template_Flexy_Token_* 
  255.     * 
  256.     * @return   string     string to build a template
  257.     * @access   public
  258.     * @see      toString*
  259.     */
  260.   
  261.  
  262.     function toString($element
  263.     {
  264.         static $len = 26; // strlen('HTML_Template_Flexy_Token_');
  265.         if ($this->options['debug']{
  266.             $x $element;
  267.             unset($x->children);
  268.             echo htmlspecialchars(print_r($x,true))."<BR>\n";
  269.         }
  270.         if ($element->token == 'GetTextStart'{
  271.             $this->inGetTextBlock = true;
  272.             return '';
  273.         }
  274.         if ($element->token == 'GetTextEnd'{
  275.             $this->inGetTextBlock = false;
  276.             return '';
  277.         }
  278.         
  279.             
  280.         $class get_class($element);
  281.         if (strlen($class>= $len{
  282.             $type substr($class,$len);
  283.             return $this->{'toString'.$type}($element);
  284.         }
  285.         
  286.         $ret $element->value;
  287.         $add $element->compileChildren($this);
  288.         if (is_a($add,'PEAR_Error')) {
  289.             return $add;
  290.         }
  291.         $ret .= $add;
  292.         
  293.         if ($element->close{
  294.             $add $element->close->compile($this);
  295.             if (is_a($add,'PEAR_Error')) {
  296.                 return $add;
  297.             }
  298.             $ret .= $add;
  299.         }
  300.         
  301.         return $ret;
  302.     }
  303.  
  304.  
  305.     /**
  306.     *   HTML_Template_Flexy_Token_Else toString
  307.     *
  308.     * @param    object    HTML_Template_Flexy_Token_Else 
  309.     * 
  310.     * @return   string     string to build a template
  311.     * @access   public
  312.     * @see      toString*
  313.     */
  314.   
  315.  
  316.     function toStringElse($element
  317.      {
  318.         // pushpull states to make sure we are in an area.. - should really check to see 
  319.         // if the state it is pulling is a if...
  320.         if ($element->pullState(=== false{
  321.             return $this->appendHTML(
  322.                 "<font color=\"red\">Unmatched {else:} on line: {$element->line}</font>"
  323.                 );
  324.         }
  325.         $element->pushState();
  326.         return $this->appendPhp("} else {");
  327.     }
  328.     
  329.     /**
  330.     *   HTML_Template_Flexy_Token_End toString
  331.     *
  332.     * @param    object    HTML_Template_Flexy_Token_Else 
  333.     * 
  334.     * @return   string     string to build a template
  335.     * @access   public
  336.     * @see      toString*
  337.     */
  338.   
  339.     function toStringEnd($element
  340.     {
  341.         // pushpull states to make sure we are in an area.. - should really check to see 
  342.         // if the state it is pulling is a if...
  343.         if ($element->pullState(=== false{
  344.             return $this->appendHTML(
  345.                 "<font color=\"red\">Unmatched {end:} on line: {$element->line}</font>"
  346.                 );
  347.         }
  348.          
  349.         return $this->appendPhp("}");
  350.     }
  351.  
  352.     /**
  353.     *   HTML_Template_Flexy_Token_EndTag toString
  354.     *
  355.     * @param    object    HTML_Template_Flexy_Token_EndTag 
  356.     * 
  357.     * @return   string     string to build a template
  358.     * @access   public
  359.     * @see      toString*
  360.     */
  361.   
  362.  
  363.  
  364.     function toStringEndTag($element
  365.     {
  366.         return $this->toStringTag($element);
  367.     }
  368.         
  369.     
  370.     
  371.     /**
  372.     *   HTML_Template_Flexy_Token_Foreach toString
  373.     *
  374.     * @param    object    HTML_Template_Flexy_Token_Foreach 
  375.     * 
  376.     * @return   string     string to build a template
  377.     * @access   public
  378.     * @see      toString*
  379.     */
  380.   
  381.     
  382.     function toStringForeach($element
  383.     {
  384.     
  385.         $loopon $element->toVar($element->loopOn);
  386.         if (is_a($loopon,'PEAR_Error')) {
  387.             return $loopon;
  388.         }
  389.         
  390.         $ret 'if ($this->options[\'strict\'] || ('.
  391.             'is_array('$loopon')  || ' .
  392.             'is_object(' $loopon  '))) ' .
  393.             'foreach(' $loopon  " ";
  394.             
  395.         $ret .= "as \${$element->key}";
  396.         
  397.         if ($element->value{
  398.             $ret .=  " => \${$element->value}";
  399.         }
  400.         $ret .= ") {";
  401.         
  402.         $element->pushState();
  403.         $element->pushVar($element->key);
  404.         $element->pushVar($element->value);
  405.         return $this->appendPhp($ret);
  406.     }
  407.     /**
  408.     *   HTML_Template_Flexy_Token_If toString
  409.     *
  410.     * @param    object    HTML_Template_Flexy_Token_If 
  411.     * 
  412.     * @return   string     string to build a template
  413.     * @access   public
  414.     * @see      toString*
  415.     */
  416.   
  417.     function toStringIf($element
  418.     {
  419.         
  420.         $var $element->toVar($element->condition);
  421.         if (is_a($var,'PEAR_Error')) {
  422.             return $var;
  423.         }
  424.         
  425.         $ret "if (".$element->isNegative . $var .")  {";
  426.         $element->pushState();
  427.         return $this->appendPhp($ret);
  428.     }
  429.  
  430.    /**
  431.     *  get Modifier Wrapper
  432.     *
  433.     * converts :h, :u, :r , .....
  434.     * @param    object    HTML_Template_Flexy_Token_Method|Var
  435.     * 
  436.     * @return   array prefix,suffix
  437.     * @access   public
  438.     * @see      toString*
  439.     */
  440.  
  441.     function getModifierWrapper($element
  442.     {
  443.         $prefix 'echo ';
  444.         
  445.         $suffix '';
  446.         $modifier strlen(trim($element->modifier)) $element->modifier : ' ';
  447.         
  448.         switch ($modifier{0}{
  449.             case 'h':
  450.                 break;
  451.             case 'u':
  452.                 $prefix 'echo urlencode(';
  453.                 $suffix ')';
  454.                 break;
  455.             case 'r':
  456.                 $prefix 'echo \'<pre>\'; echo htmlspecialchars(print_r(';
  457.                 $suffix ',true)); echo \'</pre>\';';
  458.                 break;                
  459.             case 'n'
  460.                 // blank or value..
  461.                 $numberformat @$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['numberFormat'];
  462.                 $prefix 'echo number_format(';
  463.                 $suffix $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['numberFormat'')';
  464.                 break;
  465.             case 'b'// nl2br + htmlspecialchars
  466.                 $prefix 'echo nl2br(htmlspecialchars(';
  467.                 
  468.                 // add language ?
  469.                 $suffix '))';
  470.                 break;
  471.             case ' ':
  472.                 $prefix 'echo htmlspecialchars(';
  473.                 // add language ?
  474.                 $suffix ')';
  475.                 break;
  476.             default:
  477.                $prefix 'echo $this->plugin("'.trim($element->modifier.'",';
  478.                $suffix ')'
  479.             
  480.             
  481.         }
  482.         
  483.         return array($prefix,$suffix);
  484.     }
  485.  
  486.  
  487.  
  488.   /**
  489.     *   HTML_Template_Flexy_Token_Var toString
  490.     *
  491.     * @param    object    HTML_Template_Flexy_Token_Method 
  492.     * 
  493.     * @return   string     string to build a template
  494.     * @access   public
  495.     * @see      toString*
  496.     */
  497.   
  498.     function toStringVar($element
  499.     {
  500.         // ignore modifier at present!!
  501.         
  502.         $var $element->toVar($element->value);
  503.         if (is_a($var,'PEAR_Error')) {
  504.             return $var;
  505.         }
  506.         list($prefix,$suffix$this->getModifierWrapper($element);
  507.         return $this->appendPhp$prefix $var $suffix .';');
  508.     }
  509.    /**
  510.     *   HTML_Template_Flexy_Token_Method toString
  511.     *
  512.     * @param    object    HTML_Template_Flexy_Token_Method 
  513.     * 
  514.     * @return   string     string to build a template
  515.     * @access   public
  516.     * @see      toString*
  517.     */
  518.   
  519.     function toStringMethod($element
  520.     {
  521.  
  522.               
  523.         // set up the modifier at present!!
  524.         list($prefix,$suffix$this->getModifierWrapper($element);
  525.         
  526.         // add the '!' to if
  527.         
  528.         if ($element->isConditional{
  529.             $prefix 'if ('.$element->isNegative;
  530.             $element->pushState();
  531.             $suffix ')';
  532.         }  
  533.         
  534.         
  535.         // check that method exists..
  536.         // if (method_exists($object,'method');
  537.         $bits explode('.',$element->method);
  538.         $method array_pop($bits);
  539.         
  540.         $object implode('.',$bits);
  541.         
  542.         $var $element->toVar($object);
  543.         if (is_a($var,'PEAR_Error')) {
  544.             return $var;
  545.         }
  546.         
  547.         if (($object == 'GLOBALS'&& 
  548.             $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['globalfunctions']{
  549.             // we should check if they something weird like: GLOBALS.xxxx[sdf](....)
  550.             $var $method;
  551.         else {
  552.             $prefix 'if ($this->options[\'strict\'] || (isset('.$var.
  553.                 ') && method_exists('.$var .",'{$method}'))) " . $prefix;
  554.             $var $element->toVar($element->method);
  555.         }
  556.         
  557.  
  558.         if (is_a($var,'PEAR_Error')) {
  559.             return $var;
  560.         }
  561.         
  562.         $ret  =  $prefix;
  563.         $ret .=  $var "(";
  564.         $s =0;
  565.          
  566.        
  567.          
  568.         foreach($element->args as $a{
  569.              
  570.             if ($s{
  571.                 $ret .= ",";
  572.             }
  573.             $s =1;
  574.             if ($a{0== '#'{
  575.                 $ret .= '"'addslashes(substr($a,1,-1)) '"';
  576.                 continue;
  577.             }
  578.             
  579.             $var $element->toVar($a);
  580.             if (is_a($var,'PEAR_Error')) {
  581.                 return $var;
  582.             }
  583.             $ret .= $var;
  584.             
  585.         }
  586.         $ret .= ")" $suffix;
  587.         
  588.         if ($element->isConditional{
  589.             $ret .= ' { ';
  590.         else {
  591.             $ret .= ";";
  592.         }
  593.         
  594.         
  595.         
  596.         return $this->appendPhp($ret)
  597.         
  598.          
  599.  
  600.    }
  601.    /**
  602.     *   HTML_Template_Flexy_Token_Processing toString
  603.     *
  604.     * @param    object    HTML_Template_Flexy_Token_Processing 
  605.     * 
  606.     * @return   string     string to build a template
  607.     * @access   public
  608.     * @see      toString*
  609.     */
  610.  
  611.  
  612.     function toStringProcessing($element
  613.     {
  614.         // if it's XML then quote it..
  615.         if (strtoupper(substr($element->value,2,3)) == 'XML'
  616.             return $this->appendPhp("echo '" str_replace("'","\\"."'"$element->value"';");
  617.         }
  618.         // otherwise it's PHP code - so echo it..
  619.         return $element->value;
  620.     }
  621.     
  622.     /**
  623.     *   HTML_Template_Flexy_Token_Text toString
  624.     *
  625.     * @param    object    HTML_Template_Flexy_Token_Text 
  626.     * 
  627.     * @return   string     string to build a template
  628.     * @access   public
  629.     * @see      toString*
  630.     */
  631.  
  632.  
  633.  
  634.     function toStringText($element
  635.     {
  636.         
  637.         // first get rid of stuff thats not translated etc.
  638.         // empty strings => output.
  639.         // comments -> just output
  640.         // our special tags -> output..
  641.         
  642.         if (!strlen(trim($element->value) )) {
  643.             return $this->appendHtml($element->value);
  644.         }
  645.         // dont add comments to translation lists.
  646.          
  647.         if (substr($element->value,0,4== '<!--'{
  648.             return $this->appendHtml($element->value);
  649.         }
  650.         // ignore anything wrapped with {_( .... )_}
  651.         if ($this->inGetTextBlock{
  652.             return $this->appendHtml($element->value);
  653.         }
  654.         
  655.         // argTokens is built before the tag matching (it combined
  656.         // flexy tags into %s, into the string,
  657.         // and made a list of tokens in argTokens.
  658.         
  659.         if (!count($element->argTokens&& !$element->isWord()) {
  660.             return $this->appendHtml($element->value);
  661.         }
  662.         
  663.         // grab the white space at start and end (and keep it!
  664.         
  665.         $value ltrim($element->value);
  666.         $front substr($element->value,0,-strlen($value));
  667.         $value rtrim($element->value);
  668.         $rear  substr($element->value,strlen($value));
  669.         $value trim($element->value);
  670.         
  671.         
  672.         // convert to escaped chars.. (limited..)
  673.         //$value = strtr($value,$cleanArray);
  674.         
  675.         $this->addStringToGettext($value);
  676.         $value $this->translateString($value);
  677.         // its a simple word!
  678.         if (!count($element->argTokens)) {
  679.             return $this->appendHtml($front $value $rear);
  680.         }
  681.         
  682.         
  683.         // there are subtokens..
  684.         // print_r($element->argTokens );
  685.         $args = array();
  686.         // these should only be text or vars..
  687.         
  688.         foreach($element->argTokens as $i=>$token{
  689.             $args[$token->compile($this);
  690.         }
  691.         
  692.         // we break up the translated string, and put the compiled tags 
  693.         // in between the values here.
  694.         
  695.         $bits explode('%s',$value);
  696.         $ret  $front;
  697.         
  698.         foreach($bits as $i=>$v{
  699.             $ret .= $v @$args[$i];
  700.         }
  701.         
  702.         return  $ret $rear;
  703.         
  704.     }
  705.     /**
  706.     * addStringToGettext
  707.     *
  708.     * Adds a string to the gettext array.
  709.     * 
  710.     * @param   mixed        preferably.. string to store
  711.     *
  712.     * @return   none 
  713.     * @access   public
  714.     */
  715.     
  716.     function addStringToGettext($string
  717.     {
  718.     
  719.         
  720.         
  721.         
  722.         if (!is_string($string)) {
  723.             return;
  724.         }
  725.         
  726.         if (!preg_match('/[a-z]+/i'$string)) {
  727.             return;
  728.         }
  729.         $string trim($string);
  730.         
  731.         if (substr($string,0,4== '<!--'{
  732.             return;
  733.         }
  734.         
  735.         $GLOBALS['_HTML_TEMPLATE_FLEXY_COMPILER']['gettextStrings'][$string;
  736.     }
  737.     
  738.     
  739.     /**
  740.     * translateString - a gettextWrapper
  741.     *
  742.     * tries to do gettext or falls back on File_Gettext
  743.     * This has !!!NO!!! error handling - if it fails you just get english..
  744.     * no questions asked!!!
  745.     * 
  746.     * @param   string       string to translate
  747.     *
  748.     * @return   string      translated string..
  749.     * @access   public
  750.     */
  751.   
  752.     function translateString($string)
  753.     {
  754.          
  755.         
  756.         
  757.         if (is_a($this->options['Translation2'],'Translation2')) {
  758.             $result $this->options['Translation2']->get($string);
  759.             if (!empty($result)) {
  760.                 return $result;
  761.             }
  762.             return $string;
  763.         }
  764.         
  765.         // note this stuff may have been broken by removing the \n replacement code 
  766.         // since i dont have a test for it... it may remain broken..
  767.         // use Translation2 - it has gettext backend support
  768.         // and should sort out the mess that \n etc. entail.
  769.         
  770.         
  771.         $prefix basename($GLOBALS['_HTML_TEMPLATE_FLEXY']['filename']).':';
  772.         if (@$this->options['debug']{
  773.             echo __CLASS__.":TRANSLATING $string<BR>";
  774.         }
  775.         if (function_exists('gettext'&& !$this->options['textdomain']{
  776.             if (@$this->options['debug']{
  777.                 echo __CLASS__.":USING GETTEXT?<BR>";
  778.             }
  779.             $t gettext($string);
  780.             if ($t != $string{
  781.                 return $t;
  782.             }
  783.             $tt gettext($prefix.$string);
  784.             if ($tt != $prefix.$string{
  785.                 return $tt;
  786.             }
  787.             // give up it's not translated anywhere...
  788.             return $t;
  789.              
  790.         }
  791.         if (!$this->options['textdomain'|| !$this->options['textdomainDir']{
  792.             // text domain is not set..
  793.             if (@$this->options['debug']{
  794.                 echo __CLASS__.":MISSING textdomain settings<BR>";
  795.             }
  796.             return $string;
  797.         }
  798.         $pofile $this->options['textdomainDir'
  799.                 '/' $this->options['locale'
  800.                 '/LC_MESSAGES/' $this->options['textdomain''.po';
  801.         
  802.         
  803.         // did we try to load it already..
  804.         if (@$GLOBALS['_'.__CLASS__]['PO'][$pofile=== false{
  805.             if (@$this->options['debug']{
  806.                 echo __CLASS__.":LOAD failed (Cached):<BR>";
  807.             }
  808.             return $string;
  809.         }
  810.         if (!@$GLOBALS['_'.__CLASS__]['PO'][$pofile]{
  811.             // default - cant load it..
  812.             $GLOBALS['_'.__CLASS__]['PO'][$pofile= false;
  813.             if (!file_exists($pofile)) {
  814.                  if (@$this->options['debug']{
  815.                 echo __CLASS__.":LOAD failed: {$pofile}<BR>";
  816.             }
  817.                 return $string;
  818.             }
  819.             
  820.             if (!@include_once 'File/Gettext.php'{
  821.                 if (@$this->options['debug']{
  822.                     echo __CLASS__.":LOAD no File_gettext:<BR>";
  823.                 }
  824.                 return $string;
  825.             }
  826.             
  827.             $GLOBALS['_'.__CLASS__]['PO'][$pofile= File_Gettext::factory('PO',$pofile);
  828.             $GLOBALS['_'.__CLASS__]['PO'][$pofile]->load();
  829.             //echo '<PRE>'.htmlspecialchars(print_r($GLOBALS['_'.__CLASS__]['PO'][$pofile]->strings,true));
  830.             
  831.         }
  832.         $po &$GLOBALS['_'.__CLASS__]['PO'][$pofile];
  833.         // we should have it loaded now...
  834.         // this is odd - data is a bit messed up with CR's
  835.         $string str_replace('\n',"\n",$string);
  836.         
  837.         if (isset($po->strings[$prefix.$string])) {
  838.             return $po->strings[$prefix.$string];
  839.         }
  840.         
  841.         if (!isset($po->strings[$string])) {
  842.             if (@$this->options['debug']{
  843.                     echo __CLASS__.":no match:<BR>";
  844.             }
  845.             return $string;
  846.         }
  847.         if (@$this->options['debug']{
  848.             echo __CLASS__.":MATCHED: {$po->strings[$string]}<BR>";
  849.         }
  850.         
  851.         // finally we have a match!!!
  852.         return $po->strings[$string];
  853.         
  854.     
  855.      /**
  856.     *   HTML_Template_Flexy_Token_Tag toString
  857.     *
  858.     * @param    object    HTML_Template_Flexy_Token_Tag 
  859.     * 
  860.     * @return   string     string to build a template
  861.     * @access   public
  862.     * @see      toString*
  863.     */
  864.   
  865.     function toStringTag($element{
  866.         if (strpos($element->tag,':'=== false{
  867.             $namespace 'Tag';
  868.         else {
  869.             $bits =  explode(':',$element->tag);
  870.             $namespace $bits[0];
  871.         }
  872.         if ($namespace{0== '/'{
  873.             $namespace substr($namespace,1);
  874.         }
  875.         if (empty($this->tagHandlers[$namespace])) {
  876.             
  877.             require_once 'HTML/Template/Flexy/Compiler/Standard/Tag.php';
  878.             $this->tagHandlers[$namespace&HTML_Template_Flexy_Compiler_Standard_Tag::factory($namespace,$this);
  879.             if (!$this->tagHandlers[$namespace{
  880.                 return HTML_Template_Flexy::raiseError('HTML_Template_Flexy::failed to create Namespace Handler '.$namespace 
  881.                     ' in file ' $GLOBALS['_HTML_TEMPLATE_FLEXY']['filename'],
  882.                     HTML_TEMPLATE_FLEXY_ERROR_SYNTAX ,HTML_TEMPLATE_FLEXY_ERROR_RETURN);
  883.             }
  884.                 
  885.         }
  886.         return $this->tagHandlers[$namespace]->toString($element);
  887.         
  888.         
  889.     }
  890.     
  891.  
  892. }

Documentation generated on Mon, 11 Mar 2019 13:51:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.